ValueError:连接轴的所有输入数组维度必须完全匹配,但必须沿维度1匹配,

2024-10-03 02:32:38 发布

您现在位置:Python中文网/ 问答频道 /正文

在处理ML项目时,我们遇到以下值错误: ValueError:串联轴的所有输入数组维度必须完全匹配,但沿维度1,索引0处的数组大小为118,索引1处的数组大小为850

我们一直致力于的功能如下所示:

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

导致错误的代码行是:

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

SegAllFeat的形状为(850,80),ListOfFrame2Vec的形状为(0118,80),但对这些阵列的重塑尚未成功


Tags: pathnameifnpsegmentarraystartfile