Numpy 數組操作

Numpy 中包含了一些函數用於處理數組,大概可分為以下幾類:


修改數組形狀

函數 描述
reshape 不改變數據的條件下修改形狀
flat 數組元素迭代器
flatten 返回一份數組拷貝,對拷貝所做的修改不會影響原始數組
ravel 返回展開數組

numpy.reshape

numpy.reshape 函數可以在不改變數據的條件下修改形狀,格式如下: numpy.reshape(arr, newshape, order='C')
  • arr:要修改形狀的數組
  • newshape:整數或者整數數組,新的形狀應當相容原有形狀
  • order:'C' -- 按行,'F' -- 按列,'A' -- 原順序,'k' -- 元素在內存中的出現順序。

實例

import numpy as np a = np.arange(8) print ('原始數組:') print (a) print ('\n') b = a.reshape(4,2) print ('修改後的數組:') print (b)

輸出結果如下:

原始數組:

[0 1 2 3 4 5 6 7]

修改後的數組:

[[0 1]
 [2 3]
 [4 5]
 [6 7]]

numpy.ndarray.flat

numpy.ndarray.flat 是一個數組元素迭代器,實例如下:

實例

import numpy as np a = np.arange(9).reshape(3,3) print ('原始數組:') for row in a: print (row) #對數組中每個元素都進行處理,可以使用flat屬性,該屬性是一個數組元素迭代器: print ('迭代後的數組:') for element in a.flat: print (element)

輸出結果如下:

原始數組:

[0 1 2]
[3 4 5]
[6 7 8]
迭代後的數組:

0
1
2
3
4
5
6
7
8

numpy.ndarray.flatten

numpy.ndarray.flatten 返回一份數組拷貝,對拷貝所做的修改不會影響原始數組,格式如下:

ndarray.flatten(order='C')

參數說明:

  • order:'C' -- 按行,'F' -- 按列,'A' -- 原順序,'K' -- 元素在內存中的出現順序。

實例

import numpy as np a = np.arange(8).reshape(2,4) print ('原數組:') print (a) print ('\n') # 默認按行 print ('展開的數組:') print (a.flatten()) print ('\n') print ('以 F 風格順序展開的數組:') print (a.flatten(order = 'F'))

輸出結果如下:

原數組:
[[0 1 2 3]
 [4 5 6 7]]


展開的數組:
[0 1 2 3 4 5 6 7]


以 F 風格順序展開的數組:
[0 4 1 5 2 6 3 7]

numpy.ravel

numpy.ravel() 展平的數組元素,順序通常是"C風格",返回的是數組視圖(view,有點類似 C/C++引用reference的意味),修改會影響原始數組。

該函數接收兩個參數:

numpy.ravel(a, order='C')

參數說明:

  • order:'C' -- 按行,'F' -- 按列,'A' -- 原順序,'K' -- 元素在內存中的出現順序。

實例

import numpy as np a = np.arange(8).reshape(2,4) print ('原數組:') print (a) print ('\n') print ('調用 ravel 函數之後:') print (a.ravel()) print ('\n') print ('以 F 風格順序調用 ravel 函數之後:') print (a.ravel(order = 'F'))

輸出結果如下:

原數組:
[[0 1 2 3]
 [4 5 6 7]]


調用 ravel 函數之後:

[0 1 2 3 4 5 6 7]


以 F 風格順序調用 ravel 函數之後:

[0 4 1 5 2 6 3 7]

翻轉數組

函數 描述
transpose 對換數組的維度
ndarray.T self.transpose() 相同
rollaxis 向後滾動指定的軸
swapaxes 對換數組的兩個軸

numpy.transpose

numpy.transpose 函數用於對換數組的維度,格式如下:

numpy.transpose(arr, axes)

參數說明:

  • arr:要操作的數組
  • axes:整數列表,對應維度,通常所有維度都會對換。

實例

import numpy as np a = np.arange(12).reshape(3,4) print ('原數組:') print (a ) print ('\n') print ('對換數組:') print (np.transpose(a))

