常用运算函数
矩阵加减运算
矩阵加减元算分为相同形状矩阵间的加减以及广播机制运算
import numpy as np
from sympy.benchmarks.bench_discrete_log import data_set_3
data1 = np.array([[1,2,3],[4,5,6]])
data2 = np.array([[1,2,3]])
data3 = np.array([1,2,3])
print(data1+data2)
print(data1+data3)
print(data1-data2)
print(data1-data3)
# [[2 4 6]
# [5 7 9]]
# [[2 4 6]
# [5 7 9]]
# [[0 0 0]
# [3 3 3]]
# [[0 0 0]
# [3 3 3]]
上面代码演示的是通过实现广播机制来进行加减法运算,具体实现方式为,若两个数组维度不同,则会给维度较小的数据进行维度扩充,实现方式是将其最外层添加一层值为1的维度,直到与另一维度相同,再对值为1的维度进行复制,以此实现广播机制,若仍无法进行形状匹配,则无法实现广播机制
广播机制逐元素乘除
乘法可用*或multiply(),除法可用/或divide()
import numpy as np
data1 = np.array([[1,2,3],[4,5,6]])
print(data1*2)
print(np.multiply(data1,2))
print(data1/2)
print(np.divide(data1,2))
# [[ 2 4 6]
# [ 8 10 12]]
# [[ 2 4 6]
# [ 8 10 12]]
# [[0.5 1. 1.5]
# [2. 2.5 3. ]]
# [[0.5 1. 1.5]
# [2. 2.5 3. ]]
向量乘法运算
向量乘法运算指的是点乘计算,也就是[a,b]*[c,d]=a*b+c*d
import numpy as np
data1 = np.array([1,2,3])
data2 = np.array([4,5,6])
print(data1.dot(data2))
# 32
矩阵乘法运算
矩阵乘法运算分为对应位置元素相乘和矩阵乘法
对应位置元素相乘
对应位置元素相乘可用*或multiply实现
import numpy as np
data1 = np.array([[1,2,3],[4,5,6]])
data2 = np.array([[1,2,3],[4,5,6]])
print(np.multiply(data1,data2))
print(data1*data2)
# [[ 1 4 9]
# [16 25 36]]
# [[ 1 4 9]
# [16 25 36]]
矩阵乘法
矩阵乘法通过dot()函数实现,要求左乘的矩阵的列数等于被乘的矩阵的行数
import numpy as np
data1 = np.array([[1,2,3],[4,5,6]])
data2 = np.array([[1,2],[3,4],[5,6]])
print(data1.dot(data2))
# [[22 28]
# [49 64]]
常用内置函数
基本函数
np.ceil()
np.ceil()用于数组元素向上取整
import numpy as np
data1 = np.random.randn(4,5)
print(data1)
print(np.ceil(data1))
# [[ 3.05585723 -0.37580912 -1.63515777 0.44520431 -1.25620038]
# [-1.51531436 1.00796354 -2.95765468 0.33724942 0.79253138]
# [ 0.2386308 -1.0385805 -0.82818326 1.61327862 0.86117067]
# [-1.40692597 -1.65335482 0.25097572 -0.51377466 1.06806423]]
# [[ 4. -0. -1. 1. -1.]
# [-1. 2. -2. 1. 1.]
# [ 1. -1. -0. 2. 1.]
# [-1. -1. 1. -0. 2.]]
np.floor()
np.floor()用于数组元素向下取整
import numpy as np
data1 = np.random.randn(4,5)
print(data1)
print(np.floor(data1))
# [[ 0.92378641 1.35349128 0.57975044 1.20847343 0.67943746]
# [-1.19182311 0.57659921 0.60981519 1.42663667 0.49602901]
# [-0.3224854 0.72609288 0.65194753 2.05265749 -1.11113541]
# [-1.93028677 -1.34332758 -0.42339502 -0.46828546 -1.11569573]]
# [[ 0. 1. 0. 1. 0.]
# [-2. 0. 0. 1. 0.]
# [-1. 0. 0. 2. -2.]
# [-2. -2. -1. -1. -2.]]
np.rint()
np.rint()用于数组元素四舍五入取整
import numpy as np
data1 = np.random.randn(4,5)
print(data1)
print(np.rint(data1))
# [[ 0.32973071 -0.12852531 -0.29609136 0.1099612 -1.32187404]
# [-1.7301812 1.33769697 -0.08907079 -0.85909786 0.1095878 ]
# [ 0.69617615 0.21219543 -1.54136 -1.21383603 1.22538225]
# [-0.51724409 -0.06505319 0.23575689 0.12717578 -1.86922677]]
# [[ 0. -0. -0. 0. -1.]
# [-2. 1. -0. -1. 0.]
# [ 1. 0. -2. -1. 1.]
# [-1. -0. 0. 0. -2.]]
np.isnan()
np.isnan()用于数组元素进行空值判断
import numpy as np
data1 = np.array([1,2,np.nan,3,np.nan,5])
print(np.isnan(data1))
# [False False True False True False]
np.where()
np.where()用于添加condition(条件)来进行元素筛选,返回对应元素下标,也可设置a(满足条件的返回值),b(不满足条件的返回值)
import numpy as np
data1 = np.arange(15)
print(np.where(data1 > 5))
print(np.where(data1 > 5,'True','False'))
# (array([ 6, 7, 8, 9, 10, 11, 12, 13, 14]),)
# ['False' 'False' 'False' 'False' 'False' 'False' 'True' 'True' 'True'
# 'True' 'True' 'True' 'True' 'True' 'True']
当where判别的数组为二维时,where会分别返回符合要求的元素的行下标和列下标,一一组合就是符合要求的元素的下标
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(np.where(data1>5))
# (array([1, 1, 1, 1, 2, 2, 2, 2, 2]), array([1, 2, 3, 4, 0, 1, 2, 3, 4]))
np.any()
np.any()的参数设置为判别条件,若所有元素中有任意元素满足,则范围True,否则返回False
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(np.any(data1>5))
# True
np.all()
np.all()的参数设置为判别条件,若所有元素都满足,则范围True,否则返回False
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(np.all(data1>5))
# False
统计函数
下述统计函数的演示都会用二维矩阵进行演示,每个函数都有一个参数axis,取值范围为[0,ndim)表示不同维度,在二维矩阵中,若axis取值为0,则是延行统计每一列,若取值为1,则是延列统计每一行
np.sum()
求和运算
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.sum())
print(data1.sum(axis=0))
print(data1.sum(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 105
# [15 18 21 24 27]
# [10 35 60]
np.min()
统计最小值
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.min())
print(data1.min(axis=0))
print(data1.min(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 0
# [0 1 2 3 4]
# [ 0 5 10]
np.max()
统计最大值
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.max())
print(data1.max(axis=0))
print(data1.max(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 14
# [10 11 12 13 14]
# [ 4 9 14]
np.mean()
统计平均值
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.mean())
print(data1.mean(axis=0))
print(data1.mean(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 7.0
# [5. 6. 7. 8. 9.]
# [ 2. 7. 12.]
np.argmax()
统计最大值下标
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.argmax())
print(data1.argmax(axis=0))
print(data1.argmax(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 14
# [2 2 2 2 2]
# [4 4 4]
np.argmin()
统计最小值下标
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.argmin())
print(data1.argmin(axis=0))
print(data1.argmin(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 0
# [0 0 0 0 0]
# [0 0 0]
np.cumsum()
按位置统计到当前位置为止的累加值
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.cumsum())
print(data1.cumsum(axis=0))
print(data1.cumsum(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# [ 0 1 3 6 10 15 21 28 36 45 55 66 78 91 105]
# [[ 0 1 2 3 4]
# [ 5 7 9 11 13]
# [15 18 21 24 27]]
# [[ 0 1 3 6 10]
# [ 5 11 18 26 35]
# [10 21 33 46 60]]
np.cumprod()
按位置统计到当前位置为止的累乘值
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.cumprod())
print(data1.cumprod(axis=0))
print(data1.cumprod(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
# [[ 0 1 2 3 4]
# [ 0 6 14 24 36]
# [ 0 66 168 312 504]]
# [[ 0 0 0 0 0]
# [ 5 30 210 1680 15120]
# [ 10 110 1320 17160 240240]]
np.std()
统计标准差
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.std())
print(data1.std(axis=0))
print(data1.std(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 4.320493798938574
# [4.0824829 4.0824829 4.0824829 4.0824829 4.0824829]
# [1.41421356 1.41421356 1.41421356]
np.var()
统计方差
import numpy as np
data1 = np.arange(15).reshape(3, 5)
print(data1)
print(data1.var())
print(data1.var(axis=0))
print(data1.var(axis=1))
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
# 18.666666666666668
# [16.66666667 16.66666667 16.66666667 16.66666667 16.66666667]
# [2. 2. 2.]