Python中用列表(list)保存一組值,能夠用來看成數組使用,不過因爲列表的元素能夠是任何對象,所以列表中所保存的是對象的指針。這樣爲了保存一個簡單的[1,2,3],須要有3個指針和三個整數對象。對於數值運算來講這種結構顯然比較浪費內存和CPU計算時間。此外Python還提供了一個array模塊,array對象和列表不一樣,它直接保存數值,和C語言的一維數組比較相似。可是因爲它不支持多維,也沒有各類運算函數,所以也不適合作數值運算。html
NumPy提供了兩種基本的對象:ndarray(N-dimensional array object)和 ufunc(universal function object)。ndarray(下文統一稱之爲數組)是存儲單一數據類型的多維數組,而ufunc則是可以對數組進行處理的函數。python
>>> import numpy as np
>>> np.zeros(5) //一維 array([ 0., 0., 0., 0., 0.]) >>> np.zeros((5,2))//二維 array([[ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.]])
>>> np.zeros((5,2,2))//三維
array([[[ 0., 0.],
[ 0., 0.]],數組
[[ 0., 0.],
[ 0., 0.]],dom
[[ 0., 0.],
[ 0., 0.]],函數
[[ 0., 0.],
[ 0., 0.]],spa
[[ 0., 0.],
[ 0., 0.]]]).net
>>> import numpy as np
>>> np.ones(10) //一維 array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) >>> np.ones(10,dtype="int32")//一維 array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
>>> np.ones((4,1)) //二維
array([[ 1.],
[ 1.],
[ 1.],
[ 1.]])指針
import numpy as np
d = np.arange(0,10,1) e = np.arange(0,10,2) print (d) #---------------------------------- [0 1 2 3 4 5 6 7 8 9] [0 2 4 6 8]
f = np.linspace(0,10,11,endpoint=False) print (f) #---------------------------------------- [ 0. 0.90909091 1.81818182 2.72727273 3.63636364 4.54545455 5.45454545 6.36363636 7.27272727 8.18181818 9.09090909]
g = np.logspace(0,2,20) print (g) [ 1. 1.27427499 1.62377674 2.06913808 2.6366509 3.35981829 4.2813324 5.45559478 6.95192796 8.8586679 11.28837892 14.38449888 18.32980711 23.35721469 29.76351442 37.92690191 48.32930239 61.58482111 78.47599704 100. ]
s= "abcdefgh" sa = np.fromstring(s,dtype = np.int8) print (sa) #-------------------------------------- [ 97 98 99 100 101 102 103 104]
def fun(i,j): return (i+1)*(j+1) fa = np.fromfunction(fun,(9,9)) #(9,9)表示數組的shape,傳給fun的書是每一個元素的定位,有81個位置,能夠獲得81個元素 print (fa) #--------------------------------------------------- [[ 1. 2. 3. 4. 5. 6. 7. 8. 9.] [ 2. 4. 6. 8. 10. 12. 14. 16. 18.] [ 3. 6. 9. 12. 15. 18. 21. 24. 27.] [ 4. 8. 12. 16. 20. 24. 28. 32. 36.] [ 5. 10. 15. 20. 25. 30. 35. 40. 45.] [ 6. 12. 18. 24. 30. 36. 42. 48. 54.] [ 7. 14. 21. 28. 35. 42. 49. 56. 63.] [ 8. 16. 24. 32. 40. 48. 56. 64. 72.] [ 9. 18. 27. 36. 45. 54. 63. 72. 81.]]
import numpy as np a = np.array([1,2,3,4,5]) b = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]]) print (a) print (b) #------------------------------------------- [1 2 3 4 5] [[ 1 2 3 4] [ 4 5 6 7] [ 7 8 9 10]]
>>> np.ones((3,2))
array([[ 1., 1.], [ 1., 1.], [ 1., 1.]]) >>> np.ones((3,2)).ndim 2 #二維
>>> np.ones((3,2)).size
6 #6個元素
print(a.dtype) # 數組的元素類型 int32,32bit整型數據
print(b.dtype) # 數組的元素類型 int32
aa = np.array([2,3,4,5,6],dtype = np.float) print (aa) #---------------------------------------------- [ 2. 3. 4. 5. 6.]
print(a.shape) #數組的大小 (5)
print(b.shape) #數組的大小 shape (3,4) #修改shape來修改數組軸的大小: b.shape = (4,3) print (b) #-------------------------------------- [[ 1 2 3] [ 4 4 5] [ 6 7 7] [ 8 9 10]] #若是某個軸的值爲-1,則會根據數組的總數計算此軸的長度。如b一共12個元素,修改shape b.shape = (2,-1) #那麼就會獲得一個2*6的數組 print (b) #-------------------------------------- [[ 1 2 3 4 4 5] [ 6 7 7 8 9 10]] b.shape = (6,-1) #那麼就會獲得一個6*2的數組 print (b) #-------------------------------------- [[ 1 2] [ 3 4] [ 4 5] [ 6 7] [ 7 8] [ 9 10]]
c = a.reshape((5,1)) #此方法實驗證實:只能是x*y=數組的總元素才能夠,這裏1*5只能換成5*1 print (c) #此時a的結構並沒改變,a,c共享內存。 print (a) #-------------------------------------- [[1] [2] [3] [4] [5]] [1 2 3 4 5] #修改a[1][2]的值 a[2] = 100 print (c) #此時a的結構並沒改變,a,c共享內存。 print (a) #-------------------------------------- [1 2 3 4 5] [[ 1] [ 2] [100] [ 4] [ 5]] [ 1 2 100 4 5]
>>> import numpy as np
>>> np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]]) array([[ 1, 2, 3, 4], [ 4, 5, 6, 7], [ 7, 8, 9, 10]]) >>> b = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]]) >>> b[0] array([1, 2, 3, 4]) >>> b[1] array([4, 5, 6, 7]) >>> b[1,2] 6 >>> b[1,3] 7 >>> b[1,-1] 7 >>> b[-1] array([ 7, 8, 9, 10]) >>> b[-1,2] 9 >>> b[-1,-2] 9
>>> b[:-2] #0--負2列 array([[1, 2, 3, 4]]) >>> b[1:2] array([[4, 5, 6, 7]]) >>> b[1:3] array([[ 4, 5, 6, 7], [ 7, 8, 9, 10]])
#*************矩陣的截取***********************
>>> a=np.mat(np.random.randint(2,15,size=(3,3)))
>>> a
matrix([[ 4, 10, 14],
[11, 3, 12],
[ 4, 2, 12]])
>>> a[1:,1:,]
matrix([[ 3, 12],
[ 2, 12]])code
numpy庫提供了matrix類,使用matrix類建立的是矩陣對象,它們的加減乘除運算缺省採用矩陣方式計算。可是因爲NumPy中同時存在ndarray和matrix對象,所以很容易將二者弄混。htm
#利用ones()建立一個2*4的全1矩陣
>>> np.mat(np.ones((2,4)))
matrix([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
#用numpy的隨機數rand產生一個2*2的隨機數組並轉化成矩陣
>>> np.mat(np.random.rand(2,2))
matrix([[ 0.4340437 , 0.98055453],
[ 0.52937992, 0.81452857]])
#產生一個2-8之間的整數數組大小是2*5,再轉換成矩陣。
>>> np.mat(np.random.randint(2,8,size=(2,5)))
matrix([[3, 6, 4, 4, 5],
[3, 7, 7, 2, 3]])
#eye()函數產生單位對角數組,轉換成單位對角陣
>>> np.mat(np.eye(2,2,dtype=int))
matrix([[1, 0], [0, 1]]) >>> np.mat(np.eye(3,2,dtype=int)) matrix([[1, 0], [0, 1], [0, 0]]) >>> np.mat(np.eye(3,3,dtype=int)) matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
#將一維數組轉換成對角陣
>>> np.mat(np.diag([1,2,3]))
matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]]) >>>
>>> import numpy as np
>>> a = np.matrix([[1,2,3],[5,5,6],[7,9,9]]) >>> a matrix([[1, 2, 3], [5, 5, 6], [7, 9, 9]]) >>> a**-1 #求逆 a.I也是a的逆 matrix([[-0.6 , 0.6 , -0.2 ], [-0.2 , -0.8 , 0.6 ], [ 0.66666667, 0.33333333, -0.33333333]]) >>> a*a**-1 #a乘a的逆,矩陣內積 matrix([[ 1.00000000e+00, 1.66533454e-16, -1.11022302e-16], [ 0.00000000e+00, 1.00000000e+00, -4.44089210e-16], [ 4.44089210e-16, 5.55111512e-17, 1.00000000e+00]])
>>> a.T #a的轉置 matrix([[1, 5, 7], [2, 5, 9], [3, 6, 9]]) >>>
>>> from numpy import linalg as ll
>>> ll.inv(a) #求逆
matrix([[-0.6 , 0.6 , -0.2 ], [-0.2 , -0.8 , 0.6 ], [ 0.66666667, 0.33333333, -0.33333333]]) >>> a matrix([[1, 2, 3], [5, 5, 6], [7, 9, 9]])