輸出結果如下:

原數組:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]


對換數組:

[[ 0  4  8]
 [ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]]

numpy.ndarray.T 類似 numpy.transpose:

實例

import numpy as np a = np.arange(12).reshape(3,4) print ('原數組:') print (a) print ('\n') print ('轉置數組:') print (a.T)

輸出結果如下:

原數組:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]


轉置數組:

[[ 0  4  8]
 [ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]]

numpy.rollaxis

numpy.rollaxis 函數向後滾動特定的軸到一個特定位置,格式如下:

numpy.rollaxis(arr, axis, start)

參數說明:

  • arr:數組
  • axis:要向後滾動的軸,其他軸的相對位置不會改變
  • start:默認為零,表示完整的滾動。會滾動到特定位置。

實例

import numpy as np # 創建了三維的 ndarray a = np.arange(8).reshape(2,2,2) print ('原數組:') print (a) print ('\n') # 將軸 2 滾動到軸 0(寬度到深度) print ('調用 rollaxis 函數:') print (np.rollaxis(a,2)) # 將軸 0 滾動到軸 1:(寬度到高度) print ('\n') print ('調用 rollaxis 函數:') print (np.rollaxis(a,2,1))

輸出結果如下:

原數組:
[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]


調用 rollaxis 函數:

[[[0 2]
  [4 6]]

 [[1 3]
  [5 7]]]


調用 rollaxis 函數:

[[[0 2]
  [1 3]]

 [[4 6]
  [5 7]]]

numpy.swapaxes

numpy.swapaxes 函數用於交換數組的兩個軸,格式如下:

numpy.swapaxes(arr, axis1, axis2)
  • arr:輸入的數組
  • axis1:對應第一個軸的整數
  • axis2:對應第二個軸的整數

實例

import numpy as np # 創建了三維的 ndarray a = np.arange(8).reshape(2,2,2) print ('原數組:') print (a) print ('\n') # 現在交換軸 0(深度方向)到軸 2(寬度方向) print ('調用 swapaxes 函數後的數組:') print (np.swapaxes(a, 2, 0))

輸出結果如下:

原數組:
[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]


調用 swapaxes 函數後的數組:

[[[0 4]
  [2 6]]

 [[1 5]
  [3 7]]]

修改數組維度

維度 描述
broadcast 產生模仿廣播的對象
broadcast_to 將數組廣播到新形狀
expand_dims 擴展數組的形狀
squeeze 從數組的形狀中刪除一維條目

numpy.broadcast

numpy.broadcast 用於模仿廣播的對象,它返回一個對象,該對象封裝了將一個數組廣播到另一個數組的結果。

該函數使用兩個數組作為輸入參數,如下實例:

實例

import numpy as np x = np.array([[1], [2], [3]]) y = np.array([4, 5, 6]) # 對 y 廣播 x b = np.broadcast(x,y) # 它擁有 iterator 屬性,基於自身組件的迭代器元組 print ('對 y 廣播 x:') r,c = b.iters # Python3.x 為 next(context) ,Python2.x 為 context.next() print (next(r), next(c)) print (next(r), next(c)) print ('\n') # shape 屬性返回廣播對象的形狀 print ('廣播對象的形狀:') print (b.shape) print ('\n') # 手動使用 broadcast 將 x 與 y 相加 b = np.broadcast(x,y) c = np.empty(b.shape) print ('手動使用 broadcast 將 x 與 y 相加:') print (c.shape) print ('\n') c.flat = [u + v for (u,v) in b] print ('調用 flat 函數:') print (c) print ('\n') # 獲得了和 NumPy 內建的廣播支持相同的結果 print ('x 與 y 的和:') print (x + y)

輸出結果為:

對 y 廣播 x:

1 4
1 5


廣播對象的形狀:

(3, 3)


手動使用 broadcast 將 x 與 y 相加:

(3, 3)


調用 flat 函數:

[[5. 6. 7.]
 [6. 7. 8.]
 [7. 8. 9.]]


