中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

數(shù)據(jù)科學(xué)初學(xué)者必知的NumPy基礎(chǔ)知識

2018-07-20    來源:編程學(xué)習(xí)網(wǎng)

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

本文介紹了一些 NumPy 基礎(chǔ)知識,適合數(shù)據(jù)科學(xué)初學(xué)者學(xué)習(xí)掌握。

NumPy(Numerical Python)是 Python 中的一個線性代數(shù)庫。對每一個數(shù)據(jù)科學(xué)或機(jī)器學(xué)習(xí) Python 包而言,這都是一個非常重要的庫,SciPy(Scientific Python)、Mat-plotlib(plotting library)、Scikit-learn 等都在一定程度上依賴 NumPy。

對數(shù)組執(zhí)行數(shù)學(xué)運(yùn)算和邏輯運(yùn)算時(shí),NumPy 是非常有用的。在用 Python 對 n 維數(shù)組和矩陣進(jìn)行運(yùn)算時(shí),NumPy 提供了大量有用特征。

這篇教程介紹了數(shù)據(jù)科學(xué)初學(xué)者需要了解的 NumPy 基礎(chǔ)知識,包括如何創(chuàng)建 NumPy 數(shù)組、如何使用 NumPy 中的廣播機(jī)制、如何獲取值以及如何操作數(shù)組。更重要的是,大家可以通過本文了解到 NumPy 在 Python 列表中的優(yōu)勢:更簡潔、更快速地讀寫項(xiàng)、更方便、更高效。

本教程將使用 Jupyter notebook 作為編輯器。

讓我們開始吧!

安裝 NumPy

如果你已經(jīng)裝有 Anaconda,那么你可以使用以下命令通過終端或命令提示符安裝 NumPy:

conda install numpy

如果你沒有 Anaconda,那么你可以使用以下命令從終端上安裝 NumPy:

pip install numpy

安裝好 NumPy 后,你就可以啟動 Jupyter notebook 開始學(xué)習(xí)了。接下來從 NumPy 數(shù)組開始。

NumPy 數(shù)組

NumPy 數(shù)組是包含相同類型值的網(wǎng)格。NumPy 數(shù)組有兩種形式:向量和矩陣。嚴(yán)格地講,向量是一維數(shù)組,矩陣是多維數(shù)組。在某些情況下,矩陣只有一行或一列。

首先將 NumPy 導(dǎo)入 Jupyter notebook:

import numpy as np

從 Python 列表中創(chuàng)建 NumPy 數(shù)組

我們先創(chuàng)建一個 Python 列表:

my_list = [1, 2, 3, 4, 5]

通過這個列表,我們可以簡單地創(chuàng)建一個名為 my_numpy_list 的 NumPy 數(shù)組,顯示結(jié)果:

my_numpy_list = np.array(my_list)
my_numpy_list  #This line show the result of the array generated

剛才我們將一個 Python 列表轉(zhuǎn)換成一維數(shù)組。要想得到二維數(shù)組,我們要創(chuàng)建一個元素為列表的列表,如下所示:

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

我們已經(jīng)成功創(chuàng)建了一個有 3 行 3 列的二維數(shù)組。

使用 arange() 內(nèi)置函數(shù)創(chuàng)建 NumPy 數(shù)組

與 Python 的 range() 內(nèi)置函數(shù)相似,我們可以用 arange() 創(chuàng)建一個 NumPy 數(shù)組。

my_list = np.arange(10)
#OR
my_list = np.arange(0,10)

這產(chǎn)生了 0~10 的十個數(shù)字。

要注意的是 arange() 函數(shù)中有三個參數(shù)。第三個參數(shù)表示步長。例如,要得到 0~10 中的偶數(shù),只需要將步長設(shè)置為 2 就可以了,如下所示:

my_list = np.arange(0,11,2)

還可以創(chuàng)建有 7 個 0 的一維數(shù)組:

my_zeros = np.zeros(7)

也可以創(chuàng)建有 5 個 1 的一維數(shù)組:

my_ones = np.ones(5)

同樣,我們可以生成內(nèi)容都為 0 的 3 行 5 列二維數(shù)組:

two_d = np.zeros((3,5))

使用 linspace() 內(nèi)置函數(shù)創(chuàng)建 NumPy 數(shù)組

linspace() 函數(shù)返回的數(shù)字都具有指定的間隔。也就是說,如果我們想要 1 到 3 中間隔相等的 15 個點(diǎn),我們只需使用以下命令:

lin_arr = np.linspace(1, 3, 15)

該命令可生成一維向量。

與 arange() 函數(shù)不同,linspace() 的第三個參數(shù)是要創(chuàng)建的數(shù)據(jù)點(diǎn)數(shù)量。

在 NumPy 中創(chuàng)建一個恒等矩陣

處理線性代數(shù)時(shí),恒等矩陣是非常有用的。一般而言,恒等矩陣是一個二維方矩陣,也就是說在這個矩陣中列數(shù)與行數(shù)相等。有一點(diǎn)要注意的是,恒等矩陣的對角線都是 1,其他的都是 0。恒等矩陣一般只有一個參數(shù),下述命令說明了要如何創(chuàng)建恒等矩陣:

