目錄
- 前言
- 為什么要用numpy
- 數(shù)組的創(chuàng)建
- 獲取數(shù)組的屬性
- 數(shù)組索引,切片,賦值
- 總結(jié)
前言
Numpy是Python的一個科學(xué)計算的庫,提供了矩陣運(yùn)算的功能,其一般與Scipy、matplotlib一起使用。其實,list已經(jīng)提供了類似于矩陣的表示形式,不過numpy為我們提供了更多的函數(shù)。
NumPy數(shù)組是一個多維數(shù)組對象,稱為ndarray。數(shù)組的下標(biāo)從0開始,同一個NumPy數(shù)組中所有元素的類型必須是相同的。
為什么要用numpy
Python中提供了list容器,可以當(dāng)作數(shù)組使用。但列表中的元素可以是任何對象,因此列表中保存的是對象的指針,這樣一來,為了保存一個簡單的列表[1,2,3]。就需要三個指針和三個整數(shù)對象。對于數(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)建一個新的 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(最小值,最大值,步長)(左閉右開) : 創(chuàng)建等差數(shù)列
linspace(最小值,最大值,元素數(shù)量)
logspace(開始值, 終值, 元素個數(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.5 1.75 2. 2.25 2.5 2.75 3. ]
生成特殊數(shù)組
np.ones: 創(chuàng)建一個數(shù)組, 其中的元素全為 1
np.zeros: 創(chuàng)建元素全為 0 的數(shù)組, 類似 np.ones
np.empty創(chuàng)建一個內(nèi)容隨機(jī)并且依賴與內(nèi)存狀態(tài)的數(shù)組。
np.eye: 創(chuàng)建一個對角線為 1 其他為 0 的矩陣.
np.identity: 創(chuàng)建一個主對角線為 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 #每個元素所占的字節(jié)數(shù)
8
數(shù)組索引,切片,賦值
‘…'符號表示將所有未指定索引的維度均賦為 ‘:'
‘:'在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ù)組加,對應(yīng)位置相加
[[ 2. 1.]
[ 1. 2.]]
>>> print a-b #數(shù)組減,對應(yīng)位置相減
[[ 0. 1.]
[ 1. 0.]]
>>> print b*2 #數(shù)組與數(shù)值相乘,對應(yīng)位置乘
[[ 2. 0.]
[ 0. 2.]]
>>> print (a*2)*(b*2) #數(shù)組與數(shù)組相乘,按位置一對一相乘
[[ 4. 0.]
[ 0. 4.]]
>>> print b/(a*2) #數(shù)組與數(shù)組相除,按位置一對一相除
[[ 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),同時可以使用axis指定對哪一維進(jìn)行操作:
>>> a.sum()
4.0
>>> a.sum(axis=0) #計算每一列(二維數(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 |
用一個字節(jié)存儲的布爾類型(True或False) |
inti |
由所在平臺決定其大小的整數(shù)(一般為int32或int64) |
int8/16/32/64 |
整數(shù),1/2/4/8個字節(jié)大小 |
uint8/16/32/64 |
無符號整數(shù) |
float16/32/64 |
半/單/雙精度浮點(diǎn)數(shù),16/32/64位,指數(shù)、精度也不同 |
complex64/128 |
復(fù)數(shù),分別用兩個32/64位浮點(diǎn)數(shù)表示實部和虛部 |
輸出數(shù)組
當(dāng)輸出一個數(shù)組時,NumPy以特定的布局用類似嵌套列表的形式顯示:
- 第一行從左到右輸出
- 每個切片通過一個空行與下一個隔開
- 一維數(shù)組被打印成行,二維數(shù)組成矩陣,三維數(shù)組成矩陣列表。
- 如果一個數(shù)組太長,則NumPy自動省略中間部分而只打印兩端的數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python中找出numpy array數(shù)組的最值及其索引方法
- Python中的二維數(shù)組實例(list與numpy.array)
- 基于Python Numpy的數(shù)組array和矩陣matrix詳解
- python中利用numpy.array()實現(xiàn)倆個數(shù)值列表的對應(yīng)相加方法
- 對python numpy.array插入一行或一列的方法詳解
- Python中Numpy ndarray的使用詳解
- python實現(xiàn)list由于numpy array的轉(zhuǎn)換
- Python numpy.array()生成相同元素數(shù)組的示例
- Python 實現(xiàn)Numpy中找出array中最大值所對應(yīng)的行和列
- Python 獲取numpy.array索引值的實例