背景
什麼是 NumPy 呢?
NumPy 這個詞來源於兩個單詞 – Numerical
和Python
。其是一個功能強大的 Python 庫,可以幫助程序員輕鬆地進行數值計算,通常應用於以下場景:
- 執行各種數學任務,如:數值積分、微分、內插、外推等。因此,當涉及到數學任務時,它形成了一種基於 Python 的 MATLAB 的快速替代。
- 計算機中的圖像表示爲多維數字數組。NumPy 提供了一些優秀的庫函數來快速處理圖像。例如,鏡像圖像、按特定角度旋轉圖像等。
- 在編寫機器學習算法時,需要對矩陣進行各種數值計算。如:矩陣乘法、求逆、換位、加法等。NumPy 數組用於存儲訓練數據和機器學習模型的參數。
排序,搜索和計數
排序
numpy.sort(a[, axis=-1, kind='quicksort', order=None])
Return a sorted copy of an array.- axis:排序沿數組的(軸)方向,0表示按行,1表示按列,None表示展開來排序,默認爲-1,表示沿最後的軸排序。
- kind:排序的算法,提供了快排’quicksort’、混排’mergesort’、堆排’heapsort’, 默認爲‘quicksort’。
- order:排序的字段名,可指定字段排序,默認爲None。
【例】
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
y = np.sort(x)
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
# [5.17 6.93 8.25 9.28 9.76]
# [0.01 0.19 1.73 4.23 9.27]
# [0.88 4.29 4.97 7.32 7.99]
# [0.07 6.99 7.9 8.95 9.05]]
y = np.sort(x, axis=0)
print(y)
# [[0.01 0.07 0.19 1.73 4.29]
# [2.32 4.23 0.88 1.73 6.22]
# [6.93 4.97 8.95 7.32 6.99]
# [7.99 5.17 9.28 7.9 8.25]
# [9.05 7.54 9.78 9.76 9.27]]
y = np.sort(x, axis=1)
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
# [5.17 6.93 8.25 9.28 9.76]
# [0.01 0.19 1.73 4.23 9.27]
# [0.88 4.29 4.97 7.32 7.99]
# [0.07 6.99 7.9 8.95 9.05]]
【例】
import numpy as np
dt = np.dtype([('name', 'S10'), ('age', np.int)])
a = np.array([("Mike", 21), ("Nancy", 25), ("Bob", 17), ("Jane", 27)], dtype=dt)
b = np.sort(a, order='name')
print(b)
# [(b'Bob', 17) (b'Jane', 27) (b'Mike', 21) (b'Nancy', 25)]
b = np.sort(a, order='age')
print(b)
# [(b'Bob', 17) (b'Mike', 21) (b'Nancy', 25) (b'Jane', 27)]
如果排序後,想用元素的索引位置替代排序後的實際結果,該怎麼辦呢?
numpy.argsort(a[, axis=-1, kind='quicksort', order=None])
Returns the indices that would sort an array.
【例】對數組沿給定軸執行間接排序,並使用指定排序類型返回數據的索引數組。這個索引數組用於構造排序後的數組。
import numpy as np
np.random.seed(20200612)
x = np.random.randint(0, 10, 10)
print(x)
# [6 1 8 5 5 4 1 2 9 1]
y = np.argsort(x)
print(y)
# [1 6 9 7 5 3 4 0 2 8]
print(x[y])
# [1 1 1 2 4 5 5 6 8 9]
y = np.argsort(-x)
print(y)
# [8 2 0 3 4 5 7 1 6 9]
print(x[y])
# [9 8 6 5 5 4 2 1 1 1]
【例】
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
y = np.argsort(x)
print(y)
# [[3 0 4 1 2]
# [1 0 4 2 3]
# [0 2 3 1 4]
# [2 4 1 3 0]
# [1 4 3 2 0]]
y = np.argsort(x, axis=0)
print(y)
# [[2 4 2 0 3]
# [0 2 3 2 0]
# [1 3 4 3 4]
# [3 1 1 4 1]
# [4 0 0 1 2]]
y = np.argsort(x, axis=1)
print(y)
# [[3 0 4 1 2]
# [1 0 4 2 3]
# [0 2 3 1 4]
# [2 4 1 3 0]
# [1 4 3 2 0]]
y = np.array([np.take(x[i], np.argsort(x[i])) for i in range(5)])
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
# [5.17 6.93 8.25 9.28 9.76]
# [0.01 0.19 1.73 4.23 9.27]
# [0.88 4.29 4.97 7.32 7.99]
# [0.07 6.99 7.9 8.95 9.05]]
如何將數據按照某一指標進行排序呢?
numpy.lexsort(keys[, axis=-1])
Perform an indirect stable sort using a sequence of keys.
【例】按照第一列的升序或者降序對整體數據進行排序。
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
index = np.lexsort([x[:, 0]])
print(index)
# [2 0 1 3 4]
y = x[index]
print(y)
# [[0.01 4.23 0.19 1.73 9.27]
# [2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
index = np.lexsort([-1 * x[:, 0]])
print(index)
# [4 3 1 0 2]
y = x[index]
print(y)
# [[9.05 0.07 8.95 7.9 6.99]
# [7.99 4.97 0.88 7.32 4.29]
# [6.93 5.17 9.28 9.76 8.25]
# [2.32 7.54 9.78 1.73 6.22]
# [0.01 4.23 0.19 1.73 9.27]]
【例】
import numpy as np
x = np.array([1, 5, 1, 4, 3, 4, 4])
y = np.array([9, 4, 0, 4, 0, 2, 1])
a = np.lexsort([x])
b = np.lexsort([y])
print(a)
# [0 2 4 3 5 6 1]
print(x[a])
# [1 1 3 4 4 4 5]
print(b)
# [2 4 6 5 1 3 0]
print(y[b])
# [0 0 1 2 4 4 9]
z = np.lexsort([y, x])
print(z)
# [2 0 4 6 5 3 1]
print(x[z])
# [1 1 3 4 4 4 5]
z = np.lexsort([x, y])
print(z)
# [2 4 6 5 3 1 0]
print(y[z])
# [0 0 1 2 4 4 9]
搜索
numpy.argmax(a[, axis=None, out=None])
Returns the indices of the maximum values along an axis.
【例】
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
y = np.argmax(x)
print(y) # 2
y = np.argmax(x, axis=0)
print(y)
# [4 0 0 1 2]
y = np.argmax(x, axis=1)
print(y)
# [2 3 4 0 0]
numpy.argmin(a[, axis=None, out=None])
Returns the indices of the minimum values along an axis.
【例】
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
y = np.argmin(x)
print(y) # 10
y = np.argmin(x, axis=0)
print(y)
# [2 4 2 0 3]
y = np.argmin(x, axis=1)
print(y)
# [3 1 0 2 1]
numpy.where(condition, [x=None, y=None])
函數返回輸入數組中滿足給定條件的元素的索引。
【例】
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.where(x > 5)
print(y)
# (array([5, 6, 7], dtype=int64),)
print(x[y])
# [6 7 8]
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.where(x > 25)
print(y)
# (array([3, 3, 3, 3, 3, 4, 4, 4, 4, 4], dtype=int64), array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=int64))
print(x[y])
# [26 27 28 29 30 31 32 33 34 35]
numpy.searchsorted(a, v[, side='left', sorter=None])
Find indices where elements should be inserted to maintain order.- a:一維輸入數組。當
sorter
參數爲None
的時候,a
必須爲升序數組;否則,sorter
不能爲空,存放a
中元素的index
,用於反映a
數組的升序排列方式。 - v:插入
a
數組的值,可以爲單個元素,list
或者ndarray
。 - side:查詢方向,當爲
left
時,將返回第一個符合條件的元素下標;當爲right
時,將返回最後一個符合條件的元素下標。 - sorter:一維數組存放
a
數組元素的 index,index 對應元素爲升序。
- a:一維輸入數組。當
【例】
import numpy as np
x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
y = np.searchsorted(x, 15)
print(y) # 5
y = np.searchsorted(x, 15, side='right')
print(y) # 5
y = np.searchsorted(x, -1)
print(y) # 0
y = np.searchsorted(x, -1, side='right')
print(y) # 0
y = np.searchsorted(x, 35)
print(y) # 8
y = np.searchsorted(x, 35, side='right')
print(y) # 8
y = np.searchsorted(x, 11)
print(y) # 4
y = np.searchsorted(x, 11, side='right')
print(y) # 5
y = np.searchsorted(x, 0)
print(y) # 0
y = np.searchsorted(x, 0, side='right')
print(y) # 1
y = np.searchsorted(x, 33)
print(y) # 7
y = np.searchsorted(x, 33, side='right')
print(y) # 8
【例】
import numpy as np
x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35])
print(y) # [0 0 4 5 7 8]
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], side='right')
print(y) # [0 1 5 5 8 8]
【例】
import numpy as np
x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
np.random.shuffle(x)
print(x) # [33 1 9 18 11 26 0 5]
x_sort = np.argsort(x)
print(x_sort) # [6 1 7 2 4 3 5 0]
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], sorter=x_sort)
print(y) # [0 0 4 5 7 8]
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], side='right', sorter=x_sort)
print(y) # [0 1 5 5 8 8]
計數
numpy.count_nonzero(a, axis=None)
Counts the number of non-zero values in the array a.
【例】返回數組中的非0元素個數。
import numpy as np
x = np.count_nonzero(np.eye(4))
print(x) # 4
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
print(x) # 5
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0)
print(x) # [1 1 1 1 1]
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1)
print(x) # [2 3]
當前活動
我是 終身學習者“老馬”,一個長期踐行“結伴式學習”理念的 中年大叔。
我崇尚分享,渴望成長,於2010年創立了“LSGO軟件技術團隊”,並加入了國內著名的開源組織“Datawhale”,也是“Dre@mtech”、“智能機器人研究中心”和“大數據與哲學社會科學實驗室”的一員。
願我們一起學習,一起進步,相互陪伴,共同成長。
後臺回覆「搜搜搜」,隨機獲取電子資源!
歡迎關注,請掃描二維碼: