python与医学统计中的代表值,离差的度量

【代表中央数据的度量】

【代表离差的度量】

【使用python计算例】
———————————–
# -*- coding: utf-8 -*-
import statistics as st
import scipy.stats as scist

# 腹围数据
s = [83, 82, 86, 84, 87, 79, 81, 85, 82, 92]
print(‘腹围数据(data) = ‘, s)
print(‘平均数(mean) = ‘, st.mean(s))
print(‘中位数(median) = ‘, st.median(s))
print(‘众数(mode) = ‘, st.mode(s))
print(‘几何平均数(geometric mean) = ‘, scist.gmean(s))
print(‘第一四分位数 (Q1) = ‘, scist.scoreatpercentile(s,25))
print(‘第三四分位数 (Q3) = ‘, scist.scoreatpercentile(s,75))
print(‘四分位距(interquartile range) = ‘, scist.scoreatpercentile(s,75)-scist.scoreatpercentile(s,25))
print(‘方差(Variance) = ‘, st.variance(s))
print(‘标准差(standard deviation) = ‘, st.stdev(s))
print(‘变异系数(coefficient of variation: CV) = ‘, st.stdev(s)/st.mean(s))

———————————–
结果:
———————————–
腹围数据(data) = [83, 82, 86, 84, 87, 79, 81, 85, 82, 92]
平均数(mean) = 84.1
中位数(median) = 83.5
众数(mode) = 82
几何平均数(geometric mean) = 84.029574619
第一四分位数 (Q1) = 82.0
第三四分位数 (Q3) = 85.75
四分位距(interquartile range) = 3.75
方差(Variance) = 13.433333333333332
标准差(standard deviation) = 3.665151201974256
变异系数(coefficient of variation: CV) = 0.043580870415865114

【使用python生成直方图例】
———————————–
from matplotlib import pyplot as plt
from math import log, ceil

# 腹围数据
s = [83, 82, 86, 84, 87, 79, 81, 85, 82, 92]
data = [s]
plt.rcParams[‘font.sans-serif’] = ‘FangSong’ # 仿宋
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_title(‘直方图’, size=16)
ax.set_xlabel(‘腹围’)
bs = int(ceil(1 + (log(len(s), 2)))) # 这里组数用史特基公式(Sturges’ formula)计算
ax.hist(s, bins=bs)
[xmin, xmax ,ymin, ymax] = plt.axis()
plt.axis([xmin – 1, xmax + 1,ymin, ymax + 1])
plt.show()
———————————–
hist3

【使用python生成箱线图例】
———————————–
from matplotlib import pyplot as plt

# 腹围数据
s = [83, 82, 86, 84, 87, 79, 81, 85, 82, 92]
data = [s]
lbl = [‘腹围’]
plt.rcParams[‘font.sans-serif’] = ‘FangSong’ # 仿宋
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_title(‘箱线图’, size=16)
plt.boxplot(data, labels=lbl)
plt.show()
———————————–

boxplot3

 2016-06-15

发表评论

要发表评论,您必须先登录