Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #503

Bivariate Analysis

Bivariate analysis examines the relationship between two variables at a time — most importantly, between a feature and the target, since that relationship is what a model is actually trying to learn.

Three Combinations, Three Techniques

Variable TypesTechniqueExample
Numeric × NumericScatter plot, correlation coefficientsize_sqft vs price_lakh
Numeric × CategoricalBoxplot per group, grouped meanprice_lakh distribution per city
Categorical × CategoricalCross-tabulation, stacked bar chartcity vs whether the sale closed

Numeric × Numeric — Scatter Plots

Positive Negative No Relationship

A scatter plot is often the fastest way to spot a relationship — and the only way to catch non-linear ones a correlation coefficient alone would miss.

import matplotlib.pyplot as plt

plt.scatter(df["size_sqft"], df["price_lakh"], alpha=0.6)
plt.xlabel("Size (sq ft)"); plt.ylabel("Price (lakh)")
plt.show()

print(df[["size_sqft", "price_lakh"]].corr())   # numeric confirmation of what the plot shows

Numeric × Categorical — Grouped Boxplots

import seaborn as sns

sns.boxplot(x="city", y="price_lakh", data=df)
plt.show()

print(df.groupby("city")["price_lakh"].mean().sort_values(ascending=False))

A grouped boxplot instantly shows whether price differs meaningfully by city, and whether the spread (not just the average) differs too — a city with a wide price boxplot has more variable pricing than one with a tight, narrow box.

Categorical × Categorical — Cross-Tabulation

import pandas as pd

crosstab = pd.crosstab(df["city"], df["deal_closed"])
print(crosstab)

crosstab_pct = pd.crosstab(df["city"], df["deal_closed"], normalize="index")
print(crosstab_pct)   # percentages within each city — easier to compare across cities of different sizes

Worked Example — Confirming a Relationship Numerically

import pandas as pd

df = pd.DataFrame({
    "size_sqft": [10, 20, 30, 40, 50],
    "price_lakh": [50, 55, 65, 70, 80],
})

print(df.corr())
# size_sqft and price_lakh correlate strongly (~0.99) — confirms the visual scatter trend numerically

See Correlation Analysis for the full hand-worked calculation behind this number.

Why Bivariate Analysis Against the Target Specifically Matters

The most valuable bivariate analysis in an ML project isn't feature-vs-feature — it's feature-vs-target. A feature that shows almost no visual or numeric relationship to the target during EDA is a weak candidate predictor; a feature that clearly separates the target's classes (in a boxplot) or trends with it (in a scatter plot) is a strong one — this is often a faster, more intuitive first pass than waiting for a trained model's feature importance scores.

Common Mistakes

  • Only checking correlation coefficients and skipping scatter plots — correlation only measures linear relationships; a strong curved (non-linear) relationship can have a correlation near zero while still being highly predictive.
  • Comparing raw counts in a cross-tabulation between groups of very different sizes instead of normalizing to percentages first.
  • Drawing conclusions from a bivariate relationship without considering a third variable that might actually be driving both (see Correlation Analysis on correlation vs causation).

Interview Relevance

Q: "A feature has a correlation of 0.02 with the target. Does that mean it's not predictive?" Not necessarily — correlation only captures linear relationships; the feature could still have a strong non-linear relationship with the target that a scatter plot (or a tree-based model) would reveal but a correlation coefficient would completely miss.

Practice Question

You want to check whether "years_of_experience" relates to "salary," and separately whether "department" relates to "left_company" (yes/no). Name the specific bivariate technique you'd use for each pair.

Related ML Notes

Want to go beyond the notes?

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

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →