Python Statistics: How to understand the one-sample t-test?

The one-sample t-test refers to whether there is a difference in the mean value of the sample.

For example, the standard weight of a pack of potato chips is 50g, but each pack is not necessarily 50g, then we can randomly sample the potato chips to test whether it is different from 50g.


1 Make a hypothesis:

Null hypothesis: The average weight of potato chips is 50g;

Alternative Hypothesis: The average weight of potato chips is not 50g.

If it is a one-sided test to test whether the potato chips are less than 50g, it is a two-sided test to test whether the potato chips are different from 50g.


2 Determine the level of significance:

The accuracy requirements are not too high, and the significance level is generally 0.05. After we calculate the p value, we need to compare it with the significance level. If the returned p value is less than the significance level, the null hypothesis is rejected, that is, the sample data is considered to be consistent with the overall Means are significantly different.


3 Import sample data:

junk_food = [58.530, 52.353, 74.446, 52.983, 55.877, 67.660, 47.726, 50.267, 56.501, 52.361, 45.458, 53.361, 52.129, 59.828, 41.682, 49.399, 64.211, 69.859, 42.911, 60.159]

junk_food


4 Perform a t-test on the sample data:

# t检验

stats.ttest_1samp(junk_food, 50)

Detailed code: 

The ttest_1samp function in the scipy library can be used to perform sample t tests;

50 represents the assumed population mean;

The returned result statistic represents the t value, and pvalue represents the p value;

The result is that the returned p-value of 0.0127 is less than the significance level of 0.05, so we reject the null hypothesis that the sample data is significantly different from the overall mean. That is to say, there is a significant difference between the weight of potato chips and 50g.

Guess you like

Origin blog.csdn.net/Sukey666666/article/details/130257479
Recommended