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)
上一篇:matplotlib 学习笔记


下一篇:Python-txt文件读写