入門 | 資料科學初學者必知的NumPy基礎知識
NumPy(Numerical Python)是 Python 中的一個線性代數庫。對每一個數據科學或機器學習 Python 包而言,這都是一個非常重要的庫,SciPy(Scientific Python)、Mat-plotlib(plotting library)、Scikit-learn 等都在一定程度上依賴 NumPy。
對陣列執行數學運算和邏輯運算時,NumPy 是非常有用的。在用 Python 對 n 維陣列和矩陣進行運算時,NumPy 提供了大量有用特徵。
這篇教程介紹了資料科學初學者需要了解的 NumPy 基礎知識,包括如何建立 NumPy 陣列、如何使用 NumPy 中的廣播機制、如何獲取值以及如何運算元組。更重要的是,大家可以通過本文了解到 NumPy 在 Python 列表中的優勢:更簡潔、更快速地讀寫項、更方便、更高效。
本教程將使用 Jupyter notebook 作為編輯器。
讓我們開始吧!
安裝 NumPy
如果你已經裝有 Anaconda,那麼你可以使用以下命令通過終端或命令提示符安裝 NumPy:
conda install numpy
如果你沒有 Anaconda,那麼你可以使用以下命令從終端上安裝 NumPy:
pip install numpy
安裝好 NumPy 後,你就可以啟動 Jupyter notebook 開始學習了。接下來從 NumPy 陣列開始。
NumPy 陣列
NumPy 陣列是包含相同型別值的網格。NumPy 陣列有兩種形式:向量和矩陣。嚴格地講,向量是一維陣列,矩陣是多維陣列。在某些情況下,矩陣只有一行或一列。
首先將 NumPy 匯入 Jupyter notebook:
import numpy as np
從 Python 列表中建立 NumPy 陣列
我們先建立一個 Python 列表:
my_list = [1, 2, 3, 4, 5]
通過這個列表,我們可以簡單地建立一個名為 my_numpy_list 的 NumPy 陣列,顯示結果:
my_numpy_list = np.array(my_list)
my_numpy_list #This line show the result of the array generated
剛才我們將一個 Python 列表轉換成一維陣列。要想得到二維陣列,我們要建立一個元素為列表的列表,如下所示:
second_list = [[1,2,3], [5,4,1], [3,6,7]]
new_2d_arr = np.array(second_list)
new_2d_arr #This line show the result of the array generated
我們已經成功建立了一個有 3 行 3 列的二維陣列。
使用 arange() 內建函式建立 NumPy 陣列
與 Python 的 range() 內建函式相似,我們可以用 arange() 建立一個 NumPy 陣列。
my_list = np.arange(10)
#OR
my_list = np.arange(0,10)
這產生了 0~10 的十個數字。
要注意的是 arange() 函式中有三個引數。第三個引數表示步長。例如,要得到 0~10 中的偶數,只需要將步長設定為 2 就可以了,如下所示:
my_list = np.arange(0,11,2)
還可以建立有 7 個 0 的一維陣列:
my_zeros = np.zeros(7)
也可以建立有 5 個 1 的一維陣列:
my_ones = np.ones(5)
同樣,我們可以生成內容都為 0 的 3 行 5 列二維陣列:
two_d = np.zeros((3,5))
使用 linspace() 內建函式建立 NumPy 陣列
linspace() 函式返回的數字都具有指定的間隔。也就是說,如果我們想要 1 到 3 中間隔相等的 15 個點,我們只需使用以下命令:
lin_arr = np.linspace(1, 3, 15)
該命令可生成一維向量。
與 arange() 函式不同,linspace() 的第三個引數是要建立的資料點數量。
在 NumPy 中建立一個恆等矩陣
處理線性代數時,恆等矩陣是非常有用的。一般而言,恆等矩陣是一個二維方矩陣,也就是說在這個矩陣中列數與行數相等。有一點要注意的是,恆等矩陣的對角線都是 1,其他的都是 0。恆等矩陣一般只有一個引數,下述命令說明了要如何建立恆等矩陣:
my_matrx = np.eye(6) #6 is the number of columns/rows you want
用 NumPy 建立一個隨機陣列成的陣列
我們可以使用 rand()、randn() 或 randint() 函式生成一個隨機陣列成的陣列。
● 使用 random.rand(),我們可以生成一個從 0~1 均勻產生的隨機陣列成的陣列。
例如,如果想要一個由 4 個物件組成的一維陣列,且這 4 個物件均勻分佈在 0~1,可以這樣做:
my_rand = np.random.rand(4)
如果我們想要一個有 5 行 4 列的二維陣列,則:
my_rand = np.random.rand(5, 4)
my_rand
● 使用 randn(),我們可以從以 0 為中心的標準正態分佈或高斯分佈中產生隨機樣本。例如,我們這樣生成 7 個隨機數:
my_randn = np.random.randn(7)
my_randn
繪製結果後會得到一個正態分佈曲線。
同樣地,如需建立一個 3 行 5 列的二維陣列,這樣做即可:
np.random.randn(3,5)
● 最後,我們可以使用 randint() 函式生成整數陣列。randint() 函式最多可以有三個引數:最小值(包含),最大值(不包含)以及陣列的大小。
np.random.randint(20) #generates a random integer exclusive of 20
np.random.randint(2, 20) #generates a random integer including 2 but excluding 20
np.random.randint(2, 20, 7) #generates 7 random integers including 2 but excluding 20
將一維陣列轉換成二維陣列
先建立一個有 25 個隨機整數的一維陣列:
arr = np.random.rand(25)
然後使用 reshape() 函式將其轉換為二維陣列:
arr.reshape(5,5)
注意:reshape() 僅可轉換成行列數目相等,且行列數相乘後要與元素數量相等。上例中的 arr 包含 25 個元素,因此只能重塑為 5*5 的矩陣。
定位 NumPy 陣列中的最大值和最小值
使用 max() 和 min() 函式,我們可以得到陣列中的最大值或最小值:
arr_2 = np.random.randint(0, 20, 10)
arr_2.max() #This gives the highest value in the array
arr_2.min() #This gives the lowest value in the array
使用 argmax() 和 argmin() 函式,我們可以定位陣列中最大值和最小值的索引:
arr_2.argmax() #This shows the index of the highest value in the array
arr_2.argmin() #This shows the index of the lowest value in the array
假設存在大量陣列,而你需要弄清楚陣列的形態,你想知道這個陣列是一維陣列還是二維陣列,只需要使用 shape 函式即可:
arr.shape
從 NumPy 陣列中索引/選擇多個元素(組)
在 NumPy 陣列中進行索引與 Python 類似,只需輸入想要的索引即可:
my_array = np.arange(0,11)
my_array[8] #This gives us the value of element at index 8
為了獲得陣列中的一系列值,我們可以使用切片符「:」,就像在 Python 中一樣:
my_array[2:6] #This returns everything from index 2 to 6(exclusive)
my_array[:6] #This returns everything from index 0 to 6(exclusive)
my_array[5:] #This returns everything from index 5 to the end of the array.
類似地,我們也可以通過使用 [ ][ ] 或 [,] 在二維陣列中選擇元素。
使用 [ ][ ] 從下面的二維陣列中抓取出值「60」:
two_d_arr = np.array([[10,20,30], [40,50,60], [70,80,90]])
two_d_arr[1][2] #The value 60 appears is in row index 1, and column index 2
使用 [,] 從上面的二維陣列中抓取出值「20」:
two_d_arr[0,1]
也可以用切片符抓取二維陣列的子部分。使用下面的操作從陣列中抓取一些元素:
two_d_arr[:1, :2] # This returns [[10, 20]]
two_d_arr[:2, 1:] # This returns ([[20, 30], [50, 60]])
two_d_arr[:2, :2] #This returns ([[10, 20], [40, 50]])
我們還可以索引一整行或一整列。只需使用索引數字即可抓取任意一行:
two_d_arr[0] #This grabs row 0 of the array ([10, 20, 30])
two_d_arr[:2] #This grabs everything before row 2 ([[10, 20, 30], [40, 50, 60]])
還可以使用 &、|、<、> 和 == 運算子對陣列執行條件選擇和邏輯選擇,從而對比陣列中的值和給定值:
new_arr = np.arange(5,15)
new_arr > 10 #This returns TRUE where the elements are greater than 10 [False, False, False, False, False, False, True, True, True, True]
現在我們可以輸出符合上述條件的元素:
bool_arr = new_arr > 10
new_arr[bool_arr] #This returns elements greater than 10 [11, 12, 13, 14]
new_arr[new_arr>10] #A shorter way to do what we have just done
組合使用條件運算子和邏輯運算子,我們可以得到值大於 6 小於 10 的元素:
new_arr[(new_arr>6) & (new_arr<10)]
預期結果為:([7, 8, 9])
廣播機制
廣播機制是一種快速改變 NumPy 陣列中的值的方式。
my_array[0:3] = 50
#Result is:
[50, 50, 50, 3, 4, 5, 6, 7, 8, 9, 10]
在這個例子中,我們將索引為 0 到 3 的元素的初始值改為 50。
對 NumPy 陣列執行數學運算
arr = np.arange(1,11)
arr * arr #Multiplies each element by itself
arr - arr #Subtracts each element from itself
arr + arr #Adds each element to itself
arr / arr #Divides each element by itself
我們還可以對陣列執行標量運算,NumPy 通過廣播機制使其成為可能:
arr + 50 #This adds 50 to every element in that array
NumPy 還允許在陣列上執行通用函式,如平方根函式、指數函式和三角函式等。
np.sqrt(arr) #Returns the square root of each element
np.exp(arr) #Returns the exponentials of each element
np.sin(arr) #Returns the sin of each element
np.cos(arr) #Returns the cosine of each element
np.log(arr) #Returns the logarithm of each element
np.sum(arr) #Returns the sum total of elements in the array
np.std(arr) #Returns the standard deviation of in the array
我們還可以在二維陣列中抓取行或列的總和:
mat = np.arange(1,26).reshape(5,5)
mat.sum() #Returns the sum of all the values in mat
mat.sum(axis=0) #Returns the sum of all the columns in mat
mat.sum(axis=1) #Returns the sum of all the rows in mat
現在,這篇 NumPy 教程進入了尾聲!希望對大家有所幫助。
原文釋出時間為:2018-10-23
本文作者:Ehi Aigiomawu
本文來自雲棲社群合作伙伴“OTAxMDQzNQ==&mid=2650616733&idx=2&sn=66e8c24f8effba11d87e0c41a7fd72b8&chksm=87b39c71b0c41567803605d6bb9dea0235e24e52b5a4e42beb476a56b226f9e93090264615ab&scene=0#rd" target="_blank" rel="nofollow,noindex">CDA資料分析師 ”,瞭解相關資訊可以關注“CDA資料分析師 ”。