numpy的基本API(二)——維數操作


numpy的基本維數操作API

  iwehdio的博客園:https://www.cnblogs.com/iwehdio/

 

1、np.copyto(dst, src)  copyto方法將數組src復制到dst中。如果兩個數組的形狀完全相同,則復制后兩數組中的數據相同。如果src的維數n比dst的維數低,且與dst中的最后幾個維度shape[:-n]相同,就將dst中每個形狀與src相同的都復制為src。

>>> a = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])  # (2,2,3)
>>> b = np.array([[7,8,9],[10,11,12]]) # (2,3)
>>> c = np.array([10])                      #(1,)
>>> np.copyto(a,b)
>>> a
array([[[ 7,  8,  9],
        [10, 11, 12]],
       [[ 7,  8,  9],
        [10, 11, 12]]])
>>> np.copyto(a,c)
>>> a
array([[[10, 10, 10],
        [10, 10, 10]],
       [[10, 10, 10],
        [10, 10, 10]]])

 

2、np.reshape(a, shape)  reshape方法將數組a在不改變數據的情況下改變形狀。a表示目標數組,shape表示所要改變為的形狀,可以為元組如(2,3),也可為列表如[2,3]。shape中某一維度的值為-1表示此維度的數據長度為自適應。但是shape改變的新數組大小必須與原數組相同。返回值為numpy數組。同時,可以使用a.reshape(shape),此時shape除了可為元組和列表外,也可以之間為用逗號間隔的數字2,3。

>>> a = np.arange(6)
>>> a
array([0, 1, 2, 3, 4, 5])
>>> np.reshape(a,(2, 3))

array([[0, 1, 2],
       [3, 4, 5]])
>>> np.reshape(a,(3,-1))
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> a.reshape(6,1)
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5]])

 

3、np.ravel(a)  raevl方法將數組a展平為一維數組,shape為(n, ),a為輸入數組。同樣的,也可以使用a.ravel(),注意不要丟掉括號。

>>> np.ravel(a) array([0, 1, 2, 3, 4, 5]) >>> np.array([[4,5,6],[1,2,3]]).ravel() array([4, 5, 6, 1, 2, 3])

 

4、a.flat[n]  flat方法將數組a按一維數組索引,如數據a的shape為(M, N),則索引n可取[0, M*N-1]。也可以取一個列表作為索引如a.flat[[q, w]],返回索引為q和w的數組。同時,還有a.flat=x可以把數組a中的所有值改為x,也可a.flat[[q,w]=x]對列表中的特定之進行改變。

>>> a = np.arange(1, 7).reshape(2, 3) >>> a array([[1, 2, 3], [4, 5, 6]]) >>> a.flat[5] 6
>>> a.flat[[2,4]] array([3, 5]) >>> a.flat = 3
>>> a array([[3, 3, 3], [3, 3, 3]]) >>> a.flat[[1,5]]=5
>>> a array([[1, 5, 3], [4, 5, 5]])

 

5、np.moveaxis(a, src, dst)  moveaxis方法將數組的軸進行移動。src表示所要移動的軸的索引,dst表示所要移動到的位置,都可以為列表對象進行對應移動。

需要注意的是,移動軸的含義是,把原來src索引位置的軸改為dst索引位置,而不是進行索引位置的交換。如shape為(2,3,4)的數組a經過b=np.moveaxis(a, 0, -1)后,b的shape為(3,4,2),也就是說其他軸按原來的順序向前填充了,等價於np.moveaxis(x, [0, 1, 2], [-1, -3, -2])。此外,在索引更改后的數組與原數組對應的位置進行切片,切片的shape也有可能會與原數組不同而為其轉置的shape。

