包含内容
:均值、方差、标准差、变异系数、偏度、峰度、中位数、上、下四分位数、四分位极差、直方图、茎叶图、三均值、正态性分析与检验。
126 149 143 141 127 123 137 132 135 134 146 142
135 141 150 137 144 137 134 139 148 144 142 137
147 138 140 132 149 131 139 142 138 145 147 137
135 142 151 146 129 120 143 145 142 136 147 128
142 132 138 139 147 128 139 146 139 131 138 149
先将数据集保存为
1.1.txt
。
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
from itertools import groupby
height = np.loadtxt('1.2.txt')
print('均值',np.mean(height))
print('方差',np.var(height))
print('标准差',np.std(height))
print('变异系数',np.std(height)*100 / np.mean(height))
print('偏度',st.skew(height))
print('峰度',st.kurtosis(height))
结果
:
均值 139.0
方差 49.06666666666667
标准差 7.004760286167305
变异系数 5.039395889328997
偏度 -0.4972356172167568
峰度 -0.21422162984404558
print('中位数',np.median(height))
print('上四分位数',np.quantile(height,0.75))
print('下四分位数',np.quantile(height, 0.25))
print('四分位极差',np.quantile(height,0.75)-np.quantile(height, 0.25))
print('三均值',np.median(height)/2+np.quantile(height,0.75)/4+np.quantile(height, 0.25)/4)
结果
:
中位数 139.0
上四分位数 144.25
下四分位数 135.0
四分位极差 9.25
三均值 139.3125
直方图
:
plt.bar(list(range(1,60+1)),np.array(height));
plt.title('studentHeights')
茎叶图
:
for k, g in groupby(sorted(height.astype(int)), key=lambda x: int(x) // 10):
lst = map(str, [int(y) % 10 for y in list(g)])
print (k, '|', ' '.join(lst))
图示
:
12 | 0 3 6 7 8 8 9
13 | 1 1 2 2 2 4 4 5 5 5 6 7 7 7 7 7 8 8 8 8 9 9 9 9 9
14 | 0 1 1 2 2 2 2 2 2 3 3 4 4 5 5 6 6 6 7 7 7 7 8 9 9 9
15 | 0 1
print('正态性检验',st.kstest(height, 'norm', (np.mean(height), np.std(height))))
结果
:
正态性检验 KstestResult(statistic=0.08762284592013109, pvalue=0.713116118134703)
结论
:
p p p值大于0.05,接受假设:该数据服从正态分布。