ML Notes
Essential statistics concepts for ML including descriptive statistics, probability distributions, hypothesis testing, and correlation with Python examples.
Statistics provides the mathematical foundation for understanding data and making informed decisions about model performance. Every ML practitioner needs these concepts to properly analyze datasets, evaluate models, and communicate results.
Descriptive Statistics
Probability Distributions
import numpy as np
from scipy import stats
# Normal (Gaussian) Distribution - most common in ML
normal = stats.norm(loc=0, scale=1)
print("Normal Distribution (μ=0, σ=1):")
print(f" P(X < 0): {normal.cdf(0):.4f}")
print(f" P(-1 < X < 1): {normal.cdf(1) - normal.cdf(-1):.4f}") # ~68%
print(f" P(-2 < X < 2): {normal.cdf(2) - normal.cdf(-2):.4f}") # ~95%
# Bernoulli - binary outcomes (classification)
p_spam = 0.3
print(f"\nBernoulli(p=0.3): P(spam) = {p_spam}, P(not spam) = {1-p_spam}")
# Binomial - number of successes in n trials
binom = stats.binom(n=100, p=0.3)
print(f"\nBinomial(n=100, p=0.3):")
print(f" Expected successes: {binom.mean():.1f}")
print(f" P(exactly 30): {binom.pmf(30):.4f}")
# Poisson - rare events
poisson = stats.poisson(mu=5)
print(f"\nPoisson(λ=5): P(X=3) = {poisson.pmf(3):.4f}")Correlation and Covariance
import numpy as np
import pandas as pd
# Generate correlated features
np.random.seed(42)
n = 200
age = np.random.normal(35, 10, n)
income = age * 1500 + np.random.normal(0, 5000, n) # Correlated
height = np.random.normal(170, 10, n) # Not correlated
df = pd.DataFrame({'age': age, 'income': income, 'height': height})
# Correlation matrix
print("Correlation Matrix:")
print(df.corr().round(3))
print("\nInterpretation:")
print(" age-income: Strong positive (older → higher income)")
print(" age-height: Near zero (no relationship)")
# Pearson vs Spearman correlation
from scipy.stats import pearsonr, spearmanr
r_pearson, p_val = pearsonr(age, income)
r_spearman, _ = spearmanr(age, income)
print(f"\nPearson r: {r_pearson:.4f} (linear relationships)")
print(f"Spearman r: {r_spearman:.4f} (monotonic relationships)")Hypothesis Testing
Central Limit Theorem
Interview Questions
- What is the difference between population and sample statistics?
Population statistics describe the entire group (μ, σ). Sample statistics (x̄, s) estimate population parameters from a subset. We use samples because measuring entire populations is usually impractical.
- When would you use median instead of mean?
When data has outliers or is heavily skewed. Median is robust to extreme values (income data, house prices). Mean is sensitive to outliers.
- What does a p-value actually mean?
The probability of observing results at least as extreme as the data, assuming the null hypothesis is true. It does NOT mean the probability that the hypothesis is true.
- Why is the Central Limit Theorem important for ML?
It justifies using normal distribution assumptions for confidence intervals and hypothesis tests even when the underlying data isn't normal, as long as sample sizes are sufficient.
- What is the difference between correlation and causation?
Correlation measures statistical association between variables. Causation means one variable directly influences another. Correlation does not imply causation — there may be confounders.
Practical Applications in Machine Learning
Understanding statistics isn't just academic — these concepts directly impact your ML workflow:
Feature Selection: Correlation analysis helps identify redundant features. If two features have correlation > 0.95, one is likely redundant. Statistical tests (chi-square for categorical, ANOVA for numerical vs categorical) quantify feature-target relationships.
Model Evaluation: Confidence intervals around accuracy scores tell you whether differences between models are statistically significant or just noise. A model with 85% ± 3% accuracy isn't meaningfully better than one with 84% ± 3%.
A/B Testing: When deploying ML models in production, hypothesis testing determines whether the new model genuinely outperforms the baseline. Without proper statistical testing, you might deploy a model that only appeared better due to random variation.
Outlier Detection: Z-scores and IQR methods use distributional properties to identify anomalous data points. Understanding when data is normally distributed (and when it isn't) determines which detection method is appropriate.
Common Statistical Misconceptions
Misconception 1: Large p-value means no effect exists. A large p-value means we failed to detect an effect with our sample size. The effect might exist but be too small to detect — this is a power issue, not proof of absence.
Misconception 2: Correlation of 0.7 is "strong." Context matters enormously. In physics, 0.7 might be weak. In social science, 0.7 is excellent. Always interpret correlation relative to your domain's standards.
Misconception 3: Normal distribution is required for ML. Many ML algorithms (decision trees, neural networks) make no distributional assumptions about features. Normality matters most for linear regression residuals, parametric statistical tests, and algorithms using Mahalanobis distance.
Misconception 4: More data always helps. While more data generally improves estimates, biased data doesn't become less biased with more samples. If your sampling method is flawed, additional data amplifies the bias rather than correcting it.
Quick Reference: When to Use Which Test
| Scenario | Test |
|---|---|
| Compare two group means | t-test (independent or paired) |
| Compare 3+ group means | ANOVA |
| Test categorical associations | Chi-square |
| Correlation significance | Pearson's r test |
| Non-normal data comparison | Mann-Whitney U / Wilcoxon |
| Normality check | Shapiro-Wilk test |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Statistics Basics for Machine Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, prerequisites, statistics, basics, statistics basics for machine learning
Related Machine Learning Topics