reshape函數:改變數組的維數(注意不是shape大小)
1 >>> e= np.arange(10) 2 3 >>> e 4 5 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1 >>> e.reshape(1,1,10) 2 3 array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]]) 4 5 >>> e.reshape(1,10,1) 6 7 array([[[0], 8 9 [1], 10 11 [2], 12 13 [3], 14 15 [4], 16 17 [5], 18 19 [6], 20 21 [7], 22 23 [8], 24 25 [9]]])
squeeze 函數:從數組的形狀中刪除單維度條目,即把shape中為1的維度去掉
用法:numpy.squeeze(a,axis = None)
1)a表示輸入的數組; 2)axis用於指定需要刪除的維度,但是指定的維度必須為單維度,否則將會報錯; 3)axis的取值可為None 或 int 或 tuple of ints, 可選。若axis為空,則刪除所有單維度的條目; 4)返回值:數組 5) 不會修改原數組;
>>> a = e.reshape(1,1,10) >>> a array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]]) >>> np.squeeze(a) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])