SciPy Tutorial menu

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

  1. Start with the null hypothesis: "there is no real difference"
  2. Collect data and run a test
  3. The test gives a p-value: how likely data like yours would be if the null hypothesis were true
  4. If the p-value is small (usually below 0.05), you reject the null hypothesis and call the result significant
p-valueUsual conclusion
Below 0.05The difference is probably real
0.05 or moreNot 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

python
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))
Output
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

python
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))
Output
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?

FunctionUse 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

python
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))
Output
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

python
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)
Output
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

python
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))
Output
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?

Learn SciPy with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp