Pythonでのt検定の実施
メニューを表示するにはスワイプしてください
Pythonでt検定を実施するには、対立仮説を指定し、分散がほぼ等しい(等分散)かどうかを示すだけです。
ttest_ind()内のscipy.stats関数が残りの処理を行います。以下はその構文です:
st.ttest_ind(a, b, equal_var=True, alternative='two-sided')
パラメータ:
a— 第1サンプル;b— 第2サンプル;equal_var— 分散がほぼ等しい場合はTrue、等しくない場合はFalse;alternative— 対立仮説の種類:'two-sided'— 平均値が等しくないことを示す;'less'— 第1の平均値が第2より小さいことを示す;'greater'— 第1の平均値が第2より大きいことを示す。
戻り値:
statistic— t統計量の値;pvalue— p値。
注目すべきはp-valueです。p-valueがα(通常は0.05)より小さい場合、t統計量は棄却域に入り、対立仮説が採択されます。p-valueがαより大きい場合は、帰無仮説が採択され、平均値が等しいと判断されます。
以下は、t検定をheightsデータセットに適用する例です:
123456789101112131415import pandas as pd import scipy.stats as st # Load the data male = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a849660e-ddfa-4033-80a6-94a1b7772e23/Testing2.0/male.csv').squeeze() female = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a849660e-ddfa-4033-80a6-94a1b7772e23/Testing2.0/female.csv').squeeze() # Apply t-test t_stat, pvalue = st.ttest_ind(male, female, equal_var=True, alternative="greater") if pvalue > 0.05: # Check if we should support or not the null hypothesis if pvalue > 0.05: print("We support the null hypothesis, the mean values are equal") else: print("We reject the null hypothesis, males are taller")
すべて明確でしたか?
フィードバックありがとうございます!
セクション 6. 章 6
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 6. 章 6