import numpy as np
1. 多维数组:类型 numpy.ndarray
方法:numpy.array(tuple[,dtype]): # tuple 可以是由 tuple 组成的;dtype 可以取 numpy.int32/.int16/.float64 对 tuple 的元素指定类型。 返回ndarray类型对象。>>> np.array([1.2,2,3,4],dtype = np.int32)
array([1, 2, 3, 4])
numpy.arange(num): # 返回一个 包含 0~num-1 num个元素的 int型一维数组。
>>> print np.arange(15).reshape(3,5) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]]numpy.linspace(n1,n2,i): #从 [n1,n2]中等距的找出 i 个数组成数组
>>> print np.linspace(1,3,9)
[ 1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. ]
numpy.zeros,numpy.ones,numpy.eye #等方法可以构造特定的矩阵
>>> 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((3,4,5))
[[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]]
>>> a = np.zeros((3,4,5))
>>> a
array([[[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]],
[[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]],
[[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]])
print a.ndim #数组的维数
>>> print a.ndim
3
print a.shape #数组每一维的大小
>>> print a.shape
(3L, 4L, 5L)
print a.size #数组的元素数
>>> print a.size
60
print a.dtype #元素类型
>>> print a.dtype
float64
a.itemsize #每个元素所占的字节数
>>> print a.itemsize
8
2.数组索引,切片,赋值: #也可以使用C中的下标
>>> a = np.array( [[2,3,4],[5,6,7]] )
>>> print a
[[2 3 4]
[5 6 7]]
a[m,n]索引
>>> a[1][2]
7
>>> a[1,2]
7
>>> a[1,:]
array([5, 6, 7])
>>> a[1,]
array([5, 6, 7])
>>> a[1]
array([5, 6, 7])
>>> a[1][:]
array([5, 6, 7])
>>> a[1,1:2]
array([6])
>>> a[1][1:2]
array([6])
>>> a[1,:] = [8,9,10]
>>> a
array([[ 2, 3, 4],
[ 8, 9, 10]])
>>> a[1][:] = [1,1,1]
>>> a
array([[2, 3, 4],
[1, 1, 1]])
>>> a[1,:] = [8,9,10,11]
>>> a
ValueError: cannot copy sequence with size 4 to array axis with dimension 3
>>> for x in a:
... print x
...
[2 3 4]
[1 1 1]
4.四则运算中位置对应的元素进行运算
#参与运算的矩阵必须有一样的形式>>> 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 = np.array([[1,2],[3,4],[5,6]])
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> a.sum()
21
>>> a.sum(axis=0) #计算每一列的元素和
array([ 9, 12])
>>> a.sum(axis=1) #计算每一行的元素和
array([ 3, 7, 11])
>>> a.min()
1
>>> a.max()
6
>>> a = np.array([[1,2],[3,4],[5,6]])
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.sin(a) #求sin(x)
array([[ 0.84147098, 0.90929743],
[ 0.14112001, -0.7568025 ],
[-0.95892427, -0.2794155 ]])
>>> np.sin(1/2)
0.0
>>> np.max(a)
6
>>> np.floor(a) #对每个元素向下取整
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
>>> np.floor(3.7)
3.0
>>> np.exp(a) #求ln(x)
array([[ 2.71828183, 7.3890561 ],
[ 20.08553692, 54.59815003],
[ 148.4131591 , 403.42879349]])
>>> np.exp(0)
1.0
>>> np.dot(a,a.transpose()) #进行矩阵的乘法
array([[ 5, 11, 17],
[11, 25, 39],
[17, 39, 61]])
合并数组
>>> a = np.ones((2,2))
>>> b = np.eye(2)
>>> a
array([[ 1., 1.],
[ 1., 1.]])
>>> b
array([[ 1., 0.],
[ 0., 1.]])
>>> np.vstack((a,b)).shape
(4L, 2L)
>>> np.hstack((a,b)).shape
(2L, 4L)
>>> np.vstack((a,b)) #将两个等列的矩阵合并。
array([[ 1., 1.],
[ 1., 1.],
[ 1., 0.],
[ 0., 1.]])
>>> np.hstack((a,b)) #两个等行的矩阵,将后一个的每一行接到第一个的后面。
array([[ 1., 1., 1., 0.],
[ 1., 1., 0., 1.]])
>>> c = np.hstack((a,b))
>>> c
array([[ 1., 1., 1., 0.],
[ 1., 1., 0., 1.]])
浅拷贝
>>> a[1,1] = 5
>>> b[1,1] = 5
>>> c
array([[ 1., 1., 1., 0.],
[ 1., 1., 0., 1.]])
>>> a = np.ones((2,2))
>>> b = a #为a建立新的引用b,b是a的别称
>>> a
array([[ 1., 1.],
[ 1., 1.]])
>>> b
array([[ 1., 1.],
[ 1., 1.]])
>>> a is b
>>> True
深拷贝
>>> c = a.copy() #创建了新的对象c,对c的操作和a无关
>>> c
array([[ 1., 1.],
[ 1., 1.]])
>>> a is c
False
8.矩阵的运算:
>>> a = np.array([[1,0],[2,3]])
>>> a
array([[1, 0],
[2, 3]])
>>>
>>> a.transpose()
array([[1, 2],
[0, 3]])
>>> a = np.array([[1,2,3,4],[2,3,4,5]])
>>> a
array([[1, 2, 3, 4],
[2, 3, 4, 5]])
>>> a.trace()
4
>>> for x in nplg.eig(a):
... print x
...
[ 3. 1.]
[[ 0. 0.70710678]
[ 1. -0.70710678]]