1.Numpy是什么
很简单,Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy、matplotlib一起使用。其实,list已经提供了类似于矩阵的表示形式,不过numpy为我们提供了更多的函数。如果接触过matlab、scilab,那么numpy很好入手。 在以下的代码示例中,总是先导入了numpy:(通用做法import numpu as np 简单输入)
>>> import numpy as np >>> print np.version.version 1.6.2
2. 多维数组
多维数组的类型是:numpy.ndarray。
使用numpy.array方法
以list或tuple变量为参数产生一维数组:
>>> 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))) <type 'numpy.ndarray'>
以list或tuple变量为元素产生二维数组或者多维数组:
>>> x = np.array(((1,2,3),(4,5,6)))
>>> x
array([[1, 2, 3],
[4, 5, 6]])
>>> y = np.array([[1,2,3],[4,5,6]])
>>> y
array([[1, 2, 3],
[4, 5, 6]])
index 和slicing :第一数值类似数组横坐标,第二个为纵坐标
>>> x[1,2] 6 >>> y=x[:,1] >>> y array([2, 5])
涉及改变相关问题,我们改变上面y是否会改变x?这是特别需要关注的!
>>> y
array([2, 5])
>>> y[0] = 10
>>> y
array([10, 5])
>>> x
array([[ 1, 10, 3],
[ 4, 5, 6]])
通过上面可以发现改变y会改变x ,因而我们可以推断,y和x指向是同一块内存空间值,系统没有为y 新开辟空间把x值赋值过去。
使用numpy.arange方法
>>> print np.arange(15) [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] >>> print type(np.arange(15)) <type 'numpy.ndarray'> >>> 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)) <type 'numpy.ndarray'>
使用numpy.linspace方法
例如,在从1到10中产生20个数:
>>> print np.linspace(1,10,20) [ 1. 1.47368421 1.94736842 2.42105263 2.89473684 3.36842105 3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632 6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158 9.52631579 10. ]
使用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.]]
获取数组的属性:
>>> 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
The following attributes contain information about the memory layout of the array:
| ndarray.flags | Information about the memory layout of the array. |
| ndarray.shape | Tuple of array dimensions. |
| ndarray.strides | Tuple of bytes to step in each dimension when traversing an array. |
| ndarray.ndim | Number of array dimensions. |
| ndarray.data | Python buffer object pointing to the start of the array’s data. |
| ndarray.size | Number of elements in the array. |
| ndarray.itemsize | Length of one array element in bytes. |
| ndarray.nbytes | Total bytes consumed by the elements of the array. |
| ndarray.base | Base object if memory is from some other object. |
An ndarray object has many methods which operate on or with the array in some fashion, typically returning an array result. These methods are briefly explained below. (Each method’s docstring has a more complete description.)
For the following methods there are also corresponding functions in numpy: all, any, argmax, argmin, argpartition, argsort, choose, clip,compress, copy, cumprod, cumsum, diagonal, imag, max, mean, min, nonzero, partition, prod, ptp, put, ravel, real, repeat, reshape, round,searchsorted, sort, squeeze, std, sum, swapaxes, take, trace, transpose, var.
更多Array的相关方法见:http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
用到比较多函数示例:
>>> x
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
>>> x.sum(axis=1)
array([[ 9, 12, 15],
[36, 39, 42],
[63, 66, 69]])
>>> x.sum(axis=2)
array([[ 3, 12, 21],
[30, 39, 48],
[57, 66, 75]])>>> np.sum([[0, 1], [0, 5]]) 6 >>> np.sum([[0, 1], [0, 5]], axis=0) array([0, 6]) >>> np.sum([[0, 1], [0, 5]], axis=1) array([1, 5])
合并数组
使用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 = 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]]
numpy.linalg模块中有很多关于矩阵运算的方法:
特征值、特征向量:
>>> a = np.array([[1,0],[2,3]])
>>> nplg.eig(a)
(array([ 3., 1.]), array([[ 0. , 0.70710678],
[ 1. , -0.70710678]]))
原文地址:http://blog.csdn.net/huruzun/article/details/39801217