Python-110 Two Sample T-Test in Python两个样本的t-检验 2021-06-29

  • Step 1: Data preparation in a excel table:
image.png

  • Step 2: how to conduct and where to quit, and Should perform Welch’s t-test!
#pip install scipy
#pip install openpyxl
import pandas as pd
import numpy as np

# read the data from excel
group1 = np.array(pd.read_excel('C:/Users/Mr.R/Desktop/excels/zm.xlsx', sheet_name='1', usecols='C'))
group2 = np.array(pd.read_excel('C:/Users/Mr.R/Desktop/excels/zm.xlsx', sheet_name='1', usecols='E'))

print(group1, group2)

import scipy.stats as stats

#find variance for each group
# As a rule of thumb, we can assume the populations have equal variances
# if the ratio of the larger sample variance to the smaller sample variance is less than 4:1.

print("\nVariances of the groups are:\nnp.var(group1):", np.var(group1), "\nnp.var(group2):", np.var(group2))

# calculate the ratio of large variance to smaller variance
if np.var(group1) > np.var(group2):
    ratio = np.var(group1)/np.var(group2)
else:
    ratio = np.var(group2)/np.var(group1)

#calculate whether can do two-sample t-test
if ratio < 4.0:
 print("\n Calculated large/small ratio is :", ratio)
 print("\n Yeah! The population variances are equal.\n Let us perform the two sample t-test!") # change the line
else:
    print("\n Calculated large/small ratio is :", ratio)
    print("\nOops! The population variances are not equal,can not proceed the two sample t-test!\n Should perform Welch’s t-test!")

#The ratio of the larger sample variance to the smaller sample variance
# is 17.333333333333332/11.209876543209877 = 1.5462555066079293, which is less than 4.
#This means we can assume that the population variances are equal.

#so when it is right to do the 2-sample t-test then we do!
if ratio < 4.0:
 import scipy.stats as stats

#perform two sample t-test with equal variances
 two_sample_t_test_result = stats.ttest_ind(a=group1, b=group2, equal_var=True)
 print("\n Here is the two sample t-test result:\n", two_sample_t_test_result)
image.png

  • Step3: Interpret the results.:
  • The two hypotheses for this particular two sample t-test are as follows:

  • H0: µ1 = µ2 (the two population means are equal)

  • HA: µ1 ≠µ2 (the two population means are not equal)

  • Because the p-value of our test (0.64428611) is greater than alpha = 0.05, we fail to reject the null hypothesis of the test. We do not have sufficient evidence to say that the mean height of plants between the two populations is different.

你可能感兴趣的:(Python-110 Two Sample T-Test in Python两个样本的t-检验 2021-06-29)