目錄
- 前言
- 為什么要用numpy
- 數組的創建
- 獲取數組的屬性
- 數組索引,切片,賦值
- 總結
前言
Numpy是Python的一個科學計算的庫,提供了矩陣運算的功能,其一般與Scipy、matplotlib一起使用。其實,list已經提供了類似于矩陣的表示形式,不過numpy為我們提供了更多的函數。
NumPy數組是一個多維數組對象,稱為ndarray。數組的下標從0開始,同一個NumPy數組中所有元素的類型必須是相同的。
為什么要用numpy
Python中提供了list容器,可以當作數組使用。但列表中的元素可以是任何對象,因此列表中保存的是對象的指針,這樣一來,為了保存一個簡單的列表[1,2,3]。就需要三個指針和三個整數對象。對于數值運算來說,這種結構顯然不夠高效。
Python雖然也提供了array模塊,但其只支持一維數組,不支持多維數組(在TensorFlow里面偏向于矩陣理解),也沒有各種運算函數。因而不適合數值運算。
NumPy的出現彌補了這些不足。
數組的創建
使用numpy.array方法將tuple和list, array, 或者其他的序列模式的數據轉創建為 ndarray, 默認創建一個新的 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)
#指定數組中元素的類型
>>> c
array([[ 1.+0.j, 2.+0.j],
[ 3.+0.j, 4.+0.j]])
生成均勻分布的array:
arange(最小值,最大值,步長)(左閉右開) : 創建等差數列
linspace(最小值,最大值,元素數量)
logspace(開始值, 終值, 元素個數): 創建等比數列
>>> 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. ]
生成特殊數組
np.ones: 創建一個數組, 其中的元素全為 1
np.zeros: 創建元素全為 0 的數組, 類似 np.ones
np.empty創建一個內容隨機并且依賴與內存狀態的數組。
np.eye: 創建一個對角線為 1 其他為 0 的矩陣.
np.identity: 創建一個主對角線為 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.]]
獲取數組的屬性
>>> a = np.zeros((2,2,2))
>>> a.ndim #數組的維數
3
>>> a.shape #數組每一維的大小
(2, 2, 2)
>>> a.size #數組全部元素的數量
8
>>> a.dtype #數組中元素的類型
float64
>>> print a.itemsize #每個元素所占的字節數
8
數組索引,切片,賦值
‘…'符號表示將所有未指定索引的維度均賦為 ‘:'
‘:'在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]])
數組操作
>>> 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 #數組加,對應位置相加
[[ 2. 1.]
[ 1. 2.]]
>>> print a-b #數組減,對應位置相減
[[ 0. 1.]
[ 1. 0.]]
>>> print b*2 #數組與數值相乘,對應位置乘
[[ 2. 0.]
[ 0. 2.]]
>>> print (a*2)*(b*2) #數組與數組相乘,按位置一對一相乘
[[ 4. 0.]
[ 0. 4.]]
>>> print b/(a*2) #數組與數組相除,按位置一對一相除
[[ 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
內置函數(min,max,sum),同時可以使用axis指定對哪一維進行操作:
>>> a.sum()
4.0
>>> a.sum(axis=0) #計算每一列(二維數組中類似于矩陣的列)的和
array([ 2., 2.])
>>> a.min() #數組最小值
1.0
>>> a.max() #數組最大值
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)) #合并數組
[[ 1. 1.]
[ 1. 1.]
[ 1. 0.]
[ 0. 1.]]
>>> print np.hstack((a,b)) #合并數組
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]
>>> print a.transpose() #轉置
numpy.linalg模塊中有很多關于矩陣運算的方法:
>>> import numpy.linalg as nplg
NumPy中的基本數據類型
名稱 |
描述 |
bool |
用一個字節存儲的布爾類型(True或False) |
inti |
由所在平臺決定其大小的整數(一般為int32或int64) |
int8/16/32/64 |
整數,1/2/4/8個字節大小 |
uint8/16/32/64 |
無符號整數 |
float16/32/64 |
半/單/雙精度浮點數,16/32/64位,指數、精度也不同 |
complex64/128 |
復數,分別用兩個32/64位浮點數表示實部和虛部 |
輸出數組
當輸出一個數組時,NumPy以特定的布局用類似嵌套列表的形式顯示:
- 第一行從左到右輸出
- 每個切片通過一個空行與下一個隔開
- 一維數組被打印成行,二維數組成矩陣,三維數組成矩陣列表。
- 如果一個數組太長,則NumPy自動省略中間部分而只打印兩端的數據:
>>> 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]]]
總結
到此這篇關于python基礎之Numpy庫中array用法的文章就介紹到這了,更多相關python Numpy中array用法內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python中找出numpy array數組的最值及其索引方法
- Python中的二維數組實例(list與numpy.array)
- 基于Python Numpy的數組array和矩陣matrix詳解
- python中利用numpy.array()實現倆個數值列表的對應相加方法
- 對python numpy.array插入一行或一列的方法詳解
- Python中Numpy ndarray的使用詳解
- python實現list由于numpy array的轉換
- Python numpy.array()生成相同元素數組的示例
- Python 實現Numpy中找出array中最大值所對應的行和列
- Python 獲取numpy.array索引值的實例