x 與 y 的和:

[[5 6 7]
 [6 7 8]
 [7 8 9]]

numpy.broadcast_to

numpy.broadcast_to 函數將數組廣播到新形狀。它在原始數組上返回只讀視圖。 它通常不連續。 如果新形狀不符合 NumPy 的廣播規則,該函數可能會拋出ValueError。

numpy.broadcast_to(array, shape, subok)

實例

import numpy as np a = np.arange(4).reshape(1,4) print ('原數組:') print (a) print ('\n') print ('調用 broadcast_to 函數之後:') print (np.broadcast_to(a,(4,4)))

輸出結果為:

原數組:
[[0 1 2 3]]


調用 broadcast_to 函數之後:

[[0 1 2 3]
 [0 1 2 3]
 [0 1 2 3]
 [0 1 2 3]]

numpy.expand_dims

numpy.expand_dims 函數通過在指定位置插入新的軸來擴展數組形狀,函數格式如下:

 numpy.expand_dims(arr, axis)

參數說明:

  • arr:輸入數組
  • axis:新軸插入的位置

實例

import numpy as np x = np.array(([1,2],[3,4])) print ('數組 x:') print (x) print ('\n') y = np.expand_dims(x, axis = 0) print ('數組 y:') print (y) print ('\n') print ('數組 x 和 y 的形狀:') print (x.shape, y.shape) print ('\n') # 在位置 1 插入軸 y = np.expand_dims(x, axis = 1) print ('在位置 1 插入軸之後的數組 y:') print (y) print ('\n') print ('x.ndim 和 y.ndim:') print (x.ndim,y.ndim) print ('\n') print ('x.shape 和 y.shape:') print (x.shape, y.shape)

輸出結果為:

數組 x:

[[1 2]
 [3 4]]


數組 y:

[[[1 2]
  [3 4]]]


數組 x 和 y 的形狀:

(2, 2) (1, 2, 2)


在位置 1 插入軸之後的數組 y:

[[[1 2]]

 [[3 4]]]


x.ndim 和 y.ndim:

2 3


x.shape 和 y.shape:

(2, 2) (2, 1, 2)

numpy.squeeze

numpy.squeeze 函數從給定數組的形狀中刪除一維的條目,函數格式如下:

numpy.squeeze(arr, axis)

參數說明:

  • arr:輸入數組
  • axis:整數或整數元組,用於選擇形狀中一維條目的子集

實例

import numpy as np x = np.arange(9).reshape(1,3,3) print ('數組 x:') print (x) print ('\n') y = np.squeeze(x) print ('數組 y:') print (y) print ('\n') print ('數組 x 和 y 的形狀:') print (x.shape, y.shape)

輸出結果為:

數組 x:

[[[0 1 2]
  [3 4 5]
  [6 7 8]]]


數組 y:

[[0 1 2]
 [3 4 5]
 [6 7 8]]


數組 x 和 y 的形狀:

(1, 3, 3) (3, 3)

連接數組

函數 描述
concatenate 連接沿現有軸的數組序列
stack 沿著新的軸加入一系列數組。
hstack 水準堆疊序列中的數組(列方向)
vstack 豎直堆疊序列中的數組(行方向)

numpy.concatenate

numpy.concatenate 函數用於沿指定軸連接相同形狀的兩個或多個數組,格式如下:

numpy.concatenate((a1, a2, ...), axis)

參數說明:

  • a1, a2, ...:相同類型的數組
  • axis:沿著它連接數組的軸,默認為 0

實例

import numpy as np a = np.array([[1,2],[3,4]]) print ('第一個數組:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二個數組:') print (b) print ('\n') # 兩個數組的維度相同 print ('沿軸 0 連接兩個數組:') print (np.concatenate((a,b))) print ('\n') print ('沿軸 1 連接兩個數組:') print (np.concatenate((a,b),axis = 1))

輸出結果為:

第一個數組:
[[1 2]
 [3 4]]


第二個數組:
[[5 6]
 [7 8]]


