df3 发表于 2015-12-15 12:01:06

python numpy教程

先决条件

例如,在3D空间一个点的坐标是一个秩为1的数组,因为它只有一个轴。那个轴长度为3.又例如,在以下例子中,数组的秩为2(它有两个维度).第一个维度长度为2,第二个维度长度为3.

一个例子1

>>> from numpyimport *
>>> a = arange(15).reshape(3, 5)
>>> a
array([[ 0,1,2,3,4],
[ 5,6,7,8,9],
])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int32'
>>> a.itemsize
4
>>> a.size
15
>>> type(a)
numpy.ndarray
>>> b = array()
>>> b
array()
>>> type(b)
numpy.ndarray
例如,你可以使用array函数从常规的Python列表和元组创造数组。所创建的数组类型由原序列中的元素类型推导而来。

>>> from numpyimport *
>>> a = array( )
>>> a
array()
>>> a.dtype
dtype('int32')
>>> b = array()
>>> b.dtype
dtype('float64')
一个常见的错误包括用多个数值参数调用array而不是提供一个由数值组成的列表作为一个参数。

>>> a = array(1,2,3,4)    # WRONG

>>> a = array()# RIGHT
数组将序列包含序列转化成二维的数组,序列包含序列包含序列转化成三维数组等等。

>>> b = array( [ (1.5,2,3), (4,5,6) ] )
>>> b
array([[ 1.5,2. ,3. ],
[ 4. ,5. ,6. ]])
数组类型可以在创建时显示指定

>>> c = array( [ , ], dtype=complex )
>>> c
array([[ 1.+0.j,2.+0.j],
[ 3.+0.j,4.+0.j]])
函数function创建一个全是0的数组,函数ones创建一个全1的数组,函数empty创建一个内容随机并且依赖与内存状态的数组。默认创建的数组类型(dtype)都是float64。

>>> zeros( (3,4) )
array([,
,
])
>>> ones( (2,3,4), dtype=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)
>>> empty( (2,3) )
array([,
])
为了创建一个数列,NumPy提供一个类似arange的函数返回数组而不是列表:

>>> arange( 10, 30, 5 )
array()
>>> arange( 0, 2, 0.3 )               # it accepts float arguments
array([ 0. ,0.3,0.6,0.9,1.2,1.5,1.8])
一维数组被打印成行,二维数组成矩阵,三维数组成矩阵列表。

>>> a = arange(6)                         # 1d array
>>> print a

>>>
>>> b = arange(12).reshape(4,3)         # 2d array
>>> print b
[[ 012]
[ 345]
[ 678]
[ 9 10 11]]
>>>
>>> c = arange(24).reshape(2,3,4)         # 3d array
>>> print c
[[[ 0123]
[ 4567]
[ 89 10 11]]

[

]]
如果一个数组用来打印太大了,NumPy自动省略中间部分而只打印角落

>>> print arange(10000)
[   0    1    2 ..., 9997 9998 9999]
>>>
>>> print arange(10000).reshape(100,100)
[[   0    1    2 ...,   97   98   99]
[ 100101102 ...,197198199]
[ 200201202 ...,297298299]
...,


]
禁用NumPy的这种行为并强制打印整个数组,你可以设置printoptions参数来更改打印选项。

数组的算术运算是按元素的。新的数组被创建并且被结果填充。

>>> a = array( )
>>> b = arange( 4 )
>>> b
array()
>>> c = a-b
>>> c
array()
>>> b**2
array()
>>> 10*sin(a)
array([ 9.12945251, -9.88031624,7.4511316 , -2.62374854])
>>> a>> A = array( [,
...             ] )
>>> B = array( [,
...             ] )
>>> A*B                         # elementwise product
array([,
])
>>> dot(A,B)                  # matrix product
array([,
])
有些操作符像+=和*=被用来更改已存在数组而不创建一个新的数组。

>>> a = ones((2,3), dtype=int)
>>> b = random.random((2,3))
>>> a *= 3
>>> a
array([,
])
>>> b += a
>>> b
array([[ 3.69092703,3.8324276 ,3.0114541 ],
[ 3.18679111,3.3039349 ,3.37600289]])
>>> a += b                                  # b is converted to integer type
>>> a
array([,
])
当运算的是不同类型的数组时,结果数组和更普遍和精确的已知(这种行为叫做upcast)。

