SciPy Statistical Tests
Statistical tests help you decide if a difference in your data is real or just due to chance.
The Idea of a Hypothesis Test
- Start with the null hypothesis: "there is no real difference"
- Collect data and run a test
- The test gives a p-value: how likely data like yours would be if the null hypothesis were true
- If the p-value is small (usually below 0.05), you reject the null hypothesis and call the result significant
| p-value | Usual conclusion |
|---|---|
| Below 0.05 | The difference is probably real |
| 0.05 or more | Not enough evidence of a difference |
A large p-value does not prove that there is no difference. It only means your data does not show one clearly. More data can change the answer.
One-Sample t-Test
A juice factory claims each bottle holds 500 ml. You measure ten bottles. Is the average really 500?
Example
import numpy as np
from scipy import stats
bottles = np.array([498, 495, 501, 497, 494, 499, 496, 500, 493, 497])
result = stats.ttest_1samp(bottles, popmean=500)
print("Mean:", bottles.mean())
print("p-value:", round(result.pvalue, 4))Mean: 497.0 p-value: 0.0051
The p-value is far below 0.05, so the bottles really do hold less than 500 ml on average.
Two-Sample t-Test
Two teachers used different methods. Did their classes score differently?
Example
import numpy as np
from scipy import stats
class_a = np.array([72, 78, 81, 69, 75, 80, 77, 74])
class_b = np.array([80, 85, 79, 88, 84, 82, 86, 81])
result = stats.ttest_ind(class_a, class_b)
print(round(class_a.mean(), 2), round(class_b.mean(), 2))
print("p-value:", round(result.pvalue, 5))75.75 83.12 p-value: 0.00116
Class B scored higher, and the very small p-value says this difference is unlikely to be luck.
Which t-Test?
| Function | Use when |
|---|---|
ttest_1samp(a, value) | Comparing one group to a fixed number |
ttest_ind(a, b) | Comparing two separate groups |
ttest_rel(before, after) | The same people measured twice |
Paired t-Test: Before and After
Six students took a test before and after a revision course:
Example
from scipy import stats
before = [55, 60, 48, 70, 65, 52]
after = [62, 66, 55, 74, 70, 58]
result = stats.ttest_rel(before, after)
print("p-value:", round(result.pvalue, 6))p-value: 6.5e-05
The tiny p-value shows the course helped. 6.5e-05 means 0.000065.
Correlation
Correlation measures how strongly two things move together, from -1 to 1.
- Close to 1: when one goes up, the other goes up
- Close to -1: when one goes up, the other goes down
- Close to 0: no clear straight-line link
Example
from scipy import stats
study_hours = [1, 2, 3, 4, 5, 6, 7, 8]
marks = [52, 55, 61, 64, 70, 72, 79, 85]
r, p = stats.pearsonr(study_hours, marks)
print("Pearson r:", round(r, 3))
print("p-value:", round(p, 7))
rho, p2 = stats.spearmanr(study_hours, marks)
print("Spearman:", rho)Pearson r: 0.994 p-value: 4e-07 Spearman: 1.0
More study hours go with higher marks almost perfectly. Spearman is 1.0 because the marks always rise in the same order as the hours.
Correlation is not causation. Two things moving together does not prove that one causes the other.
Chi-Square Test
The chi-square test checks if two categories are related. Does the choice of course depend on gender? Here is a table of counts:
Example
from scipy.stats import chi2_contingency
# Python Design
table = [[30, 10], # male
[20, 25]] # female
result = chi2_contingency(table)
print("p-value:", round(result.pvalue, 4))p-value: 0.0084
The p-value is below 0.05, so in this sample course choice and gender are related.
Exercise
Exercise
A t-test gives a p-value of 0.30. What is the usual conclusion?
