次序统计
计算最小值
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.amin(x)
print(y) # 11
y = np.amin(x, axis=0)
print(y) # [11 12 13 14 15]
y = np.amin(x, axis=1)
print(y) # [11 16 21 26 31]
11
[11 12 13 14 15]
[11 16 21 26 31]
计算最大值
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.amax(x)
print(y) # 35
y = np.amax(x, axis=0)
print(y) # [31 32 33 34 35]
y = np.amax(x, axis=1)
print(y) # [15 20 25 30 35]
35
[31 32 33 34 35]
[15 20 25 30 35]
计算极差
import numpy as np
np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10 2 1 1 16]
# [18 11 10 14 10]
# [11 1 9 18 8]
# [16 2 0 15 16]]
print(np.ptp(x)) # 18
print(np.ptp(x, axis=0)) # [ 8 10 10 17 8]
print(np.ptp(x, axis=1)) # [15 8 17 16]
[[10 2 1 1 16]
[18 11 10 14 10]
[11 1 9 18 8]
[16 2 0 15 16]]
18
[ 8 10 10 17 8]
[15 8 17 16]
计算分位数
import numpy as np
np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10 2 1 1 16]
# [18 11 10 14 10]
# [11 1 9 18 8]
# [16 2 0 15 16]]
print(np.percentile(x, [25, 50]))
# [ 2. 10.]
print(np.percentile(x, [25, 50], axis=0))
# [[10.75 1.75 0.75 10.75 9.5 ]
# [13.5 2. 5. 14.5 13. ]]
print(np.percentile(x, [25, 50], axis=1))
# [[ 1. 10. 8. 2.]
# [ 2. 11. 9. 15.]]
[[10 2 1 1 16]
[18 11 10 14 10]
[11 1 9 18 8]
[16 2 0 15 16]]
[ 2. 10.]
[[10.75 1.75 0.75 10.75 9.5 ]
[13.5 2. 5. 14.5 13. ]]
[[ 1. 10. 8. 2.]
[ 2. 11. 9. 15.]]
均值和方差
计算中位数
import numpy as np
np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10 2 1 1 16]
# [18 11 10 14 10]
# [11 1 9 18 8]
# [16 2 0 15 16]]
print(np.percentile(x, 50))
print(np.median(x))
# 10.0
print(np.percentile(x, 50, axis=0))
print(np.median(x, axis=0))
# [13.5 2. 5. 14.5 13. ]
print(np.percentile(x, 50, axis=1))
print(np.median(x, axis=1))
# [ 2. 11. 9. 15.]
[[10 2 1 1 16]
[18 11 10 14 10]
[11 1 9 18 8]
[16 2 0 15 16]]
10.0
10.0
[13.5 2. 5. 14.5 13. ]
[13.5 2. 5. 14.5 13. ]
[ 2. 11. 9. 15.]
[ 2. 11. 9. 15.]
计算平均值
# 计算平均值(沿轴的元素的总和除以元素的数量)。
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.mean(x)
print(y) # 23.0
y = np.mean(x, axis=0)
print(y) # [21. 22. 23. 24. 25.]
y = np.mean(x, axis=1)
print(y) # [13. 18. 23. 28. 33.]
23.0
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]
计算加权平均值
# 计算加权平均值(将各数值乘以相应的权数,然后加总求和得到总体值,再除以总的单位数。)
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.average(x)
print(y) # 23.0
y = np.average(x, axis=0)
print(y) # [21. 22. 23. 24. 25.]
y = np.average(x, axis=1)
print(y) # [13. 18. 23. 28. 33.]
y = np.arange(1, 26).reshape([5, 5])
print(y)
# [[ 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]]
z = np.average(x, weights=y)
print(z) # 27.0
z = np.average(x, axis=0, weights=y)
print(z)
# [25.54545455 26.16666667 26.84615385 27.57142857 28.33333333]
z = np.average(x, axis=1, weights=y)
print(z)
# [13.66666667 18.25 23.15384615 28.11111111 33.08695652]
23.0
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]
[[ 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]]
27.0
[25.54545455 26.16666667 26.84615385 27.57142857 28.33333333]
[13.66666667 18.25 23.15384615 28.11111111 33.08695652]
计算方差
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.var(x)
print(y) # 52.0
y = np.mean((x - np.mean(x)) ** 2)
print(y) # 52.0
y = np.var(x, ddof=1)
print(y) # 54.166666666666664
y = np.sum((x - np.mean(x)) ** 2) / (x.size - 1)
print(y) # 54.166666666666664
y = np.var(x, axis=0)
print(y) # [50. 50. 50. 50. 50.]
y = np.var(x, axis=1)
print(y) # [2. 2. 2. 2. 2.]
52.0
52.0
54.166666666666664
54.166666666666664
[50. 50. 50. 50. 50.]
[2. 2. 2. 2. 2.]
计算标准差
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.std(x)
print(y) # 7.211102550927978
y = np.sqrt(np.var(x))
print(y) # 7.211102550927978
y = np.std(x, axis=0)
print(y)
# [7.07106781 7.07106781 7.07106781 7.07106781 7.07106781]
y = np.std(x, axis=1)
print(y)
# [1.41421356 1.41421356 1.41421356 1.41421356 1.41421356]
7.211102550927978
7.211102550927978
[7.07106781 7.07106781 7.07106781 7.07106781 7.07106781]
[1.41421356 1.41421356 1.41421356 1.41421356 1.41421356]
相关
计算协方差矩阵
import numpy as np
x = [1, 2, 3, 4, 6]
y = [0, 2, 5, 6, 7]
print(np.cov(x)) # 3.7 #样本方差
print(np.cov(y)) # 8.5 #样本方差
print(np.cov(x, y))
# [[3.7 5.25]
# [5.25 8.5 ]]
print(np.var(x)) # 2.96 #方差
print(np.var(x, ddof=1)) # 3.7 #样本方差
print(np.var(y)) # 6.8 #方差
print(np.var(y, ddof=1)) # 8.5 #样本方差
z = np.mean((x - np.mean(x)) * (y - np.mean(y))) #协方差
print(z) # 4.2
z = np.sum((x - np.mean(x)) * (y - np.mean(y))) / (len(x) - 1) #样本协方差
print(z) # 5.25
z = np.dot(x - np.mean(x), y - np.mean(y)) / (len(x) - 1) #样本协方差
print(z) # 5.25
3.7
8.5
[[3.7 5.25]
[5.25 8.5 ]]
2.96
3.7
6.8
8.5
4.2
5.25
5.25
计算相关系数
import numpy as np
np.random.seed(20200623)
x, y = np.random.randint(0, 20, size=(2, 4))
print(x) # [10 2 1 1]
print(y) # [16 18 11 10]
z = np.corrcoef(x, y)
print(z)
# [[1. 0.48510096]
# [0.48510096 1. ]]
a = np.dot(x - np.mean(x), y - np.mean(y))
b = np.sqrt(np.dot(x - np.mean(x), x - np.mean(x)))
c = np.sqrt(np.dot(y - np.mean(y), y - np.mean(y)))
print(a / (b * c)) # 0.4851009629263671
[10 2 1 1]
[16 18 11 10]
[[1. 0.48510096]
[0.48510096 1. ]]
0.4851009629263671
直方图
import numpy as np
x = np.array([0.2, 6.4, 3.0, 1.6])
bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
inds = np.digitize(x, bins)
print(inds) # [1 4 3 2]
for n in range(x.size):
print(bins[inds[n] - 1], "<=", x[n], "<", bins[inds[n]])
# 0.0 <= 0.2 < 1.0
# 4.0 <= 6.4 < 10.0
# 2.5 <= 3.0 < 4.0
# 1.0 <= 1.6 < 2.5
[1 4 3 2]
0.0 <= 0.2 < 1.0
4.0 <= 6.4 < 10.0
2.5 <= 3.0 < 4.0
1.0 <= 1.6 < 2.5
import numpy as np
x = np.array([1.2, 10.0, 12.4, 15.5, 20.])
bins = np.array([0, 5, 10, 15, 20])
inds = np.digitize(x, bins, right=True)
print(inds) # [1 2 3 4 4]
inds = np.digitize(x, bins, right=False)
print(inds) # [1 3 3 4 5]
[1 2 3 4 4]
[1 3 3 4 5]
练习
y=X β
from numpy . linalg import inv
from numpy import dot, transpose
X = [[1, 6, 2] , [1, 8, 1] , [1, 10, 0] , [1 , 14, 2] , [1, 18, 0]]
y = [[7] , [9] , [13] , [17.5] , [18]]
print(X)
print(y)
[[1, 6, 2], [1, 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0]]
[[7], [9], [13], [17.5], [18]]
print(dot(inv(dot(transpose(X) , X)) , dot(transpose(X) , y)))
[[1.1875 ]
[1.01041667]
[0.39583333]]
from numpy.linalg import lstsq
print(lstsq(X,y)[0])
[[1.1875 ]
[1.01041667]
[0.39583333]]
/tmp/ipykernel_1138478/3633125914.py:2: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
print(lstsq(X,y)[0])