Python-110 One Sample T-Test 单样本t-检验 2021-06-29

1. Introduction:
  • A one sample t-test is used to test whether or not the mean of a population is equal to a specific value.
  • Example: One Sample t-Test in Python
    Suppose a botanist wants to know if the mean height of a certain species of plant is equal to 15 inches. She collects a random sample of 12 plants and records each of their heights in inches.
  • Use the following steps to conduct a one sample t-test to determine if the mean height for this species of plant is actually equal to 15 inches.

2. Hypothesis and Interpretation of the results:
  • The two hypotheses for this particular one sample t-test are as follows:
  • H0: µ = 15 (the mean height for this species of plant is 15 inches)
  • HA: µ ≠15 (the mean height is not 15 inches)

If the p-value of our test 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 for this particular species of plant is different from 15 inches.


3. How to do that?
  • data:
image.png
  • read the data column c, and conduct the test:
#pip install scipy
#pip install openpyxl
import pandas as pd

df_sheet_index = pd.read_excel('C:/Users/Mr.R/Desktop/excels/zm.xlsx', sheet_name='1', usecols='C')
print(df_sheet_index)

import scipy.stats as stats
data =df_sheet_index
# or data = [12, 13, 21, 14, 16, 11, 12, 18, 19]
hypothised_mean = 15

#perform one sample t-test
#test whether the mean is equal to the hypothised_mean
test_result = stats.ttest_1samp(a=data, popmean=hypothised_mean)

#print the used data and results 
print('test-data is :', data)
print("The final t-test result is :", test_result)
  • get the results :

statistic=array([0.09386465]), pvalue=array([0.92752497])
The t-test statistic is0.09386465 and the corresponding two-sided p-value is0.92752497.


image.png
  • Since the p-value of our test is 0.92752497, 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 for this particular species of plant is different from 15 inches.

Reference: https://www.statology.org/one-sample-t-test-python/

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