Numpy 学习专题(八)—— 统计相关内容

一、次序统计

1.计算最值

  • 计算最小值
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.min(x)
print(y)  # 11

y = np.min(x, axis=0)  # 求每列最小值
print(y)  # [11 12 13 14 15]

y = np.min(x, axis=1)  # 求每行最小值
print(y)  # [11 16 21 26 31]
  • 计算最大值
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.max(x)
print(y)  # 35

y = np.max(x, axis=0)  # 求每列最大值
print(y)  # [31 32 33 34 35]

y = np.max(x, axis=1)  # 求每行最大值
print(y)  # [15 20 25 30 35]

2.计算极差

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]

3.计算分位数

np.random.seed(20201126)

x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[ 5 16  4  6  6]
# [ 8  4  6  0 15]
# [18 18 15 19  5]
# [18  1  2 11 10]]

print(np.percentile(x, [25, 50]))  
# [4.75 7.  ]

print(np.percentile(x, [25, 50], axis=0))
# [[ 7.25  3.25  3.5   4.5   5.75]
#  [13.   10.    5.    8.5   8.  ]]

print(np.percentile(x, [25, 50], axis=1))
# [[ 5.  4. 15.  2.]
# [ 6.  6. 18. 10.]]

二、均值与方差

1.计算中位数

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.median(x))
# 10.0

print(np.median(x, axis=0))
# [13.5  2.   5.  14.5 13. ]

print(np.median(x, axis=1))
# [ 2. 11.  9. 15.]

2.计算平均值

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25]])
              
y = np.mean(x)
print(y)  # 18.0

y = np.mean(x, axis=0)
print(y)  # [16. 17. 18. 19. 20.]

y = np.mean(x, axis=1)
print(y)  # [13. 18. 23.]

3.计算加权平均值

x = np.arange(11, 20).reshape([3, 3])
print(x)
#[[11 12 13]
# [14 15 16]
# [17 18 19]]

y = np.arange(1, 10).reshape([3, 3])
print(y)
#[[1 2 3]
# [4 5 6]
# [7 8 9]]

z = np.average(x, weights=y)  # 整个数组加权
print(z)  # 16.333333333333332

z = np.average(x, axis=0, weights=y)  # 每列加权平均值
print(z)
# [15.5 16.2 17. ]

z = np.average(x, axis=1, weights=y)  # 每行加权平均值
print(z)
# [12.33333333 15.13333333 18.08333333]

4.计算方差

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, axis=0)  # 每列方差
print(y)  # [50. 50. 50. 50. 50.]

y = np.var(x, axis=1)  # 每行方差
print(y)  # [2. 2. 2. 2. 2.]

三、相关

1.计算协方差矩阵

在这里插入图片描述

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

2.计算相关系数

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.48510096
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值