[英]How do I convert a list of arrays to a single multidimensional numpy array?
我正在尝试使用从 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)
如果第一维有一个或多个不同,我们会得到一个对象 dtype 数组,本质上是列表周围的数组包装器:
In [188]: np.array([np.zeros((4,6)),np.ones((3,6))]).shape
Out[188]: (2,)
不要尝试组合形状(可能)不同的数组,除非您了解您需要什么,以及您打算如何处理结果。 第一种情况可以创建一个对象 dtype 数组,但构建过程有点迂回。 除非你真的有这样一个数组,否则我不会深入探讨。
您将需要截断或填充时间维度以使其成为相同大小的数组。 如果长度变化很大,您可以使用固定长度的分析窗口(比如超过 1 秒或 10 秒的 MFCC),并且每个输入音频剪辑有多个这样的窗口。 此处显示了此原理, 如何使用上下文窗口对整个日志 Mel 频谱图进行分段(确保所有音频的分段数相同)?
这重现了您的错误:
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)
如果第一维有一个或多个不同,我们会得到一个对象 dtype 数组,本质上是列表周围的数组包装器:
In [188]: np.array([np.zeros((4,6)),np.ones((3,6))]).shape
Out[188]: (2,)
如果我们这样做,第一种情况就有效:
In [189]: arr = np.zeros(2,object)
In [190]: arr[:] = [np.zeros((4,5)),np.ones((4,6))]
In [191]: arr
Out[191]:
array([array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]]),
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])], dtype=object)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.