Python中的Numpy入门教程

NumPy系统是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix))。据说NumPy将Python相当于变成一种免费的更强大的MatLab系统。

一个用python实现的科学计算包。包括:1、一个强大的N维数组对象Array;2、比较成熟的(广播)函数库;3、用于整合C/C++和Fortran代码的工具包;4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。 NumPy(Numeric Python)提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。多为很多大型金融公司使用,以及核心的科学计算组织如:Lawrence Livermore,NASA用其处理一些本来使用C++,Fortran或Matlab等所做的任务。

Python中的Numpy入门教程

numpy.org

1、Numpy是什么

很简单,Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy、matplotlib一起使用。其实,list已经提供了类似于矩阵的表示形式,不过numpy为我们提供了更多的函数。如果接触过matlab、scilab,那么numpy很好入手。 在以下的代码示例中,总是先导入了numpy:

>>> import numpy as np
>>> print np.version.version
1.8.0rc1

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,3,4)))
<type 'numpy.ndarray'>
>>> print np.array([[1,2],[3,4]])
[[1 2]
[3 4]]
>>> print np.array((1.2,3,4,2),dtype=np.int32)
[1 3 4 2]

使用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方法  »> 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((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

科学计算库Numpy

1.Numpy数组结构


>>> from numpy import *
>>> random.rand(4,4)
array([[ 0.19351968,  0.91315924,  0.17829807,  0.41819051],
[ 0.66942158,  0.16029611,  0.26684278,  0.9305511 ],
[ 0.34128194,  0.2255203 ,  0.23691202,  0.99558167],
[ 0.23876372,  0.39216221,  0.03952505,  0.56477796]])
>>> randMat = mat(random.rand(4,4))
>>> randMat.I
matrix([[ 1.16656052, -0.46796672,  0.64398992, -1.28181113],
[-1.52110114,  2.92250587,  1.59543781, -1.55497119],
[-0.49272369,  4.53397506, -1.40982884, -2.29753899],
[ 1.02498117, -4.64379637, -0.16860483,  3.84435249]])
>>> invRandMat = randMat.I
>>> randMat*invRandMat
matrix([[  1.00000000e+00,   0.00000000e+00,   0.00000000e+00,
2.22044605e-16],
[ -1.11022302e-16,   1.00000000e+00,   0.00000000e+00,
0.00000000e+00],
[ -5.55111512e-17,  -4.44089210e-16,   1.00000000e+00,
2.22044605e-16],
[  0.00000000e+00,  -6.66133815e-16,   0.00000000e+00,
1.00000000e+00]])
>>> eye(4)
array([[ 1.,  0.,  0.,  0.],
[ 0.,  1.,  0.,  0.],
[ 0.,  0.,  1.,  0.],
[ 0.,  0.,  0.,  1.]])
>>> 



>>> import numpy as np
>>> a = np.array([1,2,3,4])
>>> b = np.array([5,6,7,8])
>>> c = mp.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mp' is not defined
>>> c = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
>>> b
array([5, 6, 7, 8])
>>> c
array([[ 1,  2,  3,  4],
[ 4,  5,  6,  7],
[ 7,  8,  9, 10]])
>>> c.dtype
dtype('int64')
>>> a.shape
(4,)
>>> c.shape
(3, 4)
>>> c.shape = 4,3
>>> c
array([[ 1,  2,  3],
[ 4,  4,  5],
[ 6,  7,  7],
[ 8,  9, 10]])
>>> c.shape = 2,-1
>>> c
array([[ 1,  2,  3,  4,  4,  5],
[ 6,  7,  7,  8,  9, 10]])
>>> d = a.reshape((2,2))
>>> d
array([[1, 2],
[3, 4]])
>>> a
array([1, 2, 3, 4])
>>> a[1] = 100
>>> d
array([[  1, 100],
[  3,   4]])
>>> np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]],dtype=np.float)
array([[  1.,   2.,   3.,   4.],
[  4.,   5.,   6.,   7.],
[  7.,   8.,   9.,  10.]])
>>> np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]],dtype=np.complex)
array([[  1.+0.j,   2.+0.j,   3.+0.j,   4.+0.j],
[  4.+0.j,   5.+0.j,   6.+0.j,   7.+0.j],
[  7.+0.j,   8.+0.j,   9.+0.j,  10.+0.j]])
>>> np.arange(0,1,0.1)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])
>>> np.linspace(0,1,12)
array([ 0.        ,  0.09090909,  0.18181818,  0.27272727,  0.36363636,
0.45454545,  0.54545455,  0.63636364,  0.72727273,  0.81818182,
0.90909091,  1.        ])
>>> np.logspace(0,2,20)
array([   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"
>>> np.fromstring(s,dtype=np.int8)
array([ 97,  98,  99, 100, 101, 102, 103, 104], dtype=int8)
>>> np.fromstring(s,dtype=np.float)
array([  8.54088322e+194])
>>> def func(i)
File "<stdin>", line 1
def func(i)
^
SyntaxError: invalid syntax
>>> def func(i):
... return i%4+1
File "<stdin>", line 2
return i%4+1
^
IndentationError: expected an indented block
>>> return i%4+1;
File "<stdin>", line 1
SyntaxError: 'return' outside function
>>>     return i%4+1;
File "<stdin>", line 1
return i%4+1;
^
IndentationError: unexpected indent
>>> def func(i):
...     return i%4+1
... 
>>> np.fromfunction(func,(10,))
array([ 1.,  2.,  3.,  4.,  1.,  2.,  3.,  4.,  1.,  2.])
>>> def func2(i,j):
...     return (i+1)*(j+1)
... 
>>> a = np.fromfunction(func2,(9,9))
>>> a
array([[  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.]])
>>> 


2.数据基本操作


>>> a = np.arange(10)
>>> a[5]
5
>>> a[3:5]
array([3, 4])
>>> a[:5]
array([0, 1, 2, 3, 4])
>>> a[:-1]
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> a[2,4] = 100,101
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: too many indices
>>> a[2:4] = 100,101
>>> a
array([  0,   1, 100, 101,   4,   5,   6,   7,   8,   9])
>>> a[1:-1:2]
array([  1, 101,   5,   7])
>>> a[::-1]
array([  9,   8,   7,   6,   5,   4, 101, 100,   1,   0])
>>> a[5:1:-2]
array([  5, 101])
>>> b = a[3;7]
File "<stdin>", line 1
b = a[3;7]
^
SyntaxError: invalid syntax
>>> b = a[3:7]
>>> b
array([101,   4,   5,   6])
>>> b[2] = -10
>>> b
array([101,   4, -10,   6])
>>> a
array([  0,   1, 100, 101,   4, -10,   6,   7,   8,   9])
>>> x = np.arange(10,1,-1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[[3,3,1,8]]
array([7, 7, 9, 2])
>>> b = x[np.array([3,3,-3,8])]
>>> b[2]=100
>>> b
array([  7,   7, 100,   2])
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[[3,5,1]] = -1,-2,-3
>>> x
array([10, -3,  8, -1,  6, -2,  4,  3,  2])
>>> x = np.arange(5,0,-1)
>>> x
array([5, 4, 3, 2, 1])
>>> x[np.array[True,False,True,False,False,False]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
>>> x[np.array([True,False,True,False,False,False])]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many boolean indices
>>> x[np.array([True,False,True,False,False])]
array([5, 3])
>>> x[[True,False,True,False,False]]
array([4, 5, 4, 5, 5])
>>> x[np.array([True,False,True,True])]
array([5, 3, 2])
>>> x[np.array([True,False,True,True])] = -1,-2,-3
>>> x
array([-1,  4, -2, -3,  1])
>>> x = np.random.rand(10)
>>> x
array([ 0.08780116,  0.67866796,  0.55219757,  0.56943565,  0.83619672,
0.18465457,  0.71232327,  0.02118506,  0.96978577,  0.39900369])
>>> x>0.5
array([False,  True,  True,  True,  True, False,  True, False,  True, False], dtype=bool)
>>> x[x>0.5]
array([ 0.67866796,  0.55219757,  0.56943565,  0.83619672,  0.71232327,
0.96978577])
>>> np.arange(0,60,10).reshape(-1,1) + np.arange(0,6)
array([[ 0,  1,  2,  3,  4,  5],
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55]])
>>> a[(0,1,2,3,4),(1,2,3,4,5)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: too many indices for array
>>> a = np.arange(0,60,10).reshape(-1,1) + np.arange(0,6)
>>> a[(0,1,2,3,4),(1,2,3,4,5)]
array([ 1, 12, 23, 34, 45])
>>> a[3:,[0,2,5]]
array([[30, 32, 35],
[40, 42, 45],
[50, 52, 55]])
>>> 



3.矩阵操作

矩阵操作


>>> a = np.matrix([[1,2,3],[5,5,6],[7,9,9]])
>>> a*a**-1
matrix([[  1.00000000e+00,   0.00000000e+00,   0.00000000e+00],
[  4.44089210e-16,   1.00000000e+00,   4.44089210e-16],
[  0.00000000e+00,  -4.44089210e-16,   1.00000000e+00]])
>>> a = array([1,2,3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'array' is not defined
>>> a = aarray([1,2,3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'aarray' is not defined
>>> a = array([1,2,3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'array' is not defined
>>> a = np.array([1,2,3])
>>> a.reshape((-1,1))
array([[1],
[2],
[3]])
>>> a.reshape((1,-1))
array([[1, 2, 3]]) 





>>> m = np.mat([1,2,3])
>>> m
matrix([[1, 2, 3]])
>>> m[0]
matrix([[1, 2, 3]])
>>> m[0,1]
2
>>> list = [1,2,3]
>>> np.mat(list)
matrix([[1, 2, 3]])
>>> m1 = np.mat([1,2,3])
>>> m2 = np.mat([4,5,6])
>>> m1*m2.T
matrix([[32]])
>>> np.multiply(m1,m2)
matrix([[ 4, 10, 18]])
>>> m = np.mat([[2,5,1],[4,6,2]])
>>> m
matrix([[2, 5, 1],
[4, 6, 2]])
>>> m.sort()
>>> m
matrix([[1, 2, 5],
[2, 4, 6]])
>>> m.shape()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable
>>> m.shape
(2, 3)
>>> m.shape[0]
2
>>> m.shape[1]
3
>>> m[1:]
matrix([[2, 4, 6]])
>>> m{1,0:1}
File "<stdin>", line 1
m{1,0:1}
^
SyntaxError: invalid syntax
>>> m(1,0:1)
File "<stdin>", line 1
m(1,0:1)
^
SyntaxError: invalid syntax
>>> m[1,0:1]
matrix([[2]])
>>> m[1,0:3]
matrix([[2, 4, 6]])
>>> m[1,0:2]
matrix([[2, 4]])
>>> x = np.mat([0,0,0])
>>> x
matrix([[0, 0, 0]])
>>> tile(x,(3,1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tile' is not defined
>>> np.tile(x,(3,1))
matrix([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
>>> np.tile(x,(2,2))
matrix([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
>>> 




Loading Disqus comments...
Table of Contents