my_matrx = np.eye(6)    #6 is the number of columns/rows you want

用 NumPy 創(chuàng)建一個隨機(jī)數(shù)組成的數(shù)組

我們可以使用 rand()、randn() 或 randint() 函數(shù)生成一個隨機(jī)數(shù)組成的數(shù)組。

  • 使用 random.rand(),我們可以生成一個從 0~1 均勻產(chǎn)生的隨機(jī)數(shù)組成的數(shù)組。

例如,如果想要一個由 4 個對象組成的一維數(shù)組,且這 4 個對象均勻分布在 0~1,可以這樣做:

my_rand = np.random.rand(4)

如果我們想要一個有 5 行 4 列的二維數(shù)組,則:

my_rand = np.random.rand(5, 4)
my_rand
  • 使用 randn(),我們可以從以 0 為中心的標(biāo)準(zhǔn)正態(tài)分布或高斯分布中產(chǎn)生隨機(jī)樣本。例如,我們這樣生成 7 個隨機(jī)數(shù):

my_randn = np.random.randn(7)
my_randn

繪制結(jié)果后會得到一個正態(tài)分布曲線。

同樣地,如需創(chuàng)建一個 3 行 5 列的二維數(shù)組,這樣做即可:

np.random.randn(3,5)
  • 最后,我們可以使用 randint() 函數(shù)生成整數(shù)數(shù)組。randint() 函數(shù)最多可以有三個參數(shù):最小值(包含),最大值(不包含)以及數(shù)組的大小。

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

將一維數(shù)組轉(zhuǎn)換成二維數(shù)組

先創(chuàng)建一個有 25 個隨機(jī)整數(shù)的一維數(shù)組:

arr = np.random.rand(25)

然后使用 reshape() 函數(shù)將其轉(zhuǎn)換為二維數(shù)組:

arr.reshape(5,5)

注意:reshape() 僅可轉(zhuǎn)換成行列數(shù)目相等,且行列數(shù)相乘后要與元素?cái)?shù)量相等。上例中的 arr 包含 25 個元素,因此只能重塑為 5*5 的矩陣。

定位 NumPy 數(shù)組中的最大值和最小值

使用 max() 和 min() 函數(shù),我們可以得到數(shù)組中的最大值或最小值:

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() 函數(shù),我們可以定位數(shù)組中最大值和最小值的索引:

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

假設(shè)存在大量數(shù)組,而你需要弄清楚數(shù)組的形態(tài),你想知道這個數(shù)組是一維數(shù)組還是二維數(shù)組,只需要使用 shape 函數(shù)即可:

arr.shape

從 NumPy 數(shù)組中索引/選擇多個元素(組)

在 NumPy 數(shù)組中進(jìn)行索引與 Python 類似,只需輸入想要的索引即可:

my_array = np.arange(0,11)
my_array[8]  #This gives us the value of element at index 8

為了獲得數(shù)組中的一系列值,我們可以使用切片符「:」,就像在 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.

類似地,我們也可以通過使用 [ ][ ] 或 [,] 在二維數(shù)組中選擇元素。

使用 [ ][ ] 從下面的二維數(shù)組中抓取出值「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

使用 [,] 從上面的二維數(shù)組中抓取出值「20」:

two_d_arr[0,1]

也可以用切片符抓取二維數(shù)組的子部分。使用下面的操作從數(shù)組中抓取一些元素:

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]])

我們還可以索引一整行或一整列。只需使用索引數(shù)字即可抓取任意一行:

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]])

還可以使用 &、|、<、> 和 == 運(yùn)算符對數(shù)組執(zhí)行條件選擇和邏輯選擇,從而對比數(shù)組中的值和給定值:

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]

現(xiàn)在我們可以輸出符合上述條件的元素:

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

組合使用條件運(yùn)算符和邏輯運(yùn)算符,我們可以得到值大于 6 小于 10 的元素:

new_arr[(new_arr>6) & (new_arr<10)]

預(yù)期結(jié)果為:([7, 8, 9])

廣播機(jī)制

廣播機(jī)制是一種快速改變 NumPy 數(shù)組中的值的方式。

my_array[0:3] = 50
#Result is: 
[50, 50, 50, 3, 4,  5,  6,  7,  8,  9, 10]

在這個例子中,我們將索引為 0 到 3 的元素的初始值改為 50。

對 NumPy 數(shù)組執(zhí)行數(shù)學(xué)運(yùn)算

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

我們還可以對數(shù)組執(zhí)行標(biāo)量運(yùn)算,NumPy 通過廣播機(jī)制使其成為可能:

arr + 50              #This adds 50 to every element in that array

NumPy 還允許在數(shù)組上執(zhí)行通用函數(shù),如平方根函數(shù)、指數(shù)函數(shù)和三角函數(shù)等。

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

我們還可以在二維數(shù)組中抓取行或列的總和:

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

現(xiàn)在,這篇 NumPy 教程進(jìn)入了尾聲!希望對大家有所幫助。

 

來自:https://www.jiqizhixin.com/articles/2018-04-21-7

 

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:善于單挑卻難以協(xié)作,構(gòu)建多智能體AI系統(tǒng)為何如此之難?

下一篇:Python并發(fā)編程之進(jìn)程