numpy 學習彙總28
阿新 • • 發佈:2019-02-14
陣列重塑 2018/11/25 =================================================================== 1.a.reshape(shape, order='C') # 無需複製資料,陣列就能從一個形狀轉換為另一個形狀 # shape其中一維可以是-1 ,該維度的大小由資料本身推斷而來 a=np.arange(6,dtype=int).reshape(2,3) #array([[0, 1, 2],[3, 4, 5]]) np.reshape(np.arange(6,dtype=int),(2,3))#array([[0, 1, 2], [3, 4, 5]]) np.reshape(a, (3,-1)) # -1預設值自動被指定為2:shape=3*2 # array([[1, 2],[3, 4],[5, 6]]) np.reshape(a, (2,-1)) # -1預設值自動被指定為3:shape=2*3 # array([[1, 2, 3],[4, 5, 6]]) ==================================================================== 2.resize修改原陣列形狀 np.resize(a,new_shape)拷貝的副本,原陣列不改變 #返回具有指定形狀的新陣列。重複的a副本,必要時指定數量。無refcheck引數 a.resize(new_shape,refcheck = True)就地操作,改變原陣列 #新陣列用a填充,不足的填充0,不同與上面函式。 # refcheck=True檢查引用計數,預設禁止陣列放大或縮小;False可放大縮小 # 只有連續的陣列(記憶體中連續的資料元素)可以調整大小 ----------------------------------------------- a = np.array([[0, 1], [2, 3]], order='C')# array([[0, 1], [2, 3]]) a.resize((4,1)) # array([[0], [1], [2],[3]]) a = np.array([[0, 1], [2, 3]], order='C') # a.resize((2, 1)) # 錯誤-收縮陣列 # array([[0],[1]]) a.resize((2, 1),refcheck = False) # 收縮陣列被平展(儲存記憶體中順序)調整大小和重新定形 a = np.array([[0, 1], [2, 3]], order='C') # a.resize(2, 3) # 錯誤-放大陣列 a.resize((2, 3),refcheck = False) # 放大陣列:缺少填充零 # array([[0, 1, 2],[3, 0, 0]]) ------------------------------------------------- a = np.array([[0, 1], [2, 3]], order='C') np.resize(a,(4,1)) # array([[0], [1], [2],[3]]) a = np.array([[0, 1], [2, 3]], order='C') np.resize(a,(2, 1)) # 收縮陣列 # array([[0],[1]]) a = np.array([[0, 1], [2, 3]], order='C') np.resize(a,(3, 3)) # 放大陣列 # array([[0, 1, 2], [3, 0, 1], [2, 3, 0]]) a # array([[0, 1],[2, 3]]) ===================================================================== 3.轉置np.transpose(a, axes=None)#是重塑一種特殊形式,返回據資料的檢視 無引數:反轉軸的順序;對於a.shape=(2, 3)的陣列必須是無引數 (i,j)引數類似軸i,j互換 例項1:# 對於一維陣列,這不起作用; a=np.arange(6).reshape(2,3)# array([[0, 1, 2],[3, 4, 5]]) a.T # array([[0, 3], [1, 4], [2, 5]]) a.transpose() # array([[0, 3],[1, 4],[2, 5]]) # a.transpose(0)# 錯誤 # a.transpose(1)# 錯誤 例項2:#對於高維陣列, transpose需要一組軸編號: a array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]]]) a.transpose(0,1,2)# a0.shape# (2, 2, 3) 原形狀(2, 2, 3) array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]]]) a.transpose(1,0,2)# a1.shape# (2, 2, 3)原形狀(2, 2, 3) array([[[ 0, 1, 2], [ 6, 7, 8]], [[ 3, 4, 5], [ 9, 10, 11]]]) a.transpose(0,2,1)# a2.shape# (2, 3, 2)原形狀(2, 2, 3) array([[[ 0, 3], [ 1, 4], [ 2, 5]], [[ 6, 9], [ 7, 10], [ 8, 11]]]) a.transpose(2,0,1)# a3.shape#(3, 2, 2)原形狀(2, 2, 3) array([[[ 0, 3], [ 6, 9]], [[ 1, 4], [ 7, 10]], [[ 2, 5], [ 8, 11]]]) a.transpose(1,2,0)# a4.shape#(2, 3, 2)原形狀(2, 2, 3) array([[[ 0, 6], [ 1, 7], [ 2, 8]], [[ 3, 9], [ 4, 10], [ 5, 11]]]) a.transpose(2,1,0)# a5.shape#(3, 2, 2)原形狀(2, 2, 3) array([[[ 0, 6], [ 3, 9]], [[ 1, 7], [ 4, 10]], [[ 2, 8], [ 5, 11]]]) a.T # array([[[ 0, 6], [ 3, 9]],[[ 1, 7],[ 4, 10]], [[ 2, 8],[ 5, 11]]]) ================================================================== 備註:transpose的實現原理 基本原理:相同位置不變換,其他位置對換以3維陣列為例。 本程式模擬實現轉置,其他4條能夠模擬實現,2條有問題歡迎大家修正。 import numpy as np a=np.arange(12).reshape(2,2,3) def testview(a0=a): for i in range(a0.shape[0]): for m in range(a0.shape[1]): for n in range(a0.shape[2]): print('ss=',a0[i, m, n], end=",") print('ss=','') def computer_i_m_n(zi0=0,zm0=1,zn0=2,a0=a): i, m, n = a0.shape i1, m1, n1 = 0, 0, 0 if zi0 == 0: if zm0 == 1: i1, m1, n1 = i, m, n else: i1, m1, n1 = i, n, m elif zi0 == 1: if zm0 == 0: i1, m1, n1 = m, i, n else: i1, m1, n1 = m, n, i elif zi0 == 2: if zm0 == 0: i1, m1, n1 = n, i, m else: i1, m1, n1 = n, m, i return i1,m1,n1 def swap_old_new_arr(i1,m1,n1,zi0=0,zm0=1,zn0=2): if zi0 == 0: if zm0 == 1: return i1, m1, n1 else: return i1, n1, m1 elif zi0 == 1: if zm0 == 0: return m1, i1, n1 else: return m1, n1, i1 elif zi0 == 2: if zm0 == 0: return n1, i1, m1#3,2,2 else: return n1, m1, i1 def test(zi0=0,zm0=1,zn0=2,a0=a): i1,m1,n1=computer_i_m_n(zi0=zi0,zm0=zm0,zn0=zn0,a0=a0) i3,m3,n3=0,0,0 new_a=np.arange(i1*m1*n1).reshape(i1,m1,n1) # i2,m2,n2=0,0,0 for i2 in range(i1): for m2 in range(m1): for n2 in range(n1): i3,m3,n3=swap_old_new_arr(i1=i2, m1=m2, n1=n2, zi0=zi0, zm0=zm0, zn0=zn0) new_a[i2,m2,n2]=a0[i3,m3,n3] return new_a print('ss=',test(0,1,2)) print('ss=',test(0,2,1)) print('ss=',test(1,0,2)) # print('ss=',test(1,2,0))問題??報錯,請高手修改 # print('ss=',test(2,0,1))問題??報錯,請高手修改 print('ss=',test(2,1,0))