← Back to Tutorials Chapter 9: Visualization

Data Visualization for Machine Learning

Visualization is a critical tool in ML for understanding data, diagnosing model issues, and communicating results. This chapter covers matplotlib and seaborn for creating informative plots.

matplotlib

matplotlib is the foundational plotting library in Python. It provides fine-grained control over every aspect of a plot. Use it for line plots, scatter plots, bar charts, histograms, and custom visualizations.

import matplotlib.pyplot as plt
import numpy as np

# Simple line plot
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(8, 4))
plt.plot(x, y, label='sin(x)', color='#3b82f6')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('Sine Wave')
plt.legend()
plt.grid(alpha=0.3)
plt.show()

seaborn

seaborn provides a high-level interface for statistical graphics. It works well with pandas DataFrames and has built-in themes and color palettes. Use it for complex plots with minimal code.

import seaborn as sns
import pandas as pd

# Load dataset
df = sns.load_dataset('iris')

# Pair plot
sns.pairplot(df, hue='species', diag_kind='kde')
plt.show()

Histograms and Density Plots

Histograms show the distribution of a single variable. Density plots (KDE) provide a smooth estimate of the distribution. Use them to understand data ranges, central tendency, and skewness.

# Histogram and density plot
plt.figure(figsize=(10, 4))

plt.subplot(1, 2, 1)
plt.hist(df['sepal_length'], bins=20, edgecolor='black', alpha=0.7)
plt.title('Histogram')

plt.subplot(1, 2, 2)
sns.kdeplot(df['sepal_length'], fill=True)
plt.title('Density Plot')

plt.tight_layout()
plt.show()

Box and Whisker Plots

Box plots display the five-number summary (minimum, Q1, median, Q3, maximum) and highlight outliers. They are useful for comparing distributions across categories.

plt.figure(figsize=(8, 4))
sns.boxplot(x='species', y='petal_length', data=df)
plt.title('Petal Length by Species')
plt.show()

Correlation Matrix Heatmap

A heatmap of the correlation matrix helps identify relationships between numerical features. High correlations indicate redundancy and potential multicollinearity.

Data visualization examples
# Correlation heatmap
corr = df.corr(numeric_only=True)

plt.figure(figsize=(6, 5))
sns.heatmap(corr, annot=True, cmap='coolwarm', fmt='.2f')
plt.title('Correlation Matrix')
plt.show()

Scatter Matrix and Pair Plots

Pair plots show scatter plots for every pair of features and distributions on the diagonal. They are excellent for spotting patterns, clusters, and outliers in multi-dimensional data.

Best Practices for Visualizing Data before ML

Always visualize your data before training. Check for: missing values, outliers, class imbalance, skewed distributions, and feature correlations. Use visualizations to inform preprocessing decisions.

Note: Don't just default to the default plot settings. Customize colors, labels, titles, and legends to make your plots clear and publication-ready.

Summary

Visualization is an essential skill for ML practitioners. Master histograms, box plots, heatmaps, and pair plots to explore data effectively and communicate findings.

Exercise 9.1: Load the Titanic dataset from seaborn. Create the following plots: histogram of ages by survival status, box plot of fare by class, correlation heatmap of numerical features, and a pair plot colored by survival. Write a short analysis of what each plot reveals.