python基礎(chǔ)之Numpy庫中array用法總結(jié)
Numpy是Python的一個(gè)科學(xué)計(jì)算的庫,提供了矩陣運(yùn)算的功能,其一般與Scipy、matplotlib一起使用。其實(shí),list已經(jīng)提供了類似于矩陣的表示形式,不過numpy為我們提供了更多的函數(shù)。
NumPy數(shù)組是一個(gè)多維數(shù)組對(duì)象,稱為ndarray。數(shù)組的下標(biāo)從0開始,同一個(gè)NumPy數(shù)組中所有元素的類型必須是相同的。
>>> import numpy as np
為什么要用numpy
Python中提供了list容器,可以當(dāng)作數(shù)組使用。但列表中的元素可以是任何對(duì)象,因此列表中保存的是對(duì)象的指針,這樣一來,為了保存一個(gè)簡(jiǎn)單的列表[1,2,3]。就需要三個(gè)指針和三個(gè)整數(shù)對(duì)象。對(duì)于數(shù)值運(yùn)算來說,這種結(jié)構(gòu)顯然不夠高效。
Python雖然也提供了array模塊,但其只支持一維數(shù)組,不支持多維數(shù)組(在TensorFlow里面偏向于矩陣?yán)斫?,也沒有各種運(yùn)算函數(shù)。因而不適合數(shù)值運(yùn)算。
NumPy的出現(xiàn)彌補(bǔ)了這些不足。
數(shù)組的創(chuàng)建
使用numpy.array方法將tuple和list, array, 或者其他的序列模式的數(shù)據(jù)轉(zhuǎn)創(chuàng)建為 ndarray, 默認(rèn)創(chuàng)建一個(gè)新的 ndarray.
>>> np.array([1,2,3,4]) [1 2 3 4] >>> b = array( [ (1.5,2,3), (4,5,6) ] ) array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]]) >>> c = array( [ [1,2], [3,4] ], dtype=complex) #指定數(shù)組中元素的類型 >>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]])
生成均勻分布的array:
arange(最小值,最大值,步長(zhǎng))(左閉右開) : 創(chuàng)建等差數(shù)列
linspace(最小值,最大值,元素?cái)?shù)量)
logspace(開始值, 終值, 元素個(gè)數(shù)): 創(chuàng)建等比數(shù)列
>>> np.arange(15) [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] >>> np.arange(15).reshape(3,5) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] >>> np.arange( 0, 2, 0.3 ) array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) >>> np.linspace(1,3,9) [ 1. 1.25 1.51.75 2. 2.25 2.52.75 3. ]
生成特殊數(shù)組
np.ones: 創(chuàng)建一個(gè)數(shù)組, 其中的元素全為 1
np.zeros: 創(chuàng)建元素全為 0 的數(shù)組, 類似 np.ones
np.empty創(chuàng)建一個(gè)內(nèi)容隨機(jī)并且依賴與內(nèi)存狀態(tài)的數(shù)組。
np.eye: 創(chuàng)建一個(gè)對(duì)角線為 1 其他為 0 的矩陣.
np.identity: 創(chuàng)建一個(gè)主對(duì)角線為 1 其他為 0 的方陣.
>>> np.zeros((3,4)) [[ 0. 0. 0. 0.] [ 0. 0. 0. 0.] [ 0. 0. 0. 0.]] >>> np.ones((3,4)) [[ 1. 1. 1. 1.] [ 1. 1. 1. 1.] [ 1. 1. 1. 1.]] >>> np.eye(3) [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]]
獲取數(shù)組的屬性
>>> a = np.zeros((2,2,2)) >>> a.ndim#數(shù)組的維數(shù) 3 >>> a.shape #數(shù)組每一維的大小 (2, 2, 2) >>> a.size#數(shù)組全部元素的數(shù)量 8 >>> a.dtype #數(shù)組中元素的類型 float64 >>> print a.itemsize #每個(gè)元素所占的字節(jié)數(shù) 8
數(shù)組索引,切片,賦值
‘…'符號(hào)表示將所有未指定索引的維度均賦為 ‘:'
‘:'在python中表示該維所有元素
>>> a = np.array( [[2,3,4],[5,6,7]] ) >>> a [[2 3 4] [5 6 7]] >>> a[1,2] 7 >>> a[1,:] [5 6 7] >>> print a[1,1:2] [6] >>> a[1,:] = [8,9,10] >>> a [[ 2 3 4] [ 8 9 10]] >>> c[1,...] # same as c[1,:,:] or c[1] array([[100, 101, 102], [110, 112, 113]]) >>> c[...,2] # same as c[:,:,2] array([[ 2, 13], [102, 113]]) >>> def f(x,y): ... return 10*x+y ... >>> b = np.fromfunction(f,(5,4),dtype=int) # >>> b array([[ 0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]])
數(shù)組操作
>>> a = np.ones((2,2)) >>> b = np.eye(2) >>> print a [[ 1. 1.] [ 1. 1.]] >>> print b [[ 1. 0.] [ 0. 1.]]
>>> print a > 2 [[False False] [False False]] >>> print a+b #數(shù)組加,對(duì)應(yīng)位置相加 [[ 2. 1.] [ 1. 2.]] >>> print a-b #數(shù)組減,對(duì)應(yīng)位置相減 [[ 0. 1.] [ 1. 0.]] >>> print b*2 #數(shù)組與數(shù)值相乘,對(duì)應(yīng)位置乘 [[ 2. 0.] [ 0. 2.]] >>> print (a*2)*(b*2) #數(shù)組與數(shù)組相乘,按位置一對(duì)一相乘 [[ 4. 0.] [ 0. 4.]] >>> print b/(a*2) #數(shù)組與數(shù)組相除,按位置一對(duì)一相除 [[ 0.5 0. ] [ 0.0.5]] >>> print a.dot(b) # matrix product,矩陣乘 >>> np.dot(a,a) #矩陣乘法 array([[ 2., 2.], [ 2., 2.]]) >>> print (a*2)**4 [[ 16. 16.] [ 16. 16.]] >>> b = a #淺拷貝 >>> b is a True >>> c = a.copy() #深拷貝 >>> c is a False
內(nèi)置函數(shù)(min,max,sum),同時(shí)可以使用axis指定對(duì)哪一維進(jìn)行操作:
>>> a.sum() 4.0 >>> a.sum(axis=0) #計(jì)算每一列(二維數(shù)組中類似于矩陣的列)的和 array([ 2., 2.]) >>> a.min() #數(shù)組最小值 1.0 >>> a.max() #數(shù)組最大值 1.0
使用numpy下的方法:
>>> np.sin(a) array([[ 0.84147098, 0.84147098], [ 0.84147098, 0.84147098]]) >>> np.max(a) 1.0 >>> np.floor(a) array([[ 1., 1.], [ 1., 1.]]) >>> np.exp(a)#e^x array([[ 2.71828183, 2.71828183], [ 2.71828183, 2.71828183]]) >>> print np.vstack((a,b))#合并數(shù)組 [[ 1. 1.] [ 1. 1.] [ 1. 0.] [ 0. 1.]] >>> print np.hstack((a,b))#合并數(shù)組 [[ 1. 1. 1. 0.] [ 1. 1. 0. 1.]] >>> print a.transpose() #轉(zhuǎn)置
numpy.linalg模塊中有很多關(guān)于矩陣運(yùn)算的方法:
>>> import numpy.linalg as nplg
NumPy中的基本數(shù)據(jù)類型
名稱 | 描述 |
---|---|
bool | 用一個(gè)字節(jié)存儲(chǔ)的布爾類型(True或False) |
inti | 由所在平臺(tái)決定其大小的整數(shù)(一般為int32或int64) |
int8/16/32/64 | 整數(shù),1/2/4/8個(gè)字節(jié)大小 |
uint8/16/32/64 | 無符號(hào)整數(shù) |
float16/32/64 | 半/單/雙精度浮點(diǎn)數(shù),16/32/64位,指數(shù)、精度也不同 |
complex64/128 | 復(fù)數(shù),分別用兩個(gè)32/64位浮點(diǎn)數(shù)表示實(shí)部和虛部 |
輸出數(shù)組
當(dāng)輸出一個(gè)數(shù)組時(shí),NumPy以特定的布局用類似嵌套列表的形式顯示:
- 第一行從左到右輸出
- 每個(gè)切片通過一個(gè)空行與下一個(gè)隔開
- 一維數(shù)組被打印成行,二維數(shù)組成矩陣,三維數(shù)組成矩陣列表。
- 如果一個(gè)數(shù)組太長(zhǎng),則NumPy自動(dòng)省略中間部分而只打印兩端的數(shù)據(jù):
>>> a = arange(6) # 1d array >>> print a [0 1 2 3 4 5] >>> b = arange(12).reshape(4,3) # 2d array >>> print b [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] >>> c = arange(24).reshape(2,3,4)# 3d array >>> print c [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]]
總結(jié)
到此這篇關(guān)于python基礎(chǔ)之Numpy庫中array用法的文章就介紹到這了,更多相關(guān)python Numpy中array用法內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。