作者 | Jose Garcia
譯者 | 張睿毅
校對 | 張一豪、林亦霖
編輯 | 于騰凱
來源 | 數(shù)據(jù)派THU(ID:DatapiTHU)
【導(dǎo)讀】本文中,作者給出了假設(shè)檢驗的解讀與Python實現(xiàn)的詳細(xì)的假設(shè)檢驗中的主要操作。
也許所有機器學(xué)習(xí)的初學(xué)者,或者中級水平的學(xué)生,或者統(tǒng)計專業(yè)的學(xué)生,都聽說過這個術(shù)語,假設(shè)檢驗。 我將簡要介紹一下這個當(dāng)我學(xué)習(xí)時給我?guī)砹寺闊┑闹黝}。我把所有這些概念放在一起,并使用python進(jìn)行示例。
在我尋求更廣泛的事情之前要考慮一些問題?—— 什么是假設(shè)檢驗?我們?yōu)槭裁从盟渴裁词羌僭O(shè)的基本條件?什么是假設(shè)檢驗的重要參數(shù)?
讓我們一個個地開始吧:
1、?什么是假設(shè)檢驗?
假設(shè)檢驗是一種統(tǒng)計方法,用于使用實驗數(shù)據(jù)進(jìn)行統(tǒng)計決策。假設(shè)檢驗基本上是我們對人口參數(shù)做出的假設(shè)。
例如:你說班里的學(xué)生平均年齡是40歲,或者一個男生要比女生高。
我們假設(shè)所有這些例子都需要一些統(tǒng)計方法來證明這些。無論我們假設(shè)什么是真的,我們都需要一些數(shù)學(xué)結(jié)論。
2、我們?yōu)槭裁匆盟?
假設(shè)檢驗是統(tǒng)計學(xué)中必不可少的過程。假設(shè)檢驗評估關(guān)于總體的兩個相互排斥的陳述,以確定樣本數(shù)據(jù)最佳支持哪個陳述。當(dāng)我們說一個發(fā)現(xiàn)具有統(tǒng)計學(xué)意義時,這要歸功于一個假設(shè)檢驗。
3、什么是假設(shè)的基本條件?
?不同均值和方差下的正態(tài)分布
假設(shè)的基礎(chǔ)是規(guī)范化和標(biāo)準(zhǔn)規(guī)范化
https://en.wikipedia.org/wiki/Normalization_(statistics);https://stats.stackexchange.com/questions/10289/whats——the——difference——between——normalization——and——standardization
我們所有的假設(shè)都圍繞這兩個術(shù)語的基礎(chǔ)。讓我們看看這些。
標(biāo)準(zhǔn)化的正態(tài)曲線圖像和數(shù)據(jù)分布及每個部分的百分比
你一定想知道這兩個圖像之間有什么區(qū)別,有人可能會說我找不到,而其他人看到的圖像會比較平坦,而不是陡峭的。好吧伙計這不是我想要表達(dá)的,首先你可以看到有不同的正態(tài)曲線所有那些正態(tài)曲線可以有不同的均值和方差,如第二張圖像,如果你注意到圖形是合理分布的,總是均值= 0和方差= 1。當(dāng)我們使用標(biāo)準(zhǔn)化的正態(tài)數(shù)據(jù)時,z—score的概念就出現(xiàn)了。
正態(tài)分布
如果變量的分布具有正態(tài)曲線的形狀——一個特殊的鐘形曲線,則該變量被稱為正態(tài)分布或具有正態(tài)分布。正態(tài)分布圖稱為正態(tài)曲線,它具有以下所有屬性:1.均值,中位數(shù)和眾數(shù)是相等。
正態(tài)分布方程
標(biāo)準(zhǔn)化正態(tài)分布
標(biāo)準(zhǔn)正態(tài)分布是平均值為0,標(biāo)準(zhǔn)差為1的正態(tài)分布
4、哪些是假設(shè)檢驗的重要參數(shù)?
-
零假設(shè):
-
?備擇假設(shè):
-
T校驗(學(xué)生T校驗)
-
Z校驗
-
ANOVA校驗
-
卡方檢驗
鏈接:?https://www.investopedia.com/terms/v/variance.asp
鏈接:?https://www.investopedia.com/terms/h/hypothesistesting.asp
-
單樣本t檢驗
-
雙樣本t檢驗
from scipy.stats import ttest_1sampimport
numpy as npages = np.genfromtxt
(“ages.csv”)print(ages)ages_mean = np.mean(ages)
print(ages_mean)tset, pval = ttest_1samp(ages, 30)
print(“p-values”,pval)if pval < 0.05: # alpha value is 0.05 or 5% print
(" we are rejecting null hypothesis")else:
print("we are accepting null hypothesis”)
from scipy.stats import ttest_indimport numpy as npweek1 = np.genfromtxt
("week1.csv", delimiter=",")
week2 = np.genfromtxt
("week2.csv", delimiter=",")print(week1)
print("week2 data :-\n")print(week2)
week1_mean = np.mean(week1
)week2_mean = np.mean(week2)print
("week1 mean value:",week1_mean)print
("week2 mean value:",week2_mean)
week1_std = np.std(week1)week2_std =
np.std(week2)print("week1 std value:",week1_std)
print("week2 std value:",week2_std)
ttest,pval = ttest_ind(week1,week2)print
("p-value",pval)if pval <0.05: print
("we reject null hypothesis")else: print("we accept null hypothesis”)
import pandas as pd
from scipy import stats
df = pd.read_csv("blood_pressure.csv")
df[['bp_before','bp_after']].describe()
ttest,pval = stats.ttest_rel(df['bp_before'], df['bp_after'])
print(pval)
if pval<0.05:
print("reject null hypothesis")
else:
print("accept null hypothesis")
鏈接:https://www.statisticshowto.datasciencecentral.com/ probability——and——statistics/hypothesis——testing/f——test/https://www.statisticshowto.datasciencecentral.com/probability——and——statistics/chi——square/https://www.statisticshowto. datasciencecentral.com/probability——and——statistics/t——test/?
-
您的樣本量大于30, 否則,請使用t檢驗。
鏈接:?https://www.statisticshowto.datasciencecentral.com/probability——and——statistics/find——sample——size/
-
數(shù)據(jù)點應(yīng)彼此獨立, 換句話說,一個數(shù)據(jù)點不相關(guān)或不影響另一個數(shù)據(jù)點。
鏈接:?https://www.statisticshowto.datasciencecentral.com/probability——and——statistics/dependent——events——independent/
-
您的數(shù)據(jù)應(yīng)該是正常分布的。但是,對于大樣本量(超過30個),這并不總是重要的。
-
您的數(shù)據(jù)應(yīng)從人口中隨機選擇,每個項目都有相同的選擇機會。
-
如果可能的話,樣本量應(yīng)該相等。
import pandas as pd
from scipy import statsfrom statsmodels.stats
import weightstats as stestsztest ,pval = stests.ztest(df['bp_before'], x2=None, value=156)
print(float(pval))if pval<0.05:
print("reject null hypothesis")
else:
print("accept null hypothesis")
ztest ,pval1 = stests.ztest(df['bp_before'],
x2=df['bp_after'],
value=0,alternative='two-sided')print(float(pval1))if pval<0.05:
print("reject null hypothesis")else: print("accept null hypothesis")
鏈接:https://en.wikipedia.org/ wiki/Analysis_of_variance
df_anova = pd.read_csv('PlantGrowth.csv')
df_anova = df_anova[['weight','group']]grps = pd.unique(df_anova.group.values)
d_data = {grp:df_anova['weight'][df_anova.group == grp] for grp in grps}
F, p = stats.f_oneway(d_data['ctrl'], d_data['trt1'], d_data['trt2'])
print("p-value for significance is: ", p)
if p<0.05:
print("reject null hypothesis")
else:
print("accept null hypothesis")
鏈接:https://stattrek.com/Help/Glossary.aspx? Target=Categorical%20variable
import statsmodels.api as sm
from statsmodels.formula.api import olsdf_anova2 =
pd.read_csv
("https://raw.githubusercontent.com/Opensourcefordatascience/Data-sets/master/crop_yield.csv")
model = ols('Yield ~ C(Fert)*C(Water)'
, df_anova2).fit()print(f"Overall model F
({model.df_model: .0f},{model.df_resid: .0f}) = {model.fvalue: .3f}, p = {model.f_pvalue: .4f}")
res = sm.stats.anova_lm(model, typ= 2)res
鏈接:https://stattrek.com/Help/ Glossary.aspx?Target=Categorical%20variable
df_chi = pd.read_csv('chi-test.csv')
contingency_table=pd.crosstab(df_chi["Gender"],df_chi["Shopping?"])
print('contingency_table :-\n',contingency_table)
#Observed ValuesObserved_Values = contingency_table.values print
("Observed Values :
\n",Observed_Values)b=stats.chi2_contingency(contingency_table)
Expected_Values = b[3]print
("Expected Values :-\n",Expected_Values)
no_of_rows=len(contingency_table.iloc[0:2,0])
no_of_columns=len(contingency_table.iloc[0,0:2])ddof=(no_of_rows-1)*(no_of_columns-1)print
("Degree of Freedom:-",ddof
)alpha = 0.05from scipy.stats import chi2chi_square=sum([(o-e)
**2./e for o,e in zip(Observed_Values,Expected_Values)])
chi_square_statistic=chi_square[0]+chi_square[1]print
("chi-square statistic:-",chi_square_statistic)
critical_value=chi2.ppf(q=1-alpha,df=ddof)print
('critical_value:',critical_value)
#p-valuep_value=1-chi2.cdf(x=chi_square_statistic,df=ddof)
print('p-value:',p_value)print('Significance level: ',alpha)
print('Degree of Freedom: ',ddof)
print('chi-square statistic:',chi_square_statistic)
print('critical_value:',critical_value)print('p-value:',p_value)
if chi_square_statistic>=critical_value: print
("Reject H0,There is a relationship
between 2 categorical variables")
else: print("Retain H0,There is no relationship
between 2 categorical variables")
if p_value<=alpha: print
("Reject H0,There is a relationship
between 2 categorical variables")else: print
("Retain H0,There is no relationship between 2 categorical variables")
譯者介紹: 張睿毅,北京郵電大學(xué)大二物聯(lián)網(wǎng)在讀。我是一個愛自由的人。在郵電大學(xué)讀第一年書我就四處跑去蹭課,折騰整一年驚覺,與其在當(dāng)下焦慮,不如在前輩中沉淀。于是在大二以來,堅持讀書,不敢稍歇。資本主義國家的科學(xué)觀不斷刷新我的認(rèn)知框架,同時因為出國考試很早出分,也更早地感受到自己才是那個一直被束縛著的人。太多真英雄在社會上各自閃耀著光芒。這才開始,立志終身向遇到的每一個人學(xué)習(xí)。做一個純粹的計算機科學(xué)里面的小學(xué)生。喜歡 算法,數(shù)據(jù)挖掘,圖像識別,自然語言處理,神經(jīng)網(wǎng)絡(luò),人工智能等方向。
原文鏈接:
https://towardsdatascience.com/hypothesis-testing-in-machine-learning-using-python-a0dc89e169ce
◆
精彩推薦
◆
倒計時!由易觀攜手CSDN聯(lián)合主辦的第三屆易觀算法大賽還剩 7 天,冠軍團隊將獲得3萬元!
本次比賽主要預(yù)測訪問平臺的相關(guān)事件的PV,UV流量 (包括Web端,移動端等),大賽將會提供相應(yīng)事件的流量數(shù)據(jù),以及對應(yīng)時間段內(nèi)的所有事件明細(xì)表和用戶屬性表等數(shù)據(jù),進(jìn)行模型訓(xùn)練,并用訓(xùn)練好的模型預(yù)測規(guī)定日期范圍內(nèi)的事件流量。
推薦閱讀
-
知乎算法團隊負(fù)責(zé)人孫付偉: Graph?Embedding在知乎的應(yīng)用實踐
-
必看,61篇NeurIPS深度強化學(xué)習(xí)論文解讀都這里了
-
打破深度學(xué)習(xí)局限,強化學(xué)習(xí)、深度森林或是企業(yè)AI決策技術(shù)的“良藥”
-
激光雷達(dá),馬斯克看不上,卻又無可替代?
-
卷積神經(jīng)網(wǎng)絡(luò)中十大拍案叫絕的操作
-
Docker是啥?容器變革的火花?
-
5大必知的圖算法,附Python代碼實現(xiàn)
-
阿里云彈性計算負(fù)責(zé)人蔣林泉:億級場景驅(qū)動的技術(shù)自研之路
-
40 歲身體死亡,11 年后成“硅谷霍金”,他用一塊屏幕改變 100 萬人!
-
AI大神如何用區(qū)塊鏈解決模型訓(xùn)練痛點, AI+區(qū)塊鏈的正確玩法原來是這樣…… | 人物志
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
