🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #69

Univariate Analysis

Univariate analysis examines one variable at a time — its distribution shape, spread, and any anomalies — before you consider how it relates to anything else. It's the first real analytical step of EDA, after basic quality checks.

Numeric Features — What to Check

import pandas as pd
import matplotlib.pyplot as plt

df["price_lakh"].describe()      # count, mean, std, min, 25/50/75%, max
df["price_lakh"].hist(bins=15)    # shape of the distribution
plt.show()

df["price_lakh"].plot(kind="box")  # spread, quartiles, outliers at a glance
plt.show()

Reading a numeric feature's histogram, you're asking: is it roughly symmetric or skewed? Is it unimodal or does it have multiple peaks? Are there implausible values at the extremes?

Recognizing Distribution Shapes

Normal (symmetric) Right-skewed (long tail →) Left-skewed (← long tail) Bimodal (two peaks)

The shape alone tells you a lot: right-skew usually calls for median imputation/log transform; bimodal often signals two hidden subgroups.

Categorical Features — What to Check

df["city"].value_counts()                    # raw counts per category
df["city"].value_counts(normalize=True)        # proportions instead of counts
df["city"].value_counts().plot(kind="bar")      # visual comparison
plt.show()

df["city"].nunique()                            # cardinality — decides encoding strategy

Worked Example

import pandas as pd

prices = pd.Series([45, 68, 60, 72, 95, 78, 110, 130, 105, 140, 500])   # includes one outlier

print(prices.describe())
# mean is pulled up noticeably by the 500 — compare mean vs the 50% (median) row
print(prices.skew())   # positive value confirms a right-skewed distribution

Expected output: the mean will sit visibly higher than the median in .describe()'s output, and .skew() returns a clearly positive number — both independently confirming the right-skew a histogram would also show visually. See Skewness for the formal definition.

Turning Univariate Findings Into Action

What You SeeWhat It Suggests
Strongly skewed numeric featureMedian imputation over mean; consider a log transform; use robust scaling
Bimodal numeric featureInvestigate whether a hidden categorical variable explains the two peaks
High-cardinality categorical featureAvoid one-hot encoding; consider frequency or target encoding
Near-constant feature (almost no variance)Likely low predictive value; candidate for removal

Practical Use Cases

  • Deciding the right imputation strategy per feature based on its distribution shape
  • Spotting data entry errors (implausible values sitting far outside a feature's normal range)
  • Identifying near-constant or extremely high-cardinality features early, before wasting effort engineering them

Common Mistakes

  • Only looking at summary statistics (.describe()) without ever plotting a histogram — two very differently shaped distributions can share nearly identical mean/std.
  • Judging skew from .describe()'s numbers alone instead of also checking .skew() or a visual — mean vs median gives a hint, but isn't as precise.

Interview Relevance

Q: "How would you decide whether to log-transform a numeric feature?" Check its distribution — strong right-skew (long tail toward high values, common in prices/income/counts) is the classic signal that a log transform will make the feature closer to normally distributed, which helps linear models and reduces the influence of extreme values.

Practice Question

A "session_duration" feature has mean=340 seconds but median=180 seconds. What does this gap suggest about the distribution's shape, and what would you check next?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Univariate Analysis – FAQs

Quick answers about learning Univariate Analysis in Machine Learning.

This free note from CodingNow 2.0 explains Univariate Analysis in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Univariate Analysis, is 100% free with no signup required.
With focused practice, most students grasp Univariate Analysis in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now