P-Values
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 44 of 64.
The p-value turns the shuffles into one number: the share of fake gaps at least as large as the real one, either way.
In code, for the font study:
real = sans.mean() - serif.mean()
rng = np.random.default_rng(4)
diffs = shuffled_diffs(serif, sans, 1000, rng) # the last lesson's loop
p_value = (np.abs(diffs) >= np.abs(real)).mean()
print(p_value)This prints 0.039. np.abs counts a gap of -7 as large as 7: the question is whether the fonts differ at all.
Below the significance level, often 0.05, chance is a poor explanation, for a font or a model's gain. Not proof: 4 shuffles in 100 still did it.
Challenge
MediumA farm waters strawberries mornings or evenings. The starter splits strawberries.csv into morning and evening, lists columns, reads level, defines shuffled_diffs.
- For each column, compute the real gap (evening minus morning mean), shuffled gaps (morning first) and p-value.
- Print a line per column as below (gap 2 decimals, p 3):
differswhen p is belowlevel. - Count those that differ.
Another farm, level 0.05:
berries: difference -5.4, p = 0.009, differs
weight_g: difference 0.2, p = 1.0, could be chance
sweetness_brix: difference 0.46, p = 0.022, differs
leaf_spots: difference 0.6, p = 0.397, could be chance
Columns that differ: 2 of 4Try it yourself
import numpy as np
import pandas as pd
# Returns the shuffled differences of a permutation test (lesson 43's loop): fake b mean minus fake a mean
def shuffled_diffs(a, b, shuffles, rng):
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())
return np.array(diffs)
beds = pd.read_csv("strawberries.csv")
morning = beds[beds["watering"] == "morning"]
evening = beds[beds["watering"] == "evening"]
columns = ["berries", "weight_g", "sweetness_brix", "leaf_spots"]
level = float(input())
# Don't change the lines below
rng = np.random.default_rng(21)
shuffles = 1000
# 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