Numpy中文指南:进阶操作

1 广播法则

广播法则能使通用函数有意义地处理不具有相同形状的输入。

广播第一法则是,如果所有的输入数组维度不都相同,一个“1”将被重复地添加在维度较小的数组上直至所有的数组拥有一样的维度。

广播第二法则确定长度为1的数组沿着特殊的方向表现地好像它有沿着那个方向最大形状的大小。

对数组来说,沿着那个维度的数组元素的值理应相同。应用广播法则之后,所有数组的大小必须匹配。

2 索引技巧

通过数组索引

>>> a = arange(12)**2 # the first 12 square numbers
>>> i = array( [ 1,1,3,8,5 ] ) # an array of indices
>>> a[i] # 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[j] # the same shape as j
array([[ 9, 16],[8149]

调色板

>>> palette = array( [  [0,0,0], # black... [255,0,0], # red... 
                        [0,255,0], # green... 
                        [0,0,255], # blue... 
                        [255,255,255] ] ) # white
>>> image = array( [ [ 0, 1, 2, 0 ], # each value corresponds to a color in palette... 
                     [ 0, 3, 4, 0 ] ] )
>>> palette[image] # the (2,4,3) color image
array([[[ 0, 0, 0],[255, 0, 0],[ 0, 255, 0],[ 0, 0, 0]],[[ 0, 0, 0],[ 0, 0, 255],[255, 255, 255],[ 0, 0, 0]]

也可以给出不不止一维的索引,每一维的索引数组必须有相同的形状。

使用布尔数组索引,我们能想到的使用布尔数组的索引最自然方式就是使用和原数组一样形状的布尔数

>>> a = arange(12).reshape(3,4)
>>> b = a > 4
>>> b # b is a boolean with a's shape
array([[False, False, False, False],[False, True, True, True],[True, True, True, True]], dtype=bool)
>>> a[b] # 1d array with the selected elements
array([ 5, 6, 7, 8, 9, 10, 11])

这个属性在赋值时非常有用:

>>> a[b] = 0 # All elements of 'a' higher than 4 become 0
>>> a
array([[0, 1, 2, 3],[4, 0, 0, 0],[0, 0, 0, 0]])

第二种通过布尔来索引的方法更近似于整数索引;

>>> a = arange(12).reshape(3,4)
>>> b1 = array([False,True,True]) # first dim selection
>>> b2 = array([True,False,True,False]) # second dim selection
>>>
>>> a[b1,:] # selecting rows
array([[ 4, 5, 6, 7],[ 8, 9, 10, 11]])
>>>
>>> a[b1] # same thing
array([[ 4, 5, 6, 7],[ 8, 9, 10, 11]])
>>>
>>> a[:,b2] # selecting columns
array([[ 0, 2],[ 4, 6],[ 8, 10]])
>>>
>>> a[b1,b2] # a weird thing to do
array([ 4, 10])

3 ix_()函数

ix_函数可以为了获得多元组的结果而用来结合不同向量。例如,如果你想要用所有向量a、b和c元素组成的三元组来计算a+b*c

>>> a = array([2,3,4,5])
>>> b = array([8,5,4])
>>> c = array([5,4,6,8,3])
>>> ax,bx,cx = ix_(a,b,c)
>>> axarray([[[2]],[[3]],[[4]],[[5]]])
>>> bxarray([[[8],[5],[4]]])
>>> cxarray([[[5, 4, 6, 8, 3]]])
>>> ax.shape, bx.shape, cx.shape
((4, 1, 1), (1, 3, 1), (1, 1, 5))
>>> result = ax+bx*cx
>>> resultarray([[[42, 34, 50, 66, 26],[27, 22, 32, 42, 17],[22, 18, 26, 34, 14]],[[43, 35, 51, 67, 27],[28, 23, 33, 43, 18],[23, 19, 27, 35, 15]],[[44, 36, 52, 68, 28],[29, 24, 34, 44, 19],[24, 20, 28, 36, 16]],[[45, 37, 53, 69, 29],[30, 25, 35, 45, 20],[25, 21, 29, 37, 17]]])
>>> result[3,2,4]
17
>>> a[3]+b[2]*c[4]
17
# 你也可以实行如下简化:
def ufunc_reduce(ufct, *vectors):
vs = ix_(*vectors)
r = ufct.identity
for v in vs:
r = ufct(r,v)
return r
# 然后这样使用它:
>>> ufunc_reduce(add,a,b,c)
array([[[15, 14, 16, 18, 13],[12, 11, 13, 15, 10],[11, 10, 12, 14, 9]],[[16, 15, 17, 19, 14],[13, 12, 14, 16, 11],[12, 11, 13, 15, 10]],[[17, 16, 18, 20, 15],[14, 13, 15, 17, 12],[13, 12, 14, 16, 11]],[[18, 17, 19, 21, 16],[15, 14, 16, 18, 13],[14, 13, 15, 17, 12]]

4 线性代数

numpy文件夹中的linalg.py获得更多信息

转置:a.transpose()

逆矩阵:inv(a)

单位矩阵:eye(2)

矩阵乘: dot (j, j)

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

迹: trace(u)

矩阵类:

A = matrix('1.0 2.0; 3.0 4.0')
>>> A[[ 1\. 2.][ 3\4.]]
>>> A.T # transpose
[[ 1\. 3.][ 2\. 4.]]

 

区别:

>>> print A[:]; print A[:].shape
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
(3, 4)
>>> print M[:]; print M[:].shape
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
(3, 1)

5 自动改变形状

>>> 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],[12, 13, 14]],[[15, 16, 17],
[18, 19, 20],[21, 22, 23],[24, 25, 26],[272829]]

6 直方图

NumPy中histogram函数应用到一个数组返回一对变量:直方图数组和箱式向量。注意:matplotlib也有一个用来建立直方
图的函数(叫作hist,正如matlab中一样)与NumPy中的不同。主要的差别是pylab.hist自动绘制直方图,而numpy.histogram仅
仅产生数据。

v = numpy.random.normal(mu,sigma,10000)
(n, bins) = numpy.histogram(v, bins=50, normed=True) # NumPy version (no plot)
plt.plot(.5*(bins[1:]+bins[:-1]), n)
plt.show()

7 所有数据类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值