>>> a = ones(3, dtype=int32)
>>> b = linspace(0,pi,3)
>>> b.dtype.name
'float64'
>>> c = a+b
>>> c
array([ 1.      ,2.57079633,4.14159265])
>>> c.dtype.name
'float64'
>>> d = exp(c*1j)
>>> d
array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,
-0.54030231-0.84147098j])
>>> d.dtype.name
'complex128' 许多非数组运算,如计算数组所有元素之和,被作为ndarray类的方法实现

>>> a = random.random((2,3))
>>> a
array([[ 0.6903007 ,0.39168346,0.16524769],
[ 0.48819875,0.77188505,0.94792155]])
>>> a.sum()
3.4552372100521485
>>> a.min()
0.16524768654743593
>>> a.max()
0.9479215542670073
这些运算默认应用到数组好像它就是一个数字组成的列表,无关数组的形状。然而,指定axis参数你可以吧运算应用到数组指定的轴上:

>>> b = arange(12).reshape(3,4)
>>> b
array([[ 0,1,2,3],
[ 4,5,6,7],
[ 8,9, 10, 11]])
>>>
>>> b.sum(axis=0)                            # sum of each column
array()
>>>
>>> b.min(axis=1)                            # min of each row
array()
>>>
>>> b.cumsum(axis=1)                         # cumulative sum along each row
array([[ 0,1,3,6],
[ 4,9, 15, 22],
[ 8, 17, 27, 38]])
NumPy提供常见的数学函数如sin,cos和exp。在NumPy中,这些叫作“通用函数”(ufunc)。在NumPy里这些函数作用按数组的元素运算,产生一个数组作为输出。

>>> B = arange(3)
>>> B
array()
>>> exp(B)
array([ 1.      ,2.71828183,7.3890561 ])
>>> sqrt(B)
array([ 0.      ,1.      ,1.41421356])
>>> C = array()
>>> add(B, C)
array([ 2.,0.,6.])
一维数组可以被索引、切片和迭代,就像列表和其它Python序列。

>>> a = arange(10)**3
>>> a
array()
>>> a
8
>>> a
array([ 8, 27, 64])
>>> a[:6:2] = -1000    # equivalent to a = -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()
>>> 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
多维数组可以每个轴有一个索引。这些索引由一个逗号分割的元组给出。

>>> def f(x,y):
...         return 10*x+y
...
>>> b = fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0,1,2,3],
,
,
,
])
>>> b
23
>>> b                     # each row in the second column of b
array([ 1, 11, 21, 31, 41])
>>> b[ : ,1]                        # equivalent to the previous example
array([ 1, 11, 21, 31, 41])
>>> b                      # each column in the second and third row of b
array([,
])
当少于轴数的索引被提供时,确失的索引被认为是整个切片:

>>> b[-1]                                  # the last row. Equivalent to b[-1,:]
array()

迭代多维数组是就第一个轴而言的:2

>>> for row in b:
...         print row
...





然而,如果一个人想对每个数组中元素进行运算,我们可以使用flat属性,该属性是数组元素的一个迭代器:

>>> for element in b.flat:
...         print element,
...
0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43
一个数组的形状由它每个轴上的元素个数给出:

>>> a = floor(10*random.random((3,4)))
>>> a
array([[ 7.,5.,9.,3.],
[ 7.,2.,7.,8.],
[ 6.,8.,3.,2.]])
>>> a.shape
(3, 4)
一个数组的形状可以被多种命令修改:

>>> a.ravel() # flatten the array
array([ 7.,5.,9.,3.,7.,2.,7.,8.,6.,8.,3.,2.])
>>> a.shape = (6, 2)
>>> a.transpose()
array([[ 7.,9.,7.,7.,6.,3.],
[ 5.,3.,2.,8.,8.,2.]])
reshape函数改变参数形状并返回它,而resize函数改变数组自身。

