我正在尝试使用从 wav 文件中提取的 MFCC 从 .wav 文件中提取特征。
我在将 MFCC 列表转换为 numpy 数组时遇到问题。据我了解,该错误是由于 MFCC 列表中的 MFCC 尺寸相同,但我不确定解决此问题的最佳方法。
运行下面的代码时:
X = []
y = []
_min, _max = float('inf'), -float('inf')
for _ in tqdm(range(len(new_dataset))):
rand_class = np.random.choice(class_distribution.index, p=prob_distribution)
file = np.random.choice(new_dataset[new_dataset.word == rand_class].index)
label = new_dataset.at[file, 'word']
X_sample = new_dataset.at[file,'coeff']
_min = min(np.amin(X_sample), _min)
_max = max(np.amin(X_sample), _max)
X.append(X_sample if config.mode == 'conv' else X_sample.T)
y.append(classes.index(label))
X, y = np.array(X), np.array(y) #crashes here
我收到以下错误消息:
Traceback (most recent call last):
File "<ipython-input-150-8689abab6bcf>", line 14, in <module>
X, y = np.array(X), np.array(y)
ValueError: could not broadcast input array from shape (13,97) into shape (13)
在循环中添加 print(X_sample.shape) 会产生:
:
(13, 74)
(13, 83)
(13, 99)
(13, 99)
(13, 99)
(13, 55)
(13, 92)
(13, 99)
(13, 99)
(13, 78)
...
经检查,MFCC 的形状似乎并不相同,因为录音的长度也不相同。
我想知道我对这就是问题的假设是否正确,如果是,我该如何解决这个问题?如果这不是问题,那么我同样想知道解决方案。
提前致谢!
最佳答案
这会重现您的错误:
In [186]: np.array([np.zeros((4,5)),np.ones((4,6))])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-186-e369332b8a05> in <module>
----> 1 np.array([np.zeros((4,5)),np.ones((4,6))])
ValueError: could not broadcast input array from shape (4,5) into shape (4)
如果数组都具有相同的形状:
In [187]: np.array([np.zeros((4,6)),np.ones((4,6))]).shape
Out[187]: (2, 4, 6)
如果第一维有一个或多个不同,我们会得到一个对象数据类型数组,本质上是列表周围的数组包装器:
In [188]: np.array([np.zeros((4,6)),np.ones((3,6))]).shape
Out[188]: (2,)
不要尝试组合形状(可能)不同的数组,除非您了解您需要什么以及您打算如何处理结果。第一种情况可以创建一个对象数据类型数组,但构造过程有点迂回。除非你真的有这样的数组,否则我不会讨论这个。
关于python - 如何将数组列表转换为单个多维 numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59097323/