Univariate Analysis
Get to know each feature alone
One variable at a time: histograms, KDEs, value counts and summary stats characterize each feature's distribution, scale and sparsity before any relationship is examined.
What you will learn
- Use histograms/KDE and value_counts to read shape
- Detect skew, modes and sparsity
- Decide per-column encodings from distributions
What is this?
Univariate analysis examines one column at a time. For numerics: histogram/KDE shapes, skew, modes, ranges. For categoricals: value_counts cardinality and balance. Outputs drive real decisions — which columns to log-transform, which to treat as categorical, which are sparse enough to re-bucket, and which predictors are constant (and useless).
Why do you need it?
- Distribution knowledge dictates preprocessing: log for right-skew, one-hot for low-cardinality categories.
- A column with one dominant value added to a distance model just adds noise.
- Encoding, imputation and scaling choices are univariate facts.
- Half of 'model didn't work' is a feature that was never profiled.
Mental model
Meeting each team member before running a project
Univariate analysis of each column
You want to know each person's proficiency (distribution), talkativeness (mode), and outlier behavior (tails) before organizing them into teams (relationships). One-on-ones precede team dynamics.
How it works
- Numerics: df['x'].hist(bins=50) + KDE reveal shape; .skew() quantifies it.
- Categoricals: df['x'].value_counts(normalize=True) gives balance and cardinality.
- Sparsity: fraction of zeros/short codes via value_counts — 'sparse categories' get collapsed.
- Modes: multiple peaks suggest mixed populations (two segments in one column).
- Constant columns: nunique() == 1 → no predictive information.
Univariate decision flow.
Examples
Profile one numeric + one categorical
import pandas as pd, numpy as nprng = np.random.default_rng(1)x = np.exp(rng.normal(2, .8, 5000))y = rng.choice(['a','b','c'], 5000, p=[.85,.1,.05])print(pd.Series(x).describe(percentiles=[.5,.9,.99]).round(2))print(pd.Series(y).value_counts(normalize=True))The numeric shows right tail (p99 far from p50) — a log-transform candidate. The categorical is 85/10/5 — a clearly imbalanced category that may need collapsing or sampling attention.
In production
- A fraud feature engineer logs revenue columns after seeing skew in univariate profiles.
- A churn model drops a 'user_id' column based on nunique()==shape — a constant/key leak.
- A recommender bins a long-tail categorical by frequency after value_counts shows a 1%-covered tier set.
Best practices
- Profile every candidate feature; document what you saw.
- Check zeros separately from 'real' small values — zeros often encode something.
- For categoricals, record cardinality BEFORE deciding encodings.
Common mistakes
- Fitting scaling on raw skewed columns — the mean/std then lie.
- One-hot encoding a 2000-level categorical without a frequency cut.
- Missing a bimodal column that splits two populations.
Debugging tips
- value_counts(normalize=True).head() shows whether a tiny category is noise or a real group.
- If skew() ≈ 0 but the histogram has two peaks → bimodal check, not 'symmetric'.
- A log transform of a column with zeros fails — inspect zeros first.
Interview checkpoint
Hands-on lab
- Profile numerics with describe + KDE
- Profile categoricals with value_counts
- Make per-column preprocessing decisions
Profile Every Column
25 min · beginner
Quick quiz
A histogram with two clear peaks indicates:
A column with a single unique value can still be a strong predictor.