转载请注明出处:http://blog.csdn.net/l1028386804/article/details/78945157
新年新气象,2018年的第一天,给大家带来一篇Python数据分析与挖掘领域中很重要的一个类库——Numpy,那么,我们就一起进入正题吧。
>>> import numpy as np
>>> print np.version.version
1.6.2
多维数组的类型是:numpy.ndarray。
>>> print np.array([1,2,3,4])
[1 2 3 4]
>>> print np.array((1.2,2,3,4))
[ 1.2 2. 3. 4. ]
>>> print type(np.array((1.2,2,3,4)))
以list或tuple变量为元素产生二维数组:
>>> print np.array([[1,2],[3,4]])
[[1 2]
[3 4]]
生成数组的时候,可以指定数据类型,例如numpy.int32, numpy.int16, and numpy.float64等:
>>> print np.array((1.2,2,3,4), dtype=np.int32)
[1 2 3 4]
>>> print np.arange(15)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
>>> print type(np.arange(15))
>>> print np.arange(15).reshape(3,5)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
>>> print type(np.arange(15).reshape(3,5))
例如,在从1到3中产生9个数:
>>> print np.linspace(1,3,9)
[ 1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. ]
>>> print np.zeros((3,4))
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
>>> print np.ones((3,4))
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
>>> print np.eye(3)
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
创建一个三维数组:
>>> print np.zeros((2,2,2))
[[[ 0. 0.]
[ 0. 0.]]
[[ 0. 0.]
[ 0. 0.]]]
>>> a = np.zeros((2,2,2))
>>> print a.ndim #数组的维数
3
>>> print a.shape #数组每一维的大小
(2, 2, 2)
>>> print a.size #数组的元素数
8
>>> print a.dtype #元素类型
float64
>>> print a.itemsize #每个元素所占的字节数
8
示例:
>>> a = np.array( [[2,3,4],[5,6,7]] )
>>> print a
[[2 3 4]
[5 6 7]]
>>> print a[1,2]
7
>>> print a[1,:]
[5 6 7]
>>> print a[1,1:2]
[6]
>>> a[1,:] = [8,9,10]
>>> print a
[[ 2 3 4]
[ 8 9 10]]
>>> for x in np.linspace(1,3,3):
... print x
...
1.0
2.0
3.0
先构造数组a、b:
>>> 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*2)**4
[[ 16. 16.]
[ 16. 16.]]
使用数组对象自带的方法:
>>> 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)
array([[ 2.71828183, 2.71828183],
[ 2.71828183, 2.71828183]])
>>> np.dot(a,a) ##矩阵乘法
array([[ 2., 2.],
[ 2., 2.]])
使用numpy下的vstack和hstack函数
>>> a = np.ones((2,2))
>>> b = np.eye(2)
>>> 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.]]
看一下这两个函数有没有涉及到浅拷贝这种问题:
>>> c = np.hstack((a,b))
>>> print c
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]
>>> a[1,1] = 5
>>> b[1,1] = 5
>>> print c
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]
可以看到,a、b中元素的改变并未影响c。
数组对象自带了浅拷贝和深拷贝的方法,但是一般用深拷贝多一些:
>>> a = np.ones((2,2))
>>> b = a
>>> b is a
True
>>> c = a.copy() #深拷贝
>>> c is a
False
>>> a = np.array([[1,0],[2,3]])
>>> print a
[[1 0]
[2 3]]
>>> print a.transpose()
[[1 2]
[0 3]]
>>> print np.trace(a)
4
numpy.linalg模块中有很多关于矩阵运算的方法:
>>> import numpy.linalg as nplg
特征值、特征向量:
>>> print nplg.eig(a)
(array([ 3., 1.]), array([[ 0. , 0.70710678],
[ 1. , -0.70710678]]))
>>> import numpy as np
>>> a = np.array([2,3,4])
>>> a
array([2, 3, 4])
>>> a.dtype
dtype('int64')
>>> b = np.array([1.2, 3.5, 5.1])
>>> b.dtype
dtype('float64')
二维的数组
>>> b = np.array([(1.5,2,3), (4,5,6)])
>>> b
array([[ 1.5, 2. , 3. ],
[ 4. , 5. , 6. ]])
创建时指定类型
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
>>> c
array([[ 1.+0.j, 2.+0.j],
[ 3.+0.j, 4.+0.j]])
创建一些特殊的矩阵
>>> np.zeros( (3,4) )
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
>>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified
array([[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]],
[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]]], dtype=int16)
>>> np.empty( (2,3) ) # uninitialized, output may vary
array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260],
[ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
创建一些有特定规律的矩阵
>>> np.arange( 10, 30, 5 )
array([10, 15, 20, 25])
>>> np.arange( 0, 2, 0.3 ) # it accepts float arguments
array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
>>> from numpy import pi
>>> np.linspace( 0, 2, 9 ) # 9 numbers from 0 to 2
array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
>>> x = np.linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points
>>> f = np.sin(x)
加减乘除三角函数逻辑运算
>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> b
array([0, 1, 2, 3])
>>> c = a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> 10*np.sin(a)
array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])
>>> a<35
array([ True, True, False, False], dtype=bool)
矩阵运算
>>> import numpy as np
>>> A = np.arange(10,20)
>>> B = np.arange(20,30)
>>> A + B
array([30, 32, 34, 36, 38, 40, 42, 44, 46, 48])
>>> A * B
array([200, 231, 264, 299, 336, 375, 416, 459, 504, 551])
>>> A / B
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> B / A
array([2, 1, 1, 1, 1, 1, 1, 1, 1, 1])
如果需要执行矩阵运算,一般就是矩阵的乘法运算
>>> A = np.array([1,1,1,1])
>>> B = np.array([2,2,2,2])
>>> A.reshape(2,2)
array([[1, 1],
[1, 1]])
>>> B.reshape(2,2)
array([[2, 2],
[2, 2]])
>>> A * B
array([2, 2, 2, 2])
>>> np.dot(A,B)
8
>>> A.dot(B)
8
一些常用的全局函数
>>> B = np.arange(3)
>>> B
array([0, 1, 2])
>>> np.exp(B)
array([ 1. , 2.71828183, 7.3890561 ])
>>> np.sqrt(B)
array([ 0. , 1. , 1.41421356])
>>> C = np.array([2., -1., 4.])
>>> np.add(B, C)
array([ 2., 0., 6.])
>>> a = np.arange(10)**3
>>> a
array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
>>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729])
>>> a[ : :-1] # reversed a
array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000])
>>> for i in a:
... print(i**(1/3.))
...
nan
1.0
nan
3.0
nan
5.0
6.0
7.0
8.0
9.0
矩阵的遍历
>>> import numpy as np
>>> b = np.arange(16).reshape(4, 4)
>>> for row in b:
... print(row)
...
[0 1 2 3]
[4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
>>> for node in b.flat:
... print(node)
...
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
改变矩阵形状--reshape
>>> a = np.floor(10 * np.random.random((3,4)))
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.ravel()
array([ 6., 5., 1., 5., 5., 5., 8., 9., 5., 5., 9., 7.])
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
resize和reshape的区别
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.reshape(2,-1)
array([[ 6., 5., 1., 5., 5., 5.],
[ 8., 9., 5., 5., 9., 7.]])
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.resize(2,6)
>>> a
array([[ 6., 5., 1., 5., 5., 5.],
[ 8., 9., 5., 5., 9., 7.]])
矩阵的合并
>>> a = np.floor(10*np.random.random((2,2)))
>>> a
array([[ 8., 8.],
[ 0., 0.]])
>>> b = np.floor(10*np.random.random((2,2)))
>>> b
array([[ 1., 8.],
[ 0., 4.]])
>>> np.vstack((a,b))
array([[ 8., 8.],
[ 0., 0.],
[ 1., 8.],
[ 0., 4.]])
>>> np.hstack((a,b))
array([[ 8., 8., 1., 8.],
[ 0., 0., 0., 4.]])