# python中的numeric包和numarray包使用教程

>>> pyarr = [[1,2,3],
… [4,5,6],
… [7,8,9]]
>>> print pyarr
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> pyarr[1][1] = 0
>>> print pyarr
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]

>>> from numarray import *
>>> numarr = array(pyarr)
>>> print numarr
[[1 2 3]
[4 0 6]
[7 8 9]]

>>> numarr2 = numarr * 2
>>> print numarr2
[[ 2 4 6]
[ 8 0 12]
[14 16 18]]
>>> print numarr2 + numarr
[[ 3 6 9]
[12 0 18]
[21 24 27]]

>>> numarr2.shape = (9,)
>>> print numarr2
[ 2 4 6 8 0 12 14 16 18]

numeric 与 numarray 之间的区别

numarray 所做的一些改进：

def timer(fun, n, comment=””):
from time import clock
start = clock()
print comment, len(fun(n)), “elements”,
print “in %.2f seconds” % (clock()-start)
def double1(n): return map(lambda n: 2*n, xrange(n))
timer(double1, 5000000, “running map() on xrange iterator:”)
def double2(n): return [2*n for n in xrange(n)]
timer(double2, 5000000, “running listcomp on xrange iter: “)
def double3(n):
double = []
for n in xrange(n):
double.append(2*n)
return double
timer(double3, 5000000, “building new list from iterator: “)

import array
def double4(n): return [2*n for n in array.array(‘i’,range(n))]
timer(double4, 5000000, “running listcomp on array.array: “)

from numarray import *
def double5(n): return 2*arange(n)
timer(double5, 5000000, “numarray scalar multiplication: “)
def double6(n): return (2*arange(n)).tolist()
timer(double6, 5000000, “numarray mult, returning list: “)

\$ python2.3 timing.py
running map() on xrange iterator: 5000000 elements in 13.61 seconds
running listcomp on xrange iter: 5000000 elements in 16.46 seconds
building new list from iterator: 5000000 elements in 20.13 seconds
running listcomp on array.array: 5000000 elements in 25.58 seconds
numarray scalar multiplication: 5000000 elements in 0.61 seconds
numarray mult, returning list: 5000000 elements in 3.70 seconds

numerical python 的典型用例是科学建模，或者可能是相关领域，比如图形处理和旋转，或者信号处理。我将通过一个比较实际的问题来说明 numarray 的许多功能。假设您有一个参量可变的三维物理空间。抽象地说，任何参数化空间，不论有多少维，numarray 都适用。实际上很容易想像，比如一个房间，它的各个点的温度是不同的。我在 new england 的家已经到了冬天，因而这个问题似乎更有现实意义。

>>> from numarray import *
>>> room = zeros((4,3,5),float)
>>> print room
[[[ 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.]]]

>>> room += 70
>>> print room
[[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]]

>>> floor = room[3]
>>> floor -= 4
>>> print room
[[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 66. 66. 66. 66. 66.]
[ 66. 66. 66. 66. 66.]
[ 66. 66. 66. 66. 66.]]]

>>> north = room[:,0]
>>> near_fireplace = north[2:4,2:5]
>>> near_fireplace += 8
>>> north[3,2] = 90 # the fireplace cell itself
>>> print room
[[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 70. 78. 78. 78. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]]
[[ 66. 74. 90. 74. 66.]
[ 66. 66. 66. 66. 66.]
[ 66. 66. 66. 66. 66.]]]

>>> print north
[[ 70. 70. 70. 70. 70.]
[ 70. 70. 70. 70. 70.]
[ 70. 78. 78. 78. 70.]
[ 66. 74. 90. 74. 66.]]

70.066666666666663

array([[ 276., 292., 308., 292., 276.],
[ 276., 276., 276., 276., 276.],
[ 276., 276., 276., 276., 276.]])

>>> def equalize(room):
… z,y,x = map(randint, (1,1,1), room.shape)
… zmin,ymin,xmin = maximum([z-2,y-2,x-2],[0,0,0]).tolist()
… zmax,ymax,xmax = [z+1,y+1,x+1]
… region = room[zmin:zmax,ymin:ymax,xmin:xmax].copy()
… room[z-1,y-1,x-1] = sum(region.flat)/len(region.flat)
… return room

>>> print equalize(room.copy())
[[[ 70. 70. 70. 70. 70. ]
[ 70. 70. 70. 70. 70. ]
[ 70. 70. 70. 70. 70. ]]
[[ 70. 70. 71.333333 70. 70. ]
[ 70. 70. 70. 70. 70. ]
[ 70. 70. 70. 70. 70. ]]
[[ 70. 78. 78. 78. 70. ]
[ 70. 70. 70. 70. 70. ]
[ 70. 70. 70. 70. 70. ]]
[[ 66. 74. 90. 74. 66. ]
[ 66. 66. 66. 66. 66. ]
[ 66. 66. 66. 68. 66. ]]]

Posted in 未分类