Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Conduct a One Sample T-Test in Python?
A One Sample T-Test is a statistical hypothesis test used to determine whether a population mean is significantly different from a hypothesized value. Python provides the necessary tools through the SciPy library to conduct this test efficiently.
Understanding the One Sample T-Test
Before performing the test, we need to establish our hypotheses ?
- Null Hypothesis (H?): The population mean equals the hypothesized value
- Alternative Hypothesis (H?): The population mean does not equal the hypothesized value
Step-by-Step Implementation
Step 1: Import Required Libraries
We need NumPy for data handling and SciPy for statistical functions ?
import numpy as np from scipy.stats import ttest_1samp
Step 2: Prepare Sample Data
Let's create sample data representing apple weights in grams ?
# Sample data - weights of apples in grams
apple_weights = np.array([98, 102, 95, 105, 99, 101, 97, 103, 100, 98])
# Hypothesized population mean
hypothesized_mean = 100
print("Sample data:", apple_weights)
print("Sample mean:", np.mean(apple_weights))
print("Hypothesized mean:", hypothesized_mean)
Sample data: [ 98 102 95 105 99 101 97 103 100 98] Sample mean: 99.8 Hypothesized mean: 100
Step 3: Perform the One Sample T-Test
The ttest_1samp() function returns the t-statistic and p-value ?
import numpy as np
from scipy.stats import ttest_1samp
# Sample data and hypothesized mean
apple_weights = np.array([98, 102, 95, 105, 99, 101, 97, 103, 100, 98])
hypothesized_mean = 100
# Perform the One Sample T-Test
t_statistic, p_value = ttest_1samp(apple_weights, hypothesized_mean)
print(f"T-statistic: {t_statistic:.4f}")
print(f"P-value: {p_value:.4f}")
T-statistic: -0.2236 P-value: 0.8285
Step 4: Interpret the Results
Compare the p-value with the significance level (typically ? = 0.05) ?
import numpy as np
from scipy.stats import ttest_1samp
# Sample data and test
apple_weights = np.array([98, 102, 95, 105, 99, 101, 97, 103, 100, 98])
hypothesized_mean = 100
t_statistic, p_value = ttest_1samp(apple_weights, hypothesized_mean)
# Set significance level
alpha = 0.05
print(f"T-statistic: {t_statistic:.4f}")
print(f"P-value: {p_value:.4f}")
print(f"Significance level: {alpha}")
if p_value < alpha:
print("Result: Reject the null hypothesis")
print("The population mean is significantly different from", hypothesized_mean)
else:
print("Result: Fail to reject the null hypothesis")
print("No significant difference from the hypothesized mean of", hypothesized_mean)
T-statistic: -0.2236 P-value: 0.8285 Significance level: 0.05 Result: Fail to reject the null hypothesis No significant difference from the hypothesized mean of 100
Complete Example with Different Data
Let's test whether student exam scores differ significantly from an expected mean of 75 ?
import numpy as np
from scipy.stats import ttest_1samp
# Student exam scores
scores = np.array([82, 78, 85, 79, 88, 91, 77, 83, 86, 80])
expected_mean = 75
# Perform the test
t_stat, p_val = ttest_1samp(scores, expected_mean)
print(f"Sample scores: {scores}")
print(f"Sample mean: {np.mean(scores):.2f}")
print(f"Expected mean: {expected_mean}")
print(f"T-statistic: {t_stat:.4f}")
print(f"P-value: {p_val:.4f}")
# Interpretation
if p_val < 0.05:
print("Conclusion: Student performance is significantly different from expected")
else:
print("Conclusion: No significant difference from expected performance")
Sample scores: [82 78 85 79 88 91 77 83 86 80] Sample mean: 82.90 Expected mean: 75 T-statistic: 6.8416 P-value: 0.0001 Conclusion: Student performance is significantly different from expected
Key Assumptions
The One Sample T-Test requires these assumptions ?
- Normal distribution: Data should be approximately normally distributed
- Independence: Observations must be independent
- Random sampling: Data should be randomly selected from the population
Conclusion
The One Sample T-Test in Python using SciPy's ttest_1samp() function is straightforward to implement. Compare the p-value to your significance level (typically 0.05) to determine if the population mean differs significantly from your hypothesized value.