>>> a
array([[ 7.,5.],
[ 9.,3.],
[ 7.,2.],
[ 7.,8.],
[ 6.,8.],
[ 3.,2.]])
>>> a.resize((2,6))
>>> a
array([[ 7.,5.,9.,3.,7.,2.],
[ 7.,8.,6.,8.,3.,2.]])
几种方法可以沿不同轴将数组堆叠在一起:

>>> a = floor(10*random.random((2,2)))
>>> a
array([[ 1.,1.],
[ 5.,8.]])
>>> b = floor(10*random.random((2,2)))
>>> b
array([[ 3.,3.],
[ 6.,0.]])
>>> vstack((a,b))
array([[ 1.,1.],
[ 5.,8.],
[ 3.,3.],
[ 6.,0.]])
>>> hstack((a,b))
array([[ 1.,1.,3.,3.],
[ 5.,8.,6.,0.]])
函数column_stack以列将一维数组合成二维数组,它等同与vstack对一维数组。

>>> column_stack((a,b))   # With 2D arrays
array([[ 1.,1.,3.,3.],
[ 5.,8.,6.,0.]])
>>> a=array()
>>> b=array()
>>> a[:,newaxis]# This allows to have a 2D columns vector
array([[ 4.],
[ 2.]])
>>> column_stack((a[:,newaxis],b[:,newaxis]))
array([[ 4.,2.],
[ 2.,8.]])
>>> vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different
array([[ 4.],
[ 2.],
[ 2.],
[ 8.]])
在复杂情况下,r_[]和c_[]对创建沿着一个方向组合的数很有用,它们允许范围符号(“:”):

>>> r_
array()
使用hsplit你能将数组沿着它的水平轴分割,或者指定返回相同形状数组的个数,或者指定在哪些列后发生分割:

>>> a = floor(10*random.random((2,12)))
>>> a
array([[ 8.,8.,3.,9.,0.,4.,3.,0.,0.,6.,4.,4.],
[ 0.,3.,2.,9.,6.,0.,4.,5.,7.,5.,1.,4.]])
>>> hsplit(a,3)   # Split a into 3
,
[ 0.,3.,2.,9.]]), array([[ 0.,4.,3.,0.],
[ 6.,0.,4.,5.]]), array([[ 0.,6.,4.,4.],
[ 7.,5.,1.,4.]])]
>>> hsplit(a,(3,4))   # Split a after the third and the fourth column
,
[ 0.,3.,2.]]), array([[ 9.],
[ 9.]]), array([[ 0.,4.,3.,0.,0.,6.,4.,4.],
[ 6.,0.,4.,5.,7.,5.,1.,4.]])]
简单的赋值不拷贝数组对象或它们的数据。

>>> a = arange(12)
>>> b = a            # no new object is created
>>> b is a         # a and b are two names for the same ndarray object
True
>>> b.shape = 3,4    # changes the shape of a
>>> a.shape
(3, 4)
Python 传递不定对象作为参考4,所以函数调用不拷贝数组。

>>> def f(x):
...   print id(x)
...
>>> id(a)                           # id is a unique identifier of an object
148293216
>>> f(a)
148293216
不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据。

>>> c = a.view()
>>> c is a
False
>>> c.base is a                        # c is a view of the data owned by a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6                      # a's shape doesn't change
>>> a.shape
(3, 4)
>>> c = 1234                      # a's data changes
>>> a
array([[   0,    1,    2,    3],
,
[   8,    9,   10,   11]])
切片数组返回它的一个视图:

>>> s = a[ : , 1:3]   # spaces added for clarity; could also be written "s = a[:,1:3]"
>>> s[:] = 10         # s[:] is a view of s. Note the difference between s=10 and s[:]=10
>>> a
array([[   0,   10,   10,    3],
,
[   8,   10,   10,   11]])
这个复制方法完全复制数组和它的数据。

>>> d = a.copy()                        # a new array object with new data is created
>>> d is a
False
>>> d.base is a                           # d doesn't share anything with a
False
>>> d = 9999
>>> a
array([[   0,   10,   10,    3],
,
[   8,   10,   10,   11]])
创建数组

转化
操作
询问
排序
运算
基本统计
基本线性代数
通过数组索引

