Python ValueError:无法将输入数组从形状(20590)广播到形状(20)

Python ValueError:无法将输入数组从形状(20590)广播到形状(20),python,numpy,machine-learning,signal-processing,mfcc,Python,Numpy,Machine Learning,Signal Processing,Mfcc,我试图通过使用声音文件的MFCC从.wav文件中提取特征。尝试将MFCC列表转换为numpy数组时出错。我很确定发生此错误是因为列表包含具有不同形状的MFCC值(但我不确定如何解决此问题) 我已经看了另外两篇stackoverflow文章,但是它们并不能解决我的问题,因为它们对某项任务太具体了 完整错误消息: 回溯(最近一次调用上次):文件 “/…../…/…/Batch_MFCC_Data.py”, 第68行,在 X=np.数组(MFCCs)值错误:无法将输入数组从形状(20590)广播到

我试图通过使用声音文件的MFCC从.wav文件中提取特征。尝试将MFCC列表转换为numpy数组时出错。我很确定发生此错误是因为列表包含具有不同形状的MFCC值(但我不确定如何解决此问题)

我已经看了另外两篇stackoverflow文章,但是它们并不能解决我的问题,因为它们对某项任务太具体了

完整错误消息:

回溯(最近一次调用上次):文件 “/…../…/…/Batch_MFCC_Data.py”, 第68行,在 X=np.数组(MFCCs)值错误:无法将输入数组从形状(20590)广播到形状(20)

代码示例:

all_wav_paths = glob.glob('directory_of_wav_files/**/*.wav', recursive=True)
np.random.shuffle(all_wav_paths)

MFCCs = [] #array to hold all MFCC's
labels = [] #array to hold all labels

for i, wav_path in enumerate(all_wav_paths):

    individual_MFCC = MFCC_from_wav(wav_path)
    #MFCC_from_wav() -> returns the MFCC coefficients 

    label = get_class(wav_path)
    #get_class() -> returns the label of the wav file either 0 or 1

    #add features and label to the array
    MFCCs.append(individual_MFCC)
    labels.append(label)

#Must convert the training data to a Numpy Array for 
#train_test_split and saving to local drive

X = np.array(MFCCs) #THIS LINE CRASHES WITH ABOVE ERROR

# binary encode labels
onehot_encoder = OneHotEncoder(sparse=False)
Y = onehot_encoder.fit_transform(labels)

#create train/test data
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(MFCCs, Y, test_size=0.25, random_state=0)

#saving data to local drive
np.save("LABEL_SAVE_PATH", Y)
np.save("TRAINING_DATA_SAVE_PATH", X)
这里是MFCC数组中MFCC的形状的快照(来自.wav文件)

MFCCs阵列包含以下形状:

...More above...
(20, 423) #shape of returned MFCC from one of the .wav files
(20, 457)
(20, 1757)
(20, 345)
(20, 835)
(20, 345)
(20, 687)
(20, 774)
(20, 597)
(20, 719)
(20, 1195)
(20, 433)
(20, 728)
(20, 939)
(20, 345)
(20, 1112)
(20, 345)
(20, 591)
(20, 936)
(20, 1161)
....More below....
正如您所看到的,MFCC阵列中的MFCC并不都具有相同的形状,这是因为录制的时间长度不尽相同。这就是我无法将数组转换为numpy数组的原因吗?如果这是一个问题,我如何解决这个问题,使整个MFCC阵列具有相同的形状

任何完成此任务的代码片段和建议都将不胜感激


谢谢

使用以下逻辑将阵列的采样减少到
min\u形状
,即将较大的阵列减少到
min\u形状

min_shape = (20, 345)
MFCCs = [arr1, arr2, arr3, ...]    

for idx, arr in enumerate(MFCCs):
    MFCCs[idx] = arr[:, :min_shape[1]]

batch_arr = np.array(MFCCs)
然后您可以将这些数组堆叠在批处理数组中,如下面的示例所示:

In [33]: a1 = np.random.randn(2, 3)    
In [34]: a2 = np.random.randn(2, 5)    
In [35]: a3 = np.random.randn(2, 10)

In [36]: MFCCs = [a1, a2, a3]

In [37]: min_shape = (2, 2)

In [38]: for idx, arr in enumerate(MFCCs):
    ...:     MFCCs[idx] = arr[:, :min_shape[1]]
    ...:     

In [42]: batch_arr = np.array(MFCCs)

In [43]: batch_arr.shape
Out[43]: (3, 2, 2)

现在,对于第二种策略,要将较小的数组向上采样为
max\u shape
,请遵循类似的逻辑,但根据您的喜好,用零或
nan
值填充缺少的值


然后,您可以再次将这些数组堆叠为shape
(num_array,dim1,dim2)的批处理数组
;因此,对于您的情况,形状应该是
(num\u wav\u files,20,max\u column

那么,这个
MFCCs=[]
只包含形状还是实际数组本身?我的意思是什么是
类型(单个_-MFCC)
是数组还是形状信息?很抱歉混淆类型(单个_-MFCC)=是的,问题是由于您提到的不同数组之间的形状不匹配。我有两个问题:1)第一维度是否总是有形状
(20,…)
,2)你知道数组的最大大小吗?@SreehariR让我们稍微了解一下。您的
MFCC
列表(不是数组!)包含名为
individual_MFCC
的元素,它们是不同形状的
np.array
s。对的假设你的两个
个体_MFCC
np.数组
s的形状
(20424)
(20457)
。当你把它们放在
MFCC
列表中并转换成
np.array
时,你希望结果是什么形状?@kmario23好的,所以对于(1)是的,第一维度总是有形状(20,…),和2)我不知道数组的最大大小。