【Python】Numpy实现增加数组维度

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Laox1ao/article/details/82185040

方法一:用None作为索引

a = np.array([1,2,3]) # a.shape = (3,)
a = a[None,:] # a.shape =(1,3)

方法二:用np.newaxis

a = np.array([1,2,3]) # a.shape = (3,)
a = a[np.newaxis,:] # a.shape =(1,3)

方法三:用reshape

a = np.array([1,2,3]) # a.shape = (3,)
a = np.reshape(a,((1,)+a.shape)) # a.shape = (1,3)
# 或者 a.reshape(a,((1,)+a.shape))

猜你喜欢

转载自blog.csdn.net/Laox1ao/article/details/82185040