>>> a = arange(12)**2                        # the first 12 square numbers
>>> i = array( [ 1,1,3,8,5 ] )               # an array of indices
>>> a                                       # the elements of a at the positions i
array([ 1,1,9, 64, 25])
>>>
>>> j = array( [ [ 3, 4], [ 9, 7 ] ] )         # a bidimensional array of indices
>>> a                                       # the same shape as j
array([[ 9, 16],
])
当被索引数组a是多维的时,每一个唯一的索引数列指向a的第一维[^5]。以下示例通过将图片标签用调色版转换成色彩图像展示了这种行为。

>>> palette = array( [ ,                # black
...                  ,            # red
...                  ,            # green
...                  ,            # blue
...                   ] )       # white
>>> image = array( [ [ 0, 1, 2, 0 ],         # each value corresponds to a color in the palette
...                  [ 0, 3, 4, 0 ]] )
>>> palette                            # the (2,4,3) color image
array([[,
,
,
],
[,
,
,
]])
我们也可以给出不不止一维的索引,每一维的索引数组必须有相同的形状。

>>> a = arange(12).reshape(3,4)
>>> a
array([[ 0,1,2,3],
[ 4,5,6,7],
[ 8,9, 10, 11]])
>>> i = array( [ ,                        # indices for the first dim of a
...             ] )
>>> j = array( [ ,                        # indices for the second dim
...             ] )
>>>
>>> a                                     # i and j must have equal shape
array([[ 2,5],
[ 7, 11]])
>>>
>>> a
array([[ 2,6],
[ 6, 10]])
>>>
>>> a[:,j]                                     # i.e., a[ : , j]
array([[[ 2,1],
[ 3,3]],
[[ 6,5],
[ 7,7]],
[,
]])
自然,我们可以把i和j放到序列中(比如说列表)然后通过list索引。

>>> l =
>>> a                                       # equivalent to a
array([[ 2,5],
[ 7, 11]])
然而,我们不能把i和j放在一个数组中,因为这个数组将被解释成索引a的第一维。

>>> s = array( )
>>> a                                       # not what we want
---------------------------------------------------------------------------
IndexError                              Traceback (most recent call last)
in ()
----> 1 a

IndexError: index (3) out of range (0>
>>> a                              # same as a
array([[ 2,5],
[ 7, 11]])
另一个常用的数组索引用法是搜索时间序列最大值6。

>>> time = linspace(20, 145, 5)               # time scale
>>> data = sin(arange(20)).reshape(5,4)         # 4 time-dependent series
>>> time
array()
>>> data
array([[ 0.      ,0.84147098,0.90929743,0.14112001],
[-0.7568025 , -0.95892427, -0.2794155 ,0.6569866 ],
[ 0.98935825,0.41211849, -0.54402111, -0.99999021],
[-0.53657292,0.42016704,0.99060736,0.65028784],
[-0.28790332, -0.96139749, -0.75098725,0.14987721]])
>>>
>>> ind = data.argmax(axis=0)                   # index of the maxima for each series
>>> ind
array()
>>>
>>> time_max = time[ ind]                     # times corresponding to the maxima
>>>
>>> data_max = data)] # => data,0], data,1]...
>>>
>>> time_max
array()
>>> data_max
array([ 0.98935825,0.84147098,0.99060736,0.6569866 ])
>>>
>>> all(data_max == data.max(axis=0))
True
你也可以使用数组索引作为目标来赋值:

>>> a = arange(5)
>>> a
array()
>>> a[] = 0
>>> a
array()
然而,当一个索引列表包含重复时,赋值被多次完成,保留最后的值:

>>> a = arange(5)
>>> a[]=
>>> a
array()
这足够合理,但是小心如果你想用Python的+=结构,可能结果并非你所期望:

>>> a = arange(5)
>>> a[]+=1
>>> a
array()
我们能想到的使用布尔数组的索引最自然方式就是使用和原数组一样形状的布尔数组。

>>> a = arange(12).reshape(3,4)
>>> b = a > 4
>>> b                                          # b is a boolean with a's shape
array([,
,
], dtype=bool)
>>> a                                       # 1d array with the selected elements
array([ 5,6,7,8,9, 10, 11])
这个属性在赋值时非常有用:

