3
什么我想现在要做的是:是否有更多的Pythonic /优雅的方式来扩大Numpy阵列的尺寸?
x = x[:, None, None, None, None, None, None, None, None, None]
基本上,我想9种尺寸扩大我NumPy的阵列。或者预先不知道N的N个维数!
有没有更好的方法来做到这一点?
什么我想现在要做的是:是否有更多的Pythonic /优雅的方式来扩大Numpy阵列的尺寸?
x = x[:, None, None, None, None, None, None, None, None, None]
基本上,我想9种尺寸扩大我NumPy的阵列。或者预先不知道N的N个维数!
有没有更好的方法来做到这一点?
一种替代方法可以是与reshaping
-
x.reshape((-1,) + (1,)*N) # N is no. of dims to be appended
所以,基本上对于对应于独居尺寸None's
,我们使用长度1
沿着那些DIMS的形状。对于第一个轴,我们使用的形状为-1
至将所有元素都插入其中。
采样运行 -
In [119]: x = np.array([2,5,6,4])
In [120]: x.reshape((-1,) + (1,)*9).shape
Out[120]: (4, 1, 1, 1, 1, 1, 1, 1, 1, 1)