The t-Test
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 45 of 64.
Before computers, statistics had a shortcut: the t-test. A formula turns the means, spreads and group sizes into a p-value at once:
from scipy import stats
result = stats.ttest_ind(sans, serif)
print(np.round(result.pvalue, 3))
print(np.round(result.statistic, 2))For the font study this prints 0.031 and -2.61. The 1000 shuffles gave 0.039: close, as usual.
The t value is the gap in standard errors; its sign only says which group came first. The formula assumes bell-shaped data, so with small skewed groups, trust the shuffles.
It also checks whether one model really beats another.
Challenge
MediumA paint maker timed three finishes drying. The starter puts each finish's minutes in times, lists three pairs, defines permutation_p(a, b) and reads level.
- Print the t-test's p-value for matte vs satin beside the shuffles' one.
- Test every pair, first finish first, and print the one with the smallest p-value.
- Count the pairs below
level.
Round p-values to 3 decimals. Another maker, level 0.05:
matte vs satin: t-test 0.012, simulation 0.023
Clearest difference: satin vs gloss (p = 0.0)
Pairs that differ: 3 of 3Try it yourself
import numpy as np
import pandas as pd
from scipy import stats
# The permutation test of the last two lessons: returns its p-value for a against b
def permutation_p(a, b, shuffles=1000, seed=0):
rng = np.random.default_rng(seed)
pooled = np.concatenate([a, b])
diffs = []
for i in range(shuffles):
shuffled = rng.permutation(pooled)
diffs.append(shuffled[len(a):].mean() - shuffled[:len(a)].mean())
real = b.mean() - a.mean()
return (np.abs(np.array(diffs)) >= np.abs(real)).mean()
paint = pd.read_csv("paint.csv")
times = {
"matte": paint[paint["finish"] == "matte"]["minutes"],
"satin": paint[paint["finish"] == "satin"]["minutes"],
"gloss": paint[paint["finish"] == "gloss"]["minutes"],
}
pairs = [("matte", "satin"), ("matte", "gloss"), ("satin", "gloss")]
level = float(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life8Comparing Groups
Box Plots by GroupDifference in MeansCould It Be Chance?P-ValuesThe t-TestRecap: A/B Test3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler