日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

假设检验实例(python)

發(fā)布時間:2023/12/20 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 假设检验实例(python) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 數(shù)據(jù)集下載地址:Journal of Statistics Education - Data Archive

? ? ? ? ?用ctrl+F鍵搜索normtemp下載txt文件

  • 數(shù)據(jù)集描述:http://ww2.amstat.org/publications/jse/datasets/normtemp.txt

? ? ? ? ?包括130條記錄,我們主要利用體溫和性別來進行實驗

import pandas as pd import pylab import math import numpy as np import matplotlib.pyplot as plt %matplotlib inline import numpy as np from scipy.stats import norm import scipy.stats import warnings warnings.filterwarnings("ignore")df = pd.read_csv('normtemp.txt',sep=' ',names = ['Temperature','Gender','Heart Rate']) df.describe()

? ? ? ? ? ? ? ? ? ??

?體溫的分布是正態(tài)的嗎?

observed_temperatures = df['Temperature'].sort_values()#將參數(shù)導入 #按從小到大的順序進行 bin_val = np.arange(start= observed_temperatures.min(), stop= observed_temperatures.max(), step = .05) mu, std = np.mean(observed_temperatures), np.std(observed_temperatures)#均值和方差p = norm.pdf(observed_temperatures, mu, std)#畫出pdfplt.hist(observed_temperatures,bins = bin_val, density=True, stacked=True)#直方圖 plt.plot(observed_temperatures, p, color = 'red') plt.xticks(np.arange(95.75,101.25,0.25),rotation=90) plt.xlabel('Human Body Temperature Distributions') plt.xlabel('human body temperature') plt.show()print('Average (Mu): '+ str(mu) + ' / ' 'Standard Deviation: '+str(std))

? ? ? ? ? ? ? ? ? ? ? ?

Average (Mu): 98.24923076923076 / Standard Deviation: 0.7303577789050376

?從圖中可以看出該分布符合正態(tài)分布的樣子。

再次進行檢驗:

x = observed_temperatures#Shapiro-Wilk Test: https://en.wikipedia.org/wiki/Shapiro%E2%80%93Wilk_test shapiro_test, shapiro_p = scipy.stats.shapiro(x) print("Shapiro-Wilk Stat:",shapiro_test, " Shapiro-Wilk p-Value:", shapiro_p)k2, p = scipy.stats.normaltest(observed_temperatures) print('p:',p)#Another method to determining normality is through Quantile-Quantile Plots. scipy.stats.probplot(observed_temperatures, dist="norm", plot=pylab) pylab.show()

?算出p值為0.2587479863488212

Shapiro-Wilk Stat: 0.9865769743919373 Shapiro-Wilk p-Value: 0.2331680953502655 p: 0.2587479863488212

? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

?畫出ECDF:

def ecdf(data):#Compute ECDFn = len(data)x = np.sort(data)y = np.arange(1, n+1) / nreturn x, y# Compute empirical mean and standard deviation# Number of samples n = len(df['Temperature']) # Sample mean mu = np.mean(df['Temperature']) # Sample standard deviation std = np.std(df['Temperature']) print('Mean temperature: ', mu, 'with standard deviation of +/-', std)#Random sampling of the data based off of the mean of the data. normalized_sample = np.random.normal(mu, std, size=10000) x_temperature, y_temperature = ecdf(df['Temperature']) normalized_x, normalized_y = ecdf(normalized_sample)# Plot the ECDFs fig = plt.figure(figsize=(8, 5)) plt.plot(normalized_x, normalized_y) plt.plot(x_temperature, y_temperature, marker='.', linestyle='none') plt.ylabel('ECDF') plt.xlabel('Temperature') plt.legend(('Normal Distribution', 'Sample data'))

? ? ? ? ? ? ? ?

Mean temperature: 98.24923076923076 with standard deviation of +/- 0.730357778905038

可以看出上下波動的值很低。

有學者提出98.6是人類的平均體溫,我們該這樣認為嗎?

在這里我們選擇t檢驗,因為我們只能計算樣本的標準差。

from scipy import statsCW_mu = 98.6 stats.ttest_1samp(df['Temperature'], CW_mu, axis=0) Ttest_1sampResult(statistic=-5.4548232923640771, pvalue=2.4106320415610081e-07)

T-Stat -5.454 p-value近乎0了. 我們該拒絕這樣的假設

?男性和女性的體溫有明顯差異嗎?

兩獨立樣本t檢驗 H0: 沒有明顯差異 H1: 有明顯差異

female_temp = df.Temperature[df.Gender == 2] male_temp = df.Temperature[df.Gender == 1] mean_female_temp = np.mean(female_temp) mean_male_temp = np.mean(male_temp) print('Average female body temperature = ' + str(mean_female_temp)) print('Average male body temperature = ' + str(mean_male_temp))# Compute independent t-test stats.ttest_ind(female_temp, male_temp, axis=0) Average female body temperature = 98.39384615384616 Average male body temperature = 98.1046153846154 Ttest_indResult(statistic=2.2854345381654984, pvalue=0.02393188312240236)

由于P值=0.024 < 0.05,我們需要拒絕原假設,我們有%95的自信認為是有差異的!

總結(jié)

以上是生活随笔為你收集整理的假设检验实例(python)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。