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