您正在寻找的功能是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)