沿軸 0 連接兩個數組:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]


沿軸 1 連接兩個數組:
[[1 2 5 6]
 [3 4 7 8]]

numpy.stack

numpy.stack 函數用於沿新軸連接數組序列,格式如下:

numpy.stack(arrays, axis)

參數說明:

  • arrays相同形狀的數組序列
  • axis:返回數組中的軸,輸入數組沿著它來堆疊

實例

import numpy as np a = np.array([[1,2],[3,4]]) print ('第一個數組:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二個數組:') print (b) print ('\n') print ('沿軸 0 堆疊兩個數組:') print (np.stack((a,b),0)) print ('\n') print ('沿軸 1 堆疊兩個數組:') print (np.stack((a,b),1))

輸出結果如下:

第一個數組:
[[1 2]
 [3 4]]


第二個數組:
[[5 6]
 [7 8]]


沿軸 0 堆疊兩個數組:
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]


沿軸 1 堆疊兩個數組:
[[[1 2]
  [5 6]]

 [[3 4]
  [7 8]]]

numpy.hstack

numpy.hstack 是 numpy.stack 函數的變體,它通過水準堆疊來生成數組。

實例

import numpy as np a = np.array([[1,2],[3,4]]) print ('第一個數組:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二個數組:') print (b) print ('\n') print ('水準堆疊:') c = np.hstack((a,b)) print (c) print ('\n')

輸出結果如下:

第一個數組:
[[1 2]
 [3 4]]


第二個數組:
[[5 6]
 [7 8]]


水準堆疊:

[[1 2 5 6]
 [3 4 7 8]]

numpy.vstack

numpy.vstack 是 numpy.stack 函數的變體,它通過垂直堆疊來生成數組。

實例

import numpy as np a = np.array([[1,2],[3,4]]) print ('第一個數組:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二個數組:') print (b) print ('\n') print ('豎直堆疊:') c = np.vstack((a,b)) print (c)

輸出結果為:

第一個數組:
[[1 2]
 [3 4]]


第二個數組:
[[5 6]
 [7 8]]


豎直堆疊:

[[1 2]
 [3 4]
 [5 6]
 [7 8]]

分割數組

函數 數組及操作
split 將一個數組分割為多個子數組
hsplit 將一個數組水準分割為多個子數組(按列)
vsplit 將一個數組垂直分割為多個子數組(按行)

numpy.split

numpy.split 函數沿特定的軸將數組分割為子數組,格式如下:

numpy.split(ary, indices_or_sections, axis)

參數說明:

  • ary:被分割的數組
  • indices_or_sections:果是一個整數,就用該數平均切分,如果是一個數組,為沿軸切分的位置(左開右閉)
  • axis:沿著哪個維度進行切向,默認為0,橫向切分。為1時,縱向切分

實例

import numpy as np a = np.arange(9) print ('第一個數組:') print (a) print ('\n') print ('將數組分為三個大小相等的子數組:') b = np.split(a,3) print (b) print ('\n') print ('將數組在一維數組中表明的位置分割:') b = np.split(a,[4,7]) print (b)

輸出結果為:

第一個數組:
[0 1 2 3 4 5 6 7 8]


將數組分為三個大小相等的子數組:
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]


將數組在一維數組中表明的位置分割:
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]

numpy.hsplit

numpy.hsplit 函數用於水準分割數組,通過指定要返回的相同形狀的數組數量來拆分原數組。

實例

import numpy as np harr = np.floor(10 * np.random.random((2, 6))) print ('原array:') print(harr) print ('拆分後:') print(np.hsplit(harr, 3))

輸出結果為:

原array:

[[4. 7. 6. 3. 2. 6.]
 [6. 3. 6. 7. 9. 7.]]
拆分後:
[array([[4., 7.],
       [6., 3.]]), array([[6., 3.],
       [6., 7.]]), array([[2., 6.],
       [9., 7.]])]

numpy.vsplit

numpy.vsplit 沿著垂直軸分割,其分割方式與hsplit用法相同。

實例

