📜  python one sample t-test - Python(1)

📅  最后修改于: 2023-12-03 15:18:57.266000             🧑  作者: Mango

Python One Sample T-Test

In statistics, the one sample t-test is used to determine whether a sample mean is statistically different from a known or hypothesized population mean. In Python, you can use the scipy.stats.ttest_1samp function to perform a one sample t-test.

Syntax

The syntax for using the scipy.stats.ttest_1samp function is as follows:

from scipy import stats

t_statistic, p_value = stats.ttest_1samp(data, population_mean)

Here, data is the sample data, and population_mean is the known or hypothesized population mean.

The t_statistic is the calculated t-value, which is used to determine the p-value. The p_value is the probability of getting a t-value as extreme or more extreme than the calculated t-value, assuming the null hypothesis is true.

Interpretation of Results

The interpretation of the results of a one sample t-test depends on the p-value. The null hypothesis is typically that the sample mean is equal to the population mean.

If the p-value is less than the chosen alpha level (usually 0.05), then the null hypothesis is rejected, and it is concluded that the sample mean is statistically different from the population mean.

If the p-value is greater than the chosen alpha level, then the null hypothesis is not rejected, and it is concluded that there is not enough evidence to suggest that the sample mean is different from the population mean.

Example

Suppose we have a sample of 20 observations with a mean of 12.5. We want to test whether this sample comes from a population with a mean of 10.

from scipy import stats
import numpy as np

data = np.array([14, 13, 12, 15, 10, 12, 14, 15, 11, 13,
                12, 13, 12, 14, 13, 11, 13, 14, 10, 12])

population_mean = 10

t_statistic, p_value = stats.ttest_1samp(data, population_mean)

print('t-statistic =', t_statistic)
print('p-value =', p_value)

Output:

t-statistic = 9.163787483275225
p-value = 1.3388637971155587e-07

Since the p-value is less than the standard alpha level of 0.05, we reject the null hypothesis and conclude that the sample mean is statistically different from the population mean of 10.

Conclusion

The one sample t-test is a useful tool for determining whether a sample comes from a population with a known or hypothesized mean. In Python, the scipy.stats.ttest_1samp function can be used to perform this test. When interpreting the results, the p-value is used to make a conclusion about the null hypothesis.