<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python實現(xiàn)常用的假設檢驗 !

          共 2860字,需瀏覽 6分鐘

           ·

          2020-08-01 00:40

          開門見山。

          這篇文章,教大家用Python實現(xiàn)常用的假設檢驗



          服從什么分布,就用什么區(qū)間估計方式,也就就用什么檢驗!

          比如:兩個樣本方差比服從F分布,區(qū)間估計就采用F分布計算臨界值(從而得出置信區(qū)間),最終采用F檢驗。



          建設檢驗的基本步驟:




          前言


          假設檢驗用到的Python工具包

          • Statsmodels是Python中,用于實現(xiàn)統(tǒng)計建模和計量經(jīng)濟學的工具包,主要包括描述統(tǒng)計、統(tǒng)計模型估計和統(tǒng)計推斷
          • Scipy是一個數(shù)學、科學和工程計算Python工具包,主要包括統(tǒng)計,優(yōu)化,整合,線性代數(shù)等等與科學計算有關的包

          導入數(shù)據(jù)
          from sklearn.datasets import load_irisimport numpy as np#導入IRIS數(shù)據(jù)集iris = load_iris()iris=pd.DataFrame(iris.data,columns=['sepal_length','sepal_width','petal_legth','petal_width'])print(iris)

          一個總體均值的z檢驗
          np.mean(iris['petal_legth'])'''原假設:鳶尾花花瓣平均長度是4.2備擇假設:鳶尾花花瓣平均長度不是4.2
          '''
          import statsmodels.stats.weightstatsz, pval = statsmodels.stats.weightstats.ztest(iris['petal_legth'], value=4.2)print(z,pval)

          '''P=0.002 <5%, 拒絕原假設,接受備則假設。'''

          一個總體均值的t檢驗
          import scipy.statst, pval = scipy.stats.ttest_1samp(iris['petal_legth'], popmean=4.0)print(t, pval)
          '''P=0.0959 > 5%, 接受原假設,即花瓣長度為4.0。'''


          模擬雙樣本t檢驗

          #取兩個樣本iris_1 = iris[iris.petal_legth >= 2]iris_2 = iris[iris.petal_legth < 2]print(np.mean(iris_1['petal_legth']))print(np.mean(iris_2['petal_legth']))
          '''H0: 兩種鳶尾花花瓣長度一樣H1: 兩種鳶尾花花瓣長度不一樣
          '''
          import scipy.statst, pval = scipy.stats.ttest_ind(iris_1['petal_legth'],iris_2['petal_legth'])print(t,pval)
          '''p<0.05,拒絕H0,認為兩種鳶尾花花瓣長度不一樣'''


          ?練習


          數(shù)據(jù)字段說明:
          • gender:性別,1為男性,2為女性
          • Temperature:體溫
          • HeartRate:心率
          • 共130行,3列
          • 用到的數(shù)據(jù)鏈接:pan.baidu.com/s/1t4SKF6

          本周需要解決的幾個小問題:

          1. 人體體溫的總體均值是否為98.6華氏度?

          2. 人體的溫度是否服從正態(tài)分布?

          3. 人體體溫中存在的異常數(shù)據(jù)是哪些?

          4. 男女體溫是否存在明顯差異?

          5. 體溫與心率間的相關性(強?弱?中等?)


          1.1 探索數(shù)據(jù)

          import numpy as npimport pandas as pdfrom scipy import statsdata = pd.read_csv("C:\\Users\\baihua\\Desktop\\test.csv")print(data.head())sample_size = data.size #130*3out:   Temperature  Gender  HeartRate0         96.3       1         701         96.7       1         712         96.9       1         743         97.0       1         804         97.1       1         73
          print(data.describe())out: Temperature Gender HeartRatecount 130.000000 130.000000 130.000000mean 98.249231 1.500000 73.761538std 0.733183 0.501934 7.062077min 96.300000 1.000000 57.00000025% 97.800000 1.000000 69.00000050% 98.300000 1.500000 74.00000075% 98.700000 2.000000 79.000000max 100.800000 2.000000 89.000000
          人體體溫均值是98.249231

          1.2 人體的溫度是否服從正態(tài)分布?

          '''人體的溫度是否服從正態(tài)分布?先畫出分布的直方圖,然后使用scipy.stat.kstest函數(shù)進行判斷。
          '''%matplotlib inlineimport seaborn as snssns.distplot(data['Temperature'], color='b', bins=10, kde=True)

          stats.kstest(data['Temperature'], 'norm')out:KstestResult(statistic=1.0, pvalue=0.0)'''p<0.05,不符合正態(tài)分布'''

          判斷是否服從t分布
          '''判斷是否服從t分布:
          '''
          np.random.seed(1)ks = stats.t.fit(data['Temperature'])df = ks[0]loc = ks[1]scale = ks[2]t_estm = stats.t.rvs(df=df, loc=loc, scale=scale, size=sample_size)stats.ks_2samp(data['Temperature'], t_estm)
          '''
          pvalue=0.4321464176976891 <0.05,認為體溫服從t分布'''

          判斷是否服從卡方分布
          '''判斷是否服從卡方分布:
          '''np.random.seed(1)chi_square = stats.chi2.fit(data['Temperature'])df = chi_square[0]loc = chi_square[1]scale = chi_square[2]chi_estm = stats.chi2.rvs(df=df, loc=loc, scale=scale, size=sample_size)stats.ks_2samp(data['Temperature'], chi_estm)
          '''pvalue=0.3956146564478842>0.05,認為體溫服從卡方分布
          '''

          繪制卡方分布直方圖
          '''繪制卡方分布圖
          '''
          from matplotlib import pyplot as pltplt.figure()data['Temperature'].plot(kind = 'kde')chi2_distribution = stats.chi2(chi_square[0], chi_square[1],chi_square[2])x = np.linspace(chi2_distribution.ppf(0.01), chi2_distribution.ppf(0.99), 100)plt.plot(x, chi2_distribution.pdf(x), c='orange')plt.xlabel('Human temperature')plt.title('temperature on chi_square', size=20)plt.legend(['test_data', 'chi_square'])


          1.3 人體體溫中存在的異常數(shù)據(jù)是哪些?

          '''已知體溫數(shù)據(jù)服從卡方分布的情況下,可以直接使用Python計算出P=0.025和P=0.925時(該函數(shù)使用單側(cè)概率值)的分布值,在分布值兩側(cè)的數(shù)據(jù)屬于小概率,認為是異常值。'''lower1=chi2_distribution.ppf(0.025)lower2=chi2_distribution.ppf(0.925)t=data['Temperature']print(t[t )print(t[t>lower2])
          out:
          0 96.31 96.765 96.466 96.767 96.8Name: Temperature, dtype: float6463 99.464 99.5126 99.4127 99.9128 100.0129 100.8Name: Temperature, dtype: float64

          1.4 男女體溫差異是否顯著

          '''此題是一道兩個總體均值之差的假設檢驗問題,因為是否存在差別并不涉及方向,所以是雙側(cè)檢驗。建立原假設和備擇假設如下:H0:u1-u2 =0  沒有顯著差H1:u1-u2 != 0  有顯著差別
          '''data.groupby(['Gender']).size() #樣本量65male_df = data.loc[data['Gender'] == 1]female_df = data.loc[data['Gender'] == 2]
          '''使用Python自帶的函數(shù),P用的雙側(cè)累計概率'''

          import scipy.statst, pval = scipy.stats.ttest_ind(male_df['Temperature'],female_df['Temperature'])print(t,pval)if pval > 0.05: print('不能拒絕原假設,男女體溫無明顯差異。')else: print('拒絕原假設,男女體溫存在明顯差異。')
          out:-2.2854345381654984 0.02393188312240236拒絕原假設,男女體溫存在明顯差異。

          1.5 體溫與心率間的相關性(強?弱?中等?)

          '''
          體溫與心率間的相關性(強?弱?中等?)
          '''
          heartrate_s = data['HeartRate']temperature_s = data['Temperature']from matplotlib import pyplot as pltplt.scatter(heartrate_s, temperature_s)


          stat, p = stats.pearsonr(heartrate_s, temperature_s)print('stat=%.3f, p=%.3f' % (stat, p))print(stats.pearsonr(heartrate_s, temperature_s))
          '''相關系數(shù)為0.004,可以認為二者之間沒有相關性
          '''


          End.
          作者:求知鳥
          來源:知乎

          瀏覽 64
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天澡天天狠天天天做 | 18毛片视频| 偷拍自拍第一页 | 一區二區三區色 | 久久久久三级片 |