Numpy模块学习笔记2:数组的运算、切片与索引

1、数组的加减乘除:

!注意区分元素的运算和矩阵的运算!

from __future__ import division //导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入该特征时,"/"操作符执行的是截断除法(Truncating Division),当我们导入精确除法之后,"/"执行的是精确除法
import numpy as np 
a = np.array([[1.0, 2.0], [3.0, 4.0]]) 
b = np.array([[5.0, 6.0], [7.0, 8.0]]) 
sum = a + b  
difference = a - b  
product = a * b  //矩阵点乘,即元素一对一的乘法
devision = a / b  //矩阵点除,即元素一对一的除法
matrix_product = np.dot(a,b)  //矩阵乘法
matrix_devision = np.dot(a,b.I) //矩阵除法,b.I为b的逆矩阵

2、元素级的数组运算函数:ufunc函数(universal function)

                                     数学运算:

方法描述
add(x1, x2, /[, out, where, cast, order, ...]) 按元素添加参数。
subtract(x1, x2, /[, out, where, cast, ...]) 从元素方面减去参数。
multiply(x1, x2, /[, out, where, cast, ...]) 在元素方面乘以论证。
divide(x1, x2, /[, out, where, cast, ...]) 以元素方式返回输入的真正除法。
logaddexp(x1, x2, /[, out, where, cast, ...]) 输入的取幂之和的对数。
logaddexp2(x1, x2, /[, out, where, cast, ...]) base-2中输入的取幂之和的对数。
true_divide(x1, x2, /[, out, where, ...]) 以元素方式返回输入的真正除法。
floor_divide(x1, x2, /[, out, where, ...]) 返回小于或等于输入除法的最大整数。
negative(x, /[, out, where, cast, order, ...]) 数字否定, 元素方面。
positive(x, /[, out, where, cast, order, ...]) 数字正面, 元素方面。
power(x1, x2, /[, out, where, cast, ...]) 第一个数组元素从第二个数组提升到幂, 逐个元素。
remainder(x1, x2, /[, out, where, cast, ...]) 返回除法元素的余数。
mod(x1, x2, /[, out, where, cast, order, ...]) 返回除法元素的余数。
fmod(x1, x2, /[, out, where, cast, ...]) 返回除法的元素余数。
divmod(x1, x2 [, out1, out2], /[[, out, ...]) 同时返回逐元素的商和余数。
absolute(x, /[, out, where, cast, order, ...]) 逐个元素地计算绝对值。
fabs(x, /[, out, where, cast, order, ...]) 以元素方式计算绝对值。
rint(x, /[, out, where, cast, order, ...]) 将数组的元素舍入为最接近的整数。
sign(x, /[, out, where, cast, order, ...]) 返回数字符号的元素指示。
heaviside(x1, x2, /[, out, where, cast, ...]) 计算Heaviside阶跃函数。
conj(x, /[, out, where, cast, order, ...]) 以元素方式返回复共轭。
conjugate(x, /[, out, where, cast, ...]) 以元素方式返回复共轭。
exp(x, /[, out, where, cast, order, ...]) 计算输入数组中所有元素的指数。
exp2(x, /[, out, where, cast, order, ...]) 计算输入数组中所有 p 的 2**p。
log(x, /[, out, where, cast, order, ...]) 自然对数, 元素方面。
log2(x, /[, out, where, cast, order, ...]) x的基数为2的对数。
log10(x, /[, out, where, cast, order, ...]) 以元素方式返回输入数组的基数10对数。
expm1(x, /[, out, where, cast, order, ...]) 计算数组中的所有元素。exp(x) - 1
log1p(x, /[, out, where, cast, order, ...]) 返回一个加上输入数组的自然对数, 逐个元素。
sqrt(x, /[, out, where, cast, order, ...]) 以元素方式返回数组的非负平方根。
square(x, /[, out, where, cast, order, ...]) 返回输入的元素方块。
cbrt(x, /[, out, where, cast, order, ...]) 以元素方式返回数组的立方根。
reciprocal(x, /[, out, where, cast, ...]) 以元素方式返回参数的倒数。
gcd(x1, x2, /[, out, where, cast, order, ...]) 返回 | x1 | 和的最大公约数 | x2 | 。
lcm(x1, x2, /[, out, where, cast, order, ...]) 返回 | x1 | 和的最小公倍数 | x2 | 。

                                    比较函数:

方法描述
greater(x1, x2, /[, out, where, cast, ...]) 以元素方式返回(x1 > x2)的真值。
greater_equal(x1, x2, /[, out, where, ...]) 以元素方式返回(x1 >= x2)的真值。
less(x1, x2, /[, out, where, cast, ...]) 返回(x1 < x2)元素的真值。
less_equal(x1, x2, /[, out, where, cast, ...]) 以元素方式返回(x1 =< x2)的真值。
not_equal(x1, x2, /[, out, where, cast, ...]) 以元素方式返回(x1 != x2)。
equal(x1, x2, /[, out, where, cast, ...]) 以元素方式返回(x1 == x2)。
fmax(x1, x2, /[, out, where, cast, ...])  元素最大的数组元素。
fmin(x1, x2, /[, out, where, cast, ...])  元素最小的数组元素。

 

【【广播规则】】

如果两个数组 a 和 b 形状相同,即满足 a.shape == b.shape,那么 a*b 的结果就是 a 与 b 数组对应位相乘。这要求维数相同,且各维度的长度相同。

广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行。

a = np.array([[ 0, 0, 0],
                     [10,10,10],
                     [20,20,20],
                     [30,30,30]])
b = np.array([1,2,3])
print(a+b)
bb = np.tile(b, (4, 1)) //b在竖向上重复4次,得到4次重复行,横向上重复1次,得到1次重复列,生成新数组bb
print(a + bb) //a+bb等于a+b

 

2、数组的索引和切片

切片索引

b = a[2:7:2] //从a的第三个元素开始,到第七个元素结束,步长为2,生成新数组b,***a如果有多维的话,索引的是最外面一维(最左边的数)
b[1] = 3 //重新赋值,与原来不同
print(a) //a的值也会改变

c = a[2:7:2].copy() 
c[1] = 5 //重新赋值,与原来不同
print(a) //a的值不会改变

a[1:, :2] = 0 //选出除了a的第1行外,其他行的第1、2个元素,赋值为0
d = a[...,1:]  //省略号代表行数与原数组a相同,选出除了a的第1列以外的所有列,生成新数组d

花式(整数)索引

b = a[[-4,-2]]  //选出a的倒数第4行、倒数第2行,生成新数组b
c = a[[1, 5], [0, 3]]  //选出a的(1,0)和(5,3)两个元素,生成新数组c
d = a[[1, 5], [0]]  //选出a的(1,0)和(5,0)两个元素,生成新数组d
e = a[[1, 5]][:, [0, 2, 1]] //选出a的第2行和第6行,所有列,但是按第1列、第3列、第2列的顺序,生成新数组e
f = a[np.ix_([1, 5], [3,0])] //按顺序选出a的第2行、第6行,以及第4列、第1列,生成新数组f

布尔索引

b = a[a >  5] //输出a中大于5的元素,形成新列表b
c = np.array([1,3,2,0])
cc = (c == 1)|(c == 2) //生成c中元素1或2对应位置为True,其他位置为False的布尔型数组cc
d = a[cc, 1:] // 选取a中第一维为真的行(第1行、第3行)和除了第1列外的所有列,生成新数组d

e = a[np.iscomplex(a)] //选取a中为复数的所有元素
f = a[~np.iscomplex(a)] //选取a中不为复数的所有元素

 

 

posted @ 2021-01-18 21:24  venko  阅读(170)  评论(0)    收藏  举报