如何在3D numpy数组中堆叠多个2D numpy数组

时间:2019-03-17 16:39:08

标签: python numpy audio multidimensional-array numpy-ndarray

我正在从音频剪辑中提取功能。这样做,对于1个剪辑,将获得20x2维的矩阵。我大约有1000个这样的剪辑。我想将所有数据存储在尺寸为20x2x1000的1 numpy数组中。请提出同样的方法。

2 个答案:

答案 0 :(得分:0)

您要寻找的功能是np.stack。它用于沿新轴堆叠多个NumPy数组。

import numpy as np

# Generate 1000 features
original_features = [np.random.rand(20, 2) for i in range(1000)]

# Stack them into one array
stacked_features = np.stack(original_features, axis=2)
assert stacked_features.shape == (20, 2, 1000)

答案 1 :(得分:0)

There is a convenient function for this and that is numpy.dstack. Below is a snippet of code for depth stacking of arrays:

# whatever the number of arrays that you have
In [4]: tuple_of_arrs = tuple(np.random.randn(20, 2) for _ in range(10))

# stack each of the arrays along third axis
In [7]: depth_stacked = np.dstack(tuple_of_arrs)

In [8]: depth_stacked.shape
Out[8]: (20, 2, 10)
相关问题