真值测试
numpy.all() 用于判断整个数组中的元素的值是否全部满足条件,如果满足条件返回True,否则返回False。
numpy.any() 任意一个元素为True,输出为True
import numpy as np
a2 = np.arange(5) # 生成一个矩阵 [0 1 2 3 4]
print("np.all(a2):", np.all(a2)) # 输出:False
print("np.any(a2):", np.any(a2)) # 输出:True
numpy.isnan 判断是否为空
a=np.array([1,2,np.nan]) print(np.isnan(a)) #[False False True]
逻辑运算
np.logical_and (逻辑与)
np.logical_or (逻辑或)
np.logical_not (逻辑非)
np.logical_and(True, False) #False np.logical_and([True, False], [False, False]) #array([False, False], dtype=bool)
对照函数
np.greater(x, 2) x中大于2的值
np.greater_equal(x, 2) x中大于或者等于2的值
np.equal(x, 2)
np.not_equal(x, 2)
np.less(x, 2)
np.less_equal(x, 2)
x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) y = x > 2 print(y) print(np.greater(x, 2)) # [False False True True True True True True] y = x >= 2 print(y) print(np.greater_equal(x, 2)) # [False True True True True True True True] y = x == 2 print(y) print(np.equal(x, 2)) # [False True False False False False False False] y = x != 2 print(y) print(np.not_equal(x, 2)) # [ True False True True True True True True] y = x < 2 print(y) print(np.less(x, 2)) # [ True False False False False False False False] y = x <= 2 print(y) print(np.less_equal(x, 2)) # [ True True False False False False False False]
numpy.isclose比较两个array是不是每一元素都相等,默认在1e-05的误差范围内
numpy.allclose与numpy.isclose相同,区别在于numpy.isclose输出a,b两个数组每个位置判断的bool值,而numpy.allclose输出的是a,b两个数组整体的判断结果
向量化
有了向量化,编写代码时无需使用显式循环
广播
在算术运算期间处理具有不同形状的数组,让较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。
广播的规则:
- 如果两个数组的维度数dim不相同,那么小维度数组的形状将会在左边补1。
- 如果shape维度不匹配,但是有维度是1,那么可以扩展维度是1的维度匹配另一个数组;
- 如果shape维度不匹配,但是没有任何一个维度是1,则匹配引发错误;
二维数组加一维数组
import numpy as np x = np.arange(4) y = np.ones((3, 4)) print(x.shape) # (4,) x为[0. 1. 2. 3] print(y.shape) # (3, 4) print((x + y).shape) # (3, 4) print(x + y) # [[1. 2. 3. 4.] # [1. 2. 3. 4.] # [1. 2. 3. 4.]]
两个数组均需要广播
x = np.arange(4).reshape(4, 1) y = np.ones(5) #x为[[0] [1] [2] [3]] #y为[1. 1. 1. 1. 1.] print(x.shape) # (4, 1) print(y.shape) # (5,) 只有一维5个数的意思 print((x + y).shape) # (4, 5) print(x + y) # [[1. 1. 1. 1. 1.] # [2. 2. 2. 2. 2.] # [3. 3. 3. 3. 3.] # [4. 4. 4. 4. 4.]]
算术运算
加减乘除
numpy.add(x1, x2, *args, **kwargs)
numpy.subtract(x1, x2, *args, **kwargs)
numpy.multiply(x1, x2, *args, **kwargs)
numpy.divide(x1, x2, *args, **kwargs)
numpy.floor_divide(x1, x2, *args, **kwargs) 对x1/x2向下取整
numpy.power(x1, x2, *args, **kwargs) x1的x2次方
numpy.sqrt(x, *args, **kwargs) 开根号
numpy.square(x, *args, **kwargs) 平方
注意乘方的运算规则
三角函数
numpy.prod 乘积
numpy.cumprod 累乘
numpy.diff 差值
numpy.around 舍入
numpy.ceil 上限
numpy.floor 下限
import numpy as np x = np.random.rand(3, 3) * 10 print(x) # [[0.67847795 1.33073923 4.53920122] # [7.55724676 5.88854047 2.65502046] # [8.67640444 8.80110812 5.97528726]] y = np.ceil(x) print(y) # [[1. 2. 5.] # [8. 6. 3.] # [9. 9. 6.]] y = np.floor(x) print(y) # [[0. 1. 4.] # [7. 5. 2.] # [8. 8. 5.]]
numpy.clip 裁剪
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.clip(x, a_min=20, a_max=30) #小于20的用20替换,大于30的用30替换
print(y)
# [[20 20 20 20 20]
# [20 20 20 20 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [30 30 30 30 30]]
numpy.absolute 绝对值
numpy.sign 返回数字符号的逐元素指示 数学中的sign()函数
x = np.arange(-5, 5) print(x) #[-5 -4 -3 -2 -1 0 1 2 3 4] print(np.sign(x)) #[-1 -1 -1 -1 -1 0 1 1 1 1]练习
1,将数组a中大于30的值替换为30,小于10的值替换为10。
方法一:np.clip
方法二:np.where
import numpy as np np.random.seed(100) a = np.random.uniform(1, 50, 20) print(a) # [27.63 14.64 21.8 42.39 1.23 6.96 33.87 41.47 7.7 29.18 44.67 11.25 # 10.08 6.31 11.77 48.95 40.77 9.43 41. 14.43] # 方法1 b = np.clip(a, a_min=10, a_max=30) print(b) # [27.63 14.64 21.8 30. 10. 10. 30. 30. 10. 29.18 30. 11.25 # 10.08 10. 11.77 30. 30. 10. 30. 14.43] # 方法2 b = np.where(a < 10, 10, a) b = np.where(b > 30, 30, b) print(b) # [27.63 14.64 21.8 30. 10. 10. 30. 30. 10. 29.18 30. 11.25 # 10.08 10. 11.77 30. 30. 10. 30. 14.43]
2,如何找到局部极大值的坐标,或峰值
np.diff() 沿着指定轴计算差值
import numpy as np a = np.array([1, 3, 7, 1, 2, 6, 0, 1]) b1 = np.diff(a) b2 = np.sign(b1) b3 = np.diff(b2) print(b1) # [ 2 4 -6 1 4 -6 1] print(b2) # [ 1 1 -1 1 1 -1 1] print(b3) # [ 0 -2 2 0 -2 2] #等于-2处为极大值,为2处为极小值,0处在单增加或者单减少 index = np.where(np.equal(b3, -2))[0] + 1 print(index) # [2 5]
3,对矩阵做归一化
归一化: (x - min) / (max - min)
Z = np.random.random((5,5)) Zmax, Zmin = Z.max(), Z.min() Z = (Z - Zmin)/(Zmax - Zmin) print(Z)
4,提取整数部分
np.floor() 向下取整
np.ceil() 向上取整
np.trunc/fix(a) 截取整数部分
Z = np.random.uniform(0,10,10) print (Z - Z%1) print (np.floor(Z)) print (np.ceil(Z)-1) print (Z.astype(int)) print (np.trunc(Z))
平均数为:np.mean(arr1))
中位数为:np.median(arr1))
方差为:%np.var(arr1))
标准差为:np.std(arr1))
相关性矩阵为:np.cov(arr1,arr2))
协方差矩阵为:np.corrcoef(arr1,arr2)
4,从数组中提取满足给定范围内的所有元素
获取5-10之间的元素
import numpy as np a = np.array([2, 6, 1, 9, 10, 3, 27]) mask = np.logical_and(np.greater_equal(a, 5), np.less_equal(a, 10)) # 方法1 x = np.where(mask) print(a[x]) # [ 6 9 10] # 方法2 x = np.nonzero(mask) print(a[x]) # [ 6 9 10] # 方法3 x = a[np.logical_and(a >= 5, a <= 10)] print(x) # [ 6 9 10]
5,找出数组中与给定值最接近的数
np.argmin()用于检索数组中最小值的位置,并返回其下标
Z=np.array([[0,1,2,3],[4,5,6,7]]) print(Z) z=5.1 np.abs(Z - z).argmin() print(Z.flat[np.abs(Z - z).argmin()])