>>> a = 0                                 # All elements of 'a' higher than 4 become 0
>>> a
array([,
,
])
第二种通过布尔来索引的方法更近似于整数索引;对数组的每个维度我们给一个一维布尔数组来选择我们想要的切片。

>>> a = arange(12).reshape(3,4)
>>> b1 = array()             # first dim selection
>>> b2 = array()       # second dim selection
>>>
>>> a                                 # selecting rows
array([[ 4,5,6,7],
[ 8,9, 10, 11]])
>>>
>>> a                                     # same thing
array([[ 4,5,6,7],
[ 8,9, 10, 11]])
>>>
>>> a[:,b2]                                 # selecting columns
array([[ 0,2],
[ 4,6],
[ 8, 10]])
>>>
>>> a                                  # a weird thing to do
array([ 4, 10])
ix_函数可以为了获得多元组的结果而用来结合不同向量。例如,如果你想要用所有向量a、b和c元素组成的三元组来计算a+b*c:

>>> a = array()
>>> b = array()
>>> c = array()
>>> ax,bx,cx = ix_(a,b,c)
>>> ax
array([[],

[],

[],

[]])
>>> bx
array([[,
,
]])
>>> cx
array([[]])
>>> ax.shape, bx.shape, cx.shape
((4, 1, 1), (1, 3, 1), (1, 1, 5))
>>> result = ax+bx*cx
>>> result
array([[,
,
],
[,
,
],
[,
,
],
[,
,
]])
>>> result
17
>>> a+b*c
17
你也可以实行如下简化:

然后这样使用它:
>>> ufunc_reduce(add,a,b,c)
array([[,
,
],
[,
,
],
[,
,
],
[,
,
]])
参考numpy文件夹中的linalg.py获得更多信息

>>> from numpy import *
>>> from numpy.linalg import *

>>> a = array([, ])
>>> print a
[[ 1.2.]
[ 3.4.]]

>>> a.transpose()
array([[ 1.,3.],
[ 2.,4.]])

>>> inv(a)
array([[-2. ,1. ],
[ 1.5, -0.5]])

>>> u = eye(2) # unit 2x2 matrix; "eye" represents "I"
>>> u
array([[ 1.,0.],
[ 0.,1.]])
>>> j = array([, ])

>>> dot (j, j) # matrix product
array([[-1.,0.],
[ 0., -1.]])

>>> trace(u)# trace
2.0

>>> y = array([, ])
>>> solve(a, y)
array([[-3.],
[ 4.]])

>>> eig(j)
(array([ 0.+1.j,0.-1.j]),
array([[ 0.70710678+0.j,0.70710678+0.j],
[ 0.00000000-0.70710678j,0.00000000+0.70710678j]]))
Parameters:
square matrix

Returns
The eigenvalues, each repeated according to its multiplicity.

The normalized (unit "length") eigenvectors, such that the
column ``v[:,i]`` is the eigenvector corresponding to the
eigenvalue ``w`` .
这是一个关于矩阵类的简短介绍。
>>> A = matrix('1.0 2.0; 3.0 4.0')
>>> A
[[ 1.2.]
[ 3.4.]]
>>> type(A)# file where class is defined


>>> A.T# transpose
[[ 1.3.]
[ 2.4.]]

>>> X = matrix('5.0 7.0')
>>> Y = X.T
>>> Y
[
]

>>> print A*Y# matrix multiplication
[
]

>>> print A.I# inverse
[[-2.   1. ]
[ 1.5 -0.5]]

>>> solve(A, Y)# solving linear equation
matrix([[-3.],
[ 4.]])
让我们创建数组和矩阵用来切片:
>>> A = arange(12)
>>> A
array([ 0,1,2,3,4,5,6,7,8,9, 10, 11])
>>> A.shape = (3,4)
>>> M = mat(A.copy())
>>> print type(A),"",type(M)
   
>>> print A
[[ 0123]
[ 4567]
[ 89 10 11]]
>>> print M
[[ 0123]
[ 4567]
[ 89 10 11]]
现在,让我们简单的切几片。基本的切片使用切片对象或整数。例如,A[:]和M[:]的求值将表现得和Python索引很相似。然而要注意很重要的一点就是NumPy切片数组不创建数据的副本;切片提供统一数据的视图。