>>> x = np.arange(0, 24).reshape(2, 3, 4) >>> x array([[[ 0, 1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) >>> y=np.moveaxis(x, 0, -1) >>> y array([[[ 0, 12], [ 1, 13], [ 2, 14], [ 3, 15]], [[ 4, 16], [ 5, 17], [ 6, 18], [ 7, 19]], [[ 8, 20], [ 9, 21], [10, 22], [11, 23]]]) >>> y.shape (3, 4, 2)
>>> np.moveaxis(x, [0, 1, 2], [-1, -3, -2]).shape
(3, 4, 2)
>>> y[0,:,:] array([[ 0, 12], [ 1, 13], [ 2, 14], [ 3, 15]]) >>> x[:,0,:] array([[ 0, 1, 2, 3], [12, 13, 14, 15]])

可以看到,x和y都在shape為3的軸求該軸索引為0時的切片,得到的數組互為轉置。但是對所進行moveaxis操作的shape為2的軸,按其軸索引進行切片,得到的數組與原數組相同。

moveaxis方法當只對一個軸進行操作時,也可由rollaxis方法實現,但是更推薦前者。

 

6、np.swapaxes(a, axis1, axis2)  swapaxes方法對數組a中的兩個軸的位置進行互換,其他軸的位置不變。

>>> x = np.arange(0, 24).reshape(2, 3, 4) >>> x.shape (2, 3, 4) >>> y = np.swapaxes(x,0,1) >>> y array([[[ 0, 1,  2,  3], [12, 13, 14, 15]], [[ 4,  5,  6,  7], [16, 17, 18, 19]], [[ 8,  9, 10, 11], [20, 21, 22, 23]]]) >>> y.shape (3, 2, 4)

 

7、np.transpose(a, axex=None)  transpose方法排列數組a的形狀。當axex為None時,返回的是數組a的轉置。當axex為數組a軸的索引值的排列,如shape為[1,2,3,4]的數組a的axex為元組(2,1,3,0)或列表[2,1,3,0]時,表示數組a的shape變為了(第2軸,第1軸,第3軸,第0軸)的長度。

>>> a = np.ones((1, 2, 3, 4)) >>> np.transpose(a).shape (4, 3, 2, 1) >>> np.transpose(a,(2,1,3,0)).shape (3, 2, 4, 1) >>> a.T.shape (4, 3, 2, 1)

當axex為None時,np.transpose(a)與a.T是等價的。

 

8、np.atleast_1d(array1,array2,...)  atleast_1d方法將輸入視為至少一維的數組,將輸入數組放置到一個列表里。與直接創建list不同的是,把輸入的每個對象都轉換為了數組對象。atleast_2d方法將輸入視為至少二維的數組,atleast_3d方法將輸入視為至少三維的數組。其實就是把輸入數組中維度高於一維/二維/三維的數組原樣輸出,低於一維/二維/三維的數組升至一維/二維/三維輸出,把結果放入一個列表中。

>>> x = np.arange(0, 24).reshape(2, 3, 4) >>> np.atleast_1d(x,[2,3],1) [array([[[ 0, 1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]), array([2, 3]), array([1])] >>> [x,[2,3],1] [array([[[ 0, 1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]), [2, 3], 1]
>>> np.atleast_2d(x,[2,3],1)
[array([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]]), array([[2, 3]]), array([[1]])]
>>> np.atleast_3d(x,[2,3],1)
[array([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]]),
     array([[[2],[3]]]), array([[[1]]])]

atleast_1d中,[2, 3]被轉換為shape為(2, );atleast_1d中,[2, 3]被轉換為shape為(1, 2);atleast_1d中,[2, 3]被轉換為shape為(1, 2, 1)。先把整個數組當作一個元素升一維,再把數組中的每個數字當作一個元素升一維。

 

9、np.broadcast(a, b)  broadcast方法創建並返回一個數組a和b的廣播對象y。y只能通過迭代器獲取內容,並且有.shape、.index等方法,表示廣播結果的形狀和索引。廣播是指,如果數組a和b的形狀不相同則對不相同的軸的值進行排列組合。

>>> a = np.array([[1],[2]]) >>> b = np.array([[4, 5, 6,7]]) >>> a.shape, b.shape ((2, 1), (1, 4)) >>> y = np.broadcast(a, b) >>> y <numpy.broadcast at 0x213aa462050>
>>> y.shape (2, 4) >>>  for (u,v) in y: print(u, v, '\t', b.index) 1 4      1
1 5      2
1 6      3
1 7      4
2 4      5
2 5      6
2 6      7
2 7      8

 

10、np.broadcast_to(a, shape)  broadcast_to方法把數組a廣播並返回為形狀為shape的數組。shape的形式應為new_shape+a.shape,即如需要把a廣播為(m, n)次,a.shape=(q, w),則shape應為(m,n,q,w)。

>>> a = np.array([[1,2],[3,4]]) >>> a.shape (2, 2) >>> b = np.broadcast_to(a, (3,3,2,2)) array([[[[1, 2], [3, 4]], [[1, 2], [3, 4]], [[1, 2], [3, 4]]], [[[1, 2], [3, 4]], [[1, 2], [3, 4]], [[1, 2], [3, 4]]], [[[1, 2], [3, 4]], [[1, 2], [3, 4]], [[1, 2], [3, 4]]]]) >>> b.shape (6, 4, 2, 2)

 

 

11、np.broadcast_array(a, b)  broadcast_array方法獲取廣播的數組,返回的是包含廣播數組的列表。

>>> a = np.array([[1,2,3]]) >>> b = np.array([[4],[5]]) >>> np.broadcast_arrays(a, b) [array([[1, 2, 3], [1, 2, 3]]), array([[4, 4, 4], [5, 5, 5]])]    

 

12、np.expand_dims(a, axis)  expand_dims方法返回擴展輸入數組a的形狀的數組,在axis軸位置添加新軸。

>>> x = np.array([[1,2],[3,4]]) >>> x.shape (2, 2) >>> y = np.expand_dims(x, 0) >>> y array([[[1, 2], [3, 4]]]) >>> y.shape (1, 2, 2) >>> z = np.expand_dims(x, 1) >>> z array([[[1, 2]], [[3, 4]]]) >>> z.shape (2, 1, 2) >>> w = np.expand_dims(x, 2) >>> w array([[[1], [2]], [[3], [4]]]) >>> w.shape (2, 2, 1)

 

13、在reshape等API中,都有order{'C', 'F', 'A'}參數,默認為'C'。其含義主要影響數組的索引速度,'C'表示使用類似C語言的索引順序讀取/寫入元素,最后一個軸索引更改最快,回到第一個軸索引更改最快。 'F'表示使用類似Fortran語言的索引順序讀取/寫入元素,第一個索引更改最快,最后一個索引更改最慢。 'C'和'F'選項不考慮基礎數組的內存布局,僅指索引的順序。 'A'表示如果數組在內存中是連續的,則以類似於Fortran語言的索引順序讀取/寫入元素,否則為類似於C語言的順序。

 

參考:numpy中文文檔:https://www.numpy.org.cn/reference/

   numpy英文文檔:https://numpy.org/doc/1.17/reference/index.html

 

iwehdio的博客園:https://www.cnblogs.com/iwehdio/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM