学习内容:
1、人体体温的总体均值是否为98.6华氏度?
2、人体的温度是否服从正态分布?
3、人体体温中存在的异常数据是哪些?
4、男女体温是否存在明显差异?
5、体温与心率间的相关性(强?弱?中等?)
练手数据:https://pan.baidu.com/s/1mcq3d1V9bqzNftcseLEqbQ
一、总体均值估计
import pandas as pd
import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
path = "C:\\Users\\Administrator\\Desktop\\Notebook\\test.csv"
test = pd.read_csv(path)
temperature = test['Temperature']
temperature.mean()
二、正态分布
# 查看人体体温样本概括性信息
temperature.describe()
# 绘制人体体温样本曲线
import seaborn as sns
sns.set_palette("hls") #设置所有图的颜色,使用hls色彩空间
sns.distplot(test['Temperature'],color="r",bins=10,kde=True)
plt.title('Temperature')
plt.xlim(94,102)
plt.grid(True)
plt.show()
#分别用kstest、shapiro、normaltest来验证人体体温分布系数
from scipy import stats
ks_test = stats.kstest(test['Temperature'], 'norm')
shapiro_test = stats.shapiro(test['Temperature'])
normaltest_test = stats.normaltest(test['Temperature'],axis=0)
print('ks_test:',ks_test)
print('shapiro_test:',shapiro_test)
print('normaltest_test:',normaltest_test)
#绘制拟合正态分布曲线
temperature = test['Temperature']
plt.figure()
temperature.plot(kind = 'kde')
M_S = stats.norm.fit(temperature) #正态分布拟合的平均值loc,标准差scale
normalDistribution = stats.norm(M_S[0],M_S[1]) #绘制拟合的正态分布图
x = np.linspace(normalDistribution.ppf(0.01),normalDistribution.ppf(0.99),100)
plt.plot(x,normalDistribution.pdf(x),c='orange')
plt.xlabel('Temperature')
plt.title('Atemperature on NormalDistribution',size=20)
plt.legend(['Temperature','NormalDistribution'])
三、异常数据
- 置信区间
# 正态分布下的置信区间
def norm_conf (temperature,confidence=0.997):
sample_mean = np.mean(temperature)
sample_std = np.std(temperature,ddof=1)
sample_size = len(temperature)
conf_intveral = stats.norm.interval(confidence, loc=sample_mean, scale=sample_std)
print(conf_intveral)
from scipy import stats
norm_conf(temperature)
- 四分位数
在这里插入代码片# 计算上下四分位数
Q1 = temperature.quantile(q = 0.25)
Q3 = temperature.quantile(q = 0.75)
#异常值判断标准, 1.5倍的四分位差 计算上下须对应的值
low_quantile = Q1 - 1.5*(Q3-Q1)
high_quantile = Q3 + 1.5*(Q3-Q1)
#输出异常值
value = temperature[(temperature > high_quantile) | (temperature < low_quantile)]
print(value)
#############################################################
作业脱离主题了,这周是假设验证的实践,参考XX同学的作业
https://blog.csdn.net/macmurphy/article/details/103866748
#############################################################
Q1.人体体温的总体均值是否为98.6华氏度?
#Q1人体体温的总体均值是否为98.6华氏度?
#H0:人体体温的总体均值是98.6华氏度
#H1:人体体温的总体均值不是98.6华氏度
import pandas as pd
import numpy as np
from scipy import stats
path = "C:\\Users\\Administrator\\Desktop\\Notebook\\test.csv"
data = pd.read_csv(path)
Temperature = list(data['Temperature'])
Temperature_mean = np.mean(Temperature)
Temperature_std = np.std(Temperature,ddof=1)
Temperature_intveral = stats.norm.interval(0.95,Temperature_mean,Temperature_std)
print(Temperature_intveral)
Q2.人体的温度是否服从正态分布?
#Q2人体的温度是否服从正态分布?
#H0:人体的温度服从正态分布
#H1:人体的温度不服从正态分布
import pandas as pd
import numpy as np
from scipy import stats
path = "C:\\Users\\Administrator\\Desktop\\Notebook\\test.csv"
data = pd.read_csv(path)
Temperature = list(data['Temperature'])
Temperature_mean = np.mean(Temperature)
Temperature_std = np.std(Temperature,ddof=1)
Temperature_test=stats.normaltest(Temperature)
print(Temperature_test)
Q3.人体体温中存在的异常数据是哪些?
#Q3人体体温中存在的异常数据是哪些?
import pandas as pd
import numpy as np
from scipy import stats
path = "C:\\Users\\Administrator\\Desktop\\Notebook\\test.csv"
data = pd.read_csv(path)
Temperature = list(data['Temperature'])
Temperature_mean = np.mean(Temperature)
Temperature_std = np.std(Temperature,ddof=1)
lower_point = Temperature_mean - (Temperature_std*3)
high_point = Temperature_mean + (Temperature_std*3)
print("异常数据下限:",lower_point)
print("异常数据上限:",high_point)
print("异常数据为:")
data[data['Temperature']<lower_point]
data[data['Temperature']>high_point]
Q4.男女体温是否存在明显差异?
#Q4男女体温是否存在明显差异?
#H0:男女体温存在明显差异
#H1:男女体温不存在明显差异
import pandas as pd
import numpy as np
from scipy import stats
path = "C:\\Users\\Administrator\\Desktop\\Notebook\\test.csv"
data = pd.read_csv(path)
Temperature_male=list(data[data["Gender"]==1]["Temperature"])
Temperature_female=list(data[data['Gender']==2]['Temperature'])
#两样本方差比较
levene=stats.levene(Temperature_male,Temperature_female)
print(levene)
#两独立样本t检验就是根据样本数据对两个样本来自的两独立总体的均值是否有显著差异进行推断;
#进行两独立样本t检验的条件是,两样本的总体相互独立且符合正态分布。
Temperature_sex=stats.ttest_ind(Temperature_male,Temperature_female)
print(Temperature_sex)
Q5.体温与心率间的相关性(强?弱?中等?)
#Q5体温与心率间的相关性(强?弱?中等?)
import pandas as pd
import numpy as np
from scipy import stats
path = "C:\\Users\\Administrator\\Desktop\\Notebook\\test.csv"
data = pd.read_csv(path)
data.head(5)
data.describe()
T_H_pearsonr=stats.pearsonr(data['Temperature'],data['HeartRate'])
print(T_H_pearsonr)
#############################################################################################
@ 2020.01.11 木居居士的统计学小组 第十一周 打卡
安利公益监督学习组织 - 【公众号】数据科学家联盟
https://mp.weixin.qq.com/s/1WWmbLZucz9vIp-4tKKQ5Q
感谢木东大佬、饼干大佬、南头大佬、星空妹砸、Desitiny、 DD-Kylin的无私付出,抱拳ing~