重塑NumPy数组

时间:2020-06-16 16:15:32

标签: python numpy reshape numpy-ndarray

SegAllFeat的形状为(850,80),而ogListOfFrame2Vec的形状为(0,118,80)。 SegAllFeat和ListofFrame2Vec都是NumPy数组。

def function_FeatureExtract(path, indices, featureNum, classLabel):

    extension = '.wav'

    ListOfFrame2Vec = np.empty((0, frame_number, featureNum))
    LabelOfTotalFrame2Vec = np.empty((0, 1, 1))

    DictionaryFiletoIndex=[]

    for root, dirs_list, files_list in os.walk(path):
        for file_name in files_list:
            #if os.path.splitext(file_name)[-1] == extension:
            audiofile = os.path.join(root, file_name)
            print(audiofile)
            audio, s_rate = librosa.load(audiofile, sr=sample_rate)
            #print(file_name)
            segment_start_flag=0
            start_seg = 0
            while (start_seg + segment_length) < len(audio):

                sound1 = audio[start_seg:(start_seg + segment_length)]

                featureSet=function_FeatureExtractfromSinglewindow(sound1, hop_length, sample_rate)

                if segment_start_flag==0:
                    SegAllFeat=featureSet
                    segment_start_flag=1
                else:
                    SegAllFeat=np.vstack((SegAllFeat,featureSet))

                start_seg = start_seg + overlappiong

            if segment_start_flag==1:
                SegAllFeat=normalize(SegAllFeat, norm = 'l2', axis=0)
                SegAllFeat=SegAllFeat[:, indices[0:featureNum]]

            ListOfFrame2Vec = np.append(ListOfFrame2Vec, array([SegAllFeat]), axis=0)

            if classLabel == 1:
                LabelOfTotalFrame2Vec=np.append(LabelOfTotalFrame2Vec,np.array([1]))
            else:
                LabelOfTotalFrame2Vec = np.append(LabelOfTotalFrame2Vec, np.array([0]))
            #LabelOfTotalFrame2Vec



            DictionaryFiletoIndex.append(file_name)


            print(ListOfFrame2Vec.shape)
            print(LabelOfTotalFrame2Vec.shape)
    return ListOfFrame2Vec, LabelOfTotalFrame2Vec, DictionaryFiletoIndex

我需要将SegAllFeat重塑为(0,850,80)。我该怎么做才能得到这种形状?

0 个答案:

没有答案