np.newaxis用法

插入新维度

import numpy as np
a=np.array([1,2,3,4,5])
print ('a:',a)
print('a.shape',a.shape)
a1=a[:,np.newaxis]
print ('a1:\n',a1)
print('a1.shape',a1.shape)
a2=a[np.newaxis,:]
print ('a2:\n',a1)
print('a2.shape',a2.shape)

结果

a: [1 2 3 4 5]
a.shape (5,)
a1:
 [[1]       
 [2]        
 [3]        
 [4]        
 [5]]       
a1.shape (5, 1)
a2:
 [[1]
 [2]
 [3]
 [4]
 [5]]
a2.shape (1, 5)
posted @ 2022-01-28 16:47  wuyuan2011woaini  阅读(64)  评论(0编辑  收藏  举报