import numpy as np a = np.arange(16).reshape(4,4) print ('第一個數組:') print (a) print ('\n') print ('豎直分割:') b = np.vsplit(a,2) print (b)

輸出結果為:

第一個數組:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]


豎直分割:

[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])]

數組元素的添加與刪除

函數 元素及描述
resize 返回指定形狀的新數組
append 將值添加到數組末尾
insert 沿指定軸將值插入到指定下標之前
delete 刪掉某個軸的子數組,並返回刪除後的新數組
unique 查找數組內的唯一元素

numpy.resize

numpy.resize 函數返回指定大小的新數組。

如果新數組大小大於原始大小,則包含原始數組中的元素的副本。

numpy.resize(arr, shape)

參數說明:

  • arr:要修改大小的數組
  • shape:返回數組的新形狀

實例

import numpy as np a = np.array([[1,2,3],[4,5,6]]) print ('第一個數組:') print (a) print ('\n') print ('第一個數組的形狀:') print (a.shape) print ('\n') b = np.resize(a, (3,2)) print ('第二個數組:') print (b) print ('\n') print ('第二個數組的形狀:') print (b.shape) print ('\n') # 要注意 a 的第一行在 b 中重複出現,因為尺寸變大了 print ('修改第二個數組的大小:') b = np.resize(a,(3,3)) print (b)

輸出結果為:

第一個數組:
[[1 2 3]
 [4 5 6]]


第一個數組的形狀:

(2, 3)


第二個數組:
[[1 2]
 [3 4]
 [5 6]]


第二個數組的形狀:

(3, 2)


修改第二個數組的大小:

[[1 2 3]
 [4 5 6]
 [1 2 3]]

numpy.append

numpy.append 函數在數組的末尾添加值。 追加操作會分配整個數組,並把原來的數組複製到新數組中。 此外,輸入數組的維度必須匹配否則將生成ValueError。

append 函數返回的始終是一個一維數組。

numpy.append(arr, values, axis=None)

參數說明:

  • arr:輸入數組
  • values:要向arr添加的值,需要和arr形狀相同(除了要添加的軸)
  • axis:默認為 None。當axis無定義時,是橫向加成,返回總是為一維數組!當axis有定義的時候,分別為0和1的時候。當axis有定義的時候,分別為0和1的時候(列數要相同)。當axis為1時,數組是加在右邊(行數要相同)。

實例

import numpy as np a = np.array([[1,2,3],[4,5,6]]) print ('第一個數組:') print (a) print ('\n') print ('向數組添加元素:') print (np.append(a, [7,8,9])) print ('\n') print ('沿軸 0 添加元素:') print (np.append(a, [[7,8,9]],axis = 0)) print ('\n') print ('沿軸 1 添加元素:') print (np.append(a, [[5,5,5],[7,8,9]],axis = 1))

輸出結果為:

第一個數組:
[[1 2 3]
 [4 5 6]]


向數組添加元素:
[1 2 3 4 5 6 7 8 9]


沿軸 0 添加元素:

[[1 2 3]
 [4 5 6]
 [7 8 9]]


沿軸 1 添加元素:

[[1 2 3 5 5 5]
 [4 5 6 7 8 9]]

numpy.insert

numpy.insert 函數在給定索引之前,沿給定軸在輸入數組中插入值。

如果值的類型轉換為要插入,則它與輸入數組不同。 插入沒有原地的,函數會返回一個新數組。 此外,如果未提供軸,則輸入數組會被展開。

numpy.insert(arr, obj, values, axis)

參數說明:

  • arr:輸入數組
  • obj:在其之前插入值的索引
  • values:要插入的值
  • axis:沿著它插入的軸,如果未提供,則輸入數組會被展開

實例

import numpy as np a = np.array([[1,2],[3,4],[5,6]]) print ('第一個數組:') print (a) print ('\n') print ('未傳遞 Axis 參數。 在插入之前輸入數組會被展開。') print (np.insert(a,3,[11,12])) print ('\n') print ('傳遞了 Axis 參數。 會廣播值數組來配輸入數組。') print ('沿軸 0 廣播:') print (np.insert(a,1,[11],axis = 0)) print ('\n') print ('沿軸 1 廣播:') print (np.insert(a,1,11,axis = 1))