>>> print A[:]; print A[:].shape
[[ 0123]
[ 4567]
[ 89 10 11]]
(3, 4)
>>> print M[:]; print M[:].shape
[[ 0123]
[ 4567]
[ 89 10 11]]
(3, 4)
现在有些和Python索引不同的了:你可以同时使用逗号分割索引来沿着多个轴索引。

>>> print A[:,1]; print A[:,1].shape

(3,)
>>> print M[:,1]; print M[:,1].shape
[

]
(3, 1)
假如我们想要一个数组的第一列和第三列,一种方法是使用列表切片:

>>> A[:,]
array([[ 1,3],
[ 5,7],
[ 9, 11]])
稍微复杂点的方法是使用take()方法(method):

>>> A[:,].take(,axis=1)
array([[ 1,3],
[ 5,7],
[ 9, 11]])
如果我们想跳过第一行,我们可以这样:

>>> A.take(,axis=1)
array([[ 5,7],
[ 9, 11]])
或者我们仅仅使用A]。还有一种方法是通过矩阵向量积(叉积)。

>>> A
array([[ 5,7],
[ 9, 11]])
为了读者的方便,在次写下之前的矩阵:

>>> A
array([[ 5,7],
[ 9, 11]])
现在让我们做些更复杂的。比如说我们想要保留第一行大于1的列。一种方法是创建布尔索引:

>>> A>1
array(, dtype=bool)
>>> A[:,A>1]
array([[ 2,3],
[ 6,7],
])
就是我们想要的!但是索引矩阵没这么方便。

>>> M>1
matrix([], dtype=bool)
>>> M[:,M>1]
matrix([])
这个过程的问题是用“矩阵切片”来切片产生一个矩阵12,但是矩阵有个方便的A属性,它的值是数组呈现的。所以我们仅仅做以下替代:

>>> M[:,M.A>1]
matrix([[ 2,3],
[ 6,7],
])
如果我们想要在矩阵两个方向有条件地切片,我们必须稍微调整策略,代之以:

>>> A>2,A>1]
array([ 6, 11])
>>> M>2,M.A>1]
matrix([[ 6, 11]])
我们需要使用向量积ix_:

>>> A>2,A>1)]
array([[ 6,7],
])
>>> M>2,M.A>1)]
matrix([[ 6,7],
])
更改数组的维度,你可以省略一个尺寸,它将被自动推导出来。
>>> a = arange(30)
>>> a.shape = 2,-1,3# -1 means "whatever is needed"
>>> a.shape
(2, 5, 3)
>>> a
array([[[ 0,1,2],
[ 3,4,5],
[ 6,7,8],
[ 9, 10, 11],
],
[,
,
,
,
]])
我们如何用两个相同尺寸的行向量列表构建一个二维数组?在MATLAB中这非常简单:如果x和y是两个相同长度的向量,你仅仅需要做m=。在NumPy中这个过程通过函数column_stack、dstack、hstack和vstack来完成,取决于你想要在那个维度上组合。例如:
x = arange(0,10,2)                     # x=()
y = arange(5)                        # y=()
m = vstack()                      # m=([,
#   ])
xy = hstack()                     # xy =()
NumPy中histogram函数应用到一个数组返回一对变量:直方图数组和箱式向量。注意:matplotlib也有一个用来建立直方图的函数(叫作hist,正如matlab中一样)与NumPy中的不同。主要的差别是pylab.hist自动绘制直方图,而numpy.histogram仅仅产生数据。

import numpy
import pylab
# Build a vector of 10000 normal deviates with variance 0.5^2 and mean 2
mu, sigma = 2, 0.5
v = numpy.random.normal(mu,sigma,10000)
# Plot a normalized histogram with 50 bins
pylab.hist(v, bins=50, normed=1)       # matplotlib version (plot)
pylab.show()
# Compute the histogram with numpy and then plot it
(n, bins) = numpy.histogram(v, bins=50, normed=True)# NumPy version (no plot)
pylab.plot(.5*(bins+bins[:-1]), n)
pylab.show()
页: [1]
查看完整版本: python numpy教程