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

我正在从音频剪辑中提取特征。在为 1 个剪辑执行此操作时,20x2将获得一个维度矩阵。我有1000很多这样的剪辑。我想将所有数据存储在 1 个 numpy 维度数组中20x2x1000。请提出相同的方法。


慕姐4208626
浏览 208回答 2
2回答

POPMUISE

您正在寻找的功能是np.stack. 它用于沿新轴堆叠多个 NumPy 数组。import numpy as np# Generate 1000 featuresoriginal_features = [np.random.rand(20, 2) for i in range(1000)]# Stack them into one arraystacked_features = np.stack(original_features, axis=2)assert stacked_features.shape == (20, 2, 1000)

一只萌萌小番薯

有一个方便的函数,那就是numpy.dstack。下面是数组深度堆叠的代码片段:# whatever the number of arrays that you haveIn [4]: tuple_of_arrs = tuple(np.random.randn(20, 2) for _ in range(10))# stack each of the arrays along third axisIn [7]: depth_stacked = np.dstack(tuple_of_arrs)In [8]: depth_stacked.shapeOut[8]: (20, 2, 10)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python