輸出結果如下:

第一個數組:
[[1 2]
 [3 4]
 [5 6]]


未傳遞 Axis 參數。 在插入之前輸入數組會被展開。

[ 1  2  3 11 12  4  5  6]


傳遞了 Axis 參數。 會廣播值數組來配輸入數組。

沿軸 0 廣播:

[[ 1  2]
 [11 11]
 [ 3  4]
 [ 5  6]]


沿軸 1 廣播:

[[ 1 11  2]
 [ 3 11  4]
 [ 5 11  6]]

numpy.delete

numpy.delete 函數返回從輸入數組中刪除指定子數組的新數組。 與 insert() 函數的情況一樣,如果未提供軸參數,則輸入數組將展開。

Numpy.delete(arr, obj, axis)

參數說明:

  • arr:輸入數組
  • obj:可以被切片,整數或者整數數組,表明要從輸入數組刪除的子數組
  • axis:沿著它刪除給定子數組的軸,如果未提供,則輸入數組會被展開

實例

import numpy as np a = np.arange(12).reshape(3,4) print ('第一個數組:') print (a) print ('\n') print ('未傳遞 Axis 參數。 在插入之前輸入數組會被展開。') print (np.delete(a,5)) print ('\n') print ('刪除第二列:') print (np.delete(a,1,axis = 1)) print ('\n') print ('包含從數組中刪除的替代值的切片:') a = np.array([1,2,3,4,5,6,7,8,9,10]) print (np.delete(a, np.s_[::2]))

輸出結果為:

第一個數組:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]


未傳遞 Axis 參數。 在插入之前輸入數組會被展開。

[ 0  1  2  3  4  6  7  8  9 10 11]


刪除第二列:
[[ 0  2  3]
 [ 4  6  7]
 [ 8 10 11]]


包含從數組中刪除的替代值的切片:

[ 2  4  6  8 10]

numpy.unique

numpy.unique 函數用於去除數組中的重複元素。

numpy.unique(arr, return_index, return_inverse, return_counts)
  • arr:輸入數組,如果不是一維數組則會展開
  • return_index:如果為true,返回新列表元素在舊列表中的位置(下標),並以列表形式儲
  • return_inverse:如果為true,返回舊列表元素在新列表中的位置(下標),並以列表形式儲
  • return_counts:如果為true,返回去重數組中的元素在原數組中的出現次數

實例

import numpy as np a = np.array([5,2,6,2,7,5,6,8,2,9]) print ('第一個數組:') print (a) print ('\n') print ('第一個數組的去重值:') u = np.unique(a) print (u) print ('\n') print ('去重數組的索引數組:') u,indices = np.unique(a, return_index = True) print (indices) print ('\n') print ('我們可以看到每個和原數組下標對應的數值:') print (a) print ('\n') print ('去重數組的下標:') u,indices = np.unique(a,return_inverse = True) print (u) print ('\n') print ('下標為:') print (indices) print ('\n') print ('使用下標重構原數組:') print (u[indices]) print ('\n') print ('返回去重元素的重複數量:') u,indices = np.unique(a,return_counts = True) print (u) print (indices)

輸出結果為:

第一個數組:
[5 2 6 2 7 5 6 8 2 9]


第一個數組的去重值:
[2 5 6 7 8 9]


去重數組的索引數組:
[1 0 2 4 7 9]


我們可以看到每個和原數組下標對應的數值:
[5 2 6 2 7 5 6 8 2 9]


去重數組的下標:
[2 5 6 7 8 9]


下標為:
[1 0 2 0 3 1 2 4 0 5]


使用下標重構原數組:
[5 2 6 2 7 5 6 8 2 9]


返回去重元素的重複數量:
[2 5 6 7 8 9]
[3 2 2 1 1 1]