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 #205

Seaborn for Machine Learning

Seaborn is built on top of Matplotlib and specializes in statistical visualization — it's the library you'll use most during EDA, because it makes distribution, correlation and category-comparison plots a single line of code.

The EDA Workhorses

import seaborn as sns
import matplotlib.pyplot as plt

# 1. Correlation heatmap — spot multicollinearity and predictive features fast
corr = df.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap="coolwarm", fmt=".2f")
plt.show()

# 2. Boxplot — compare a numeric feature across categories, spot outliers
sns.boxplot(x="department", y="salary", data=df)
plt.show()

# 3. Pairplot — every numeric feature plotted against every other, at a glance
sns.pairplot(df[["age", "income", "credit_score", "churned"]], hue="churned")
plt.show()

# 4. Distribution plot with a smoothed curve
sns.histplot(df["income"], kde=True)
plt.show()

Reading a Correlation Heatmap

Values range from -1 to +1: near +1 means two features move together strongly, near -1 means they move in opposite directions, and near 0 means little linear relationship. Two features that are highly correlated with each other (not with the target) is a signal for multicollinearity — worth addressing in Feature Selection. A feature highly correlated with the target is a promising predictor.

Why a Pairplot with hue Is So Useful

Coloring points by the target class (hue="churned") instantly shows whether a feature — or a combination of two features — visually separates the classes. If two classes overlap completely in every pairwise plot, a linear model will likely struggle, hinting you may need feature engineering or a non-linear model.

Practical Use Cases

  • Spotting multicollinearity between features before modeling
  • Comparing a numeric feature's distribution across categories or classes
  • Confusion matrix visualization after evaluating a classifier

Common Mistakes

  • Interpreting correlation as causation — a strong correlation in a heatmap never proves one feature causes another.
  • Running df.corr() only on numeric columns without first checking which columns are actually numeric vs. mistakenly-numeric categorical codes.

Interview Relevance

Q: "How would you check for multicollinearity before training a linear model?" Plot a correlation heatmap between features (not the target) — pairs with very high correlation (e.g. >0.9) are candidates to drop or combine, since they can destabilize a linear model's coefficients.

Practice Question

A correlation heatmap shows square_feet and num_rooms have a correlation of 0.93 with each other. What issue could this cause for a linear regression model, and what would you do about it?

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 →