Menu
CoddyTech

Correlation Heatmaps

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 32 of 64.

A matrix of twenty features is a wall of numbers. A heatmap colors each cell, so the strong pairs jump out. A cinema's eight days:

A 4 by 4 heatmap titled Cinema Correlations with a color bar from -1 to 1. Tickets and popcorn are deep red together, temp_c is deep blue against both, and the staff row is pale

This code draws it:

cinema = pd.DataFrame({
    "tickets": [120, 340, 210, 410, 180, 290, 380, 150],
    "popcorn": [70, 150, 100, 200, 95, 120, 170, 60],
    "temp_c": [24, 12, 15, 9, 20, 19, 11, 22],
    "staff": [6, 5, 8, 7, 5, 6, 6, 8],
})
corr = cinema.corr()
fig, ax = plt.subplots()
im = ax.imshow(corr, cmap="coolwarm", vmin=-1, vmax=1)
fig.colorbar(im)
ax.set_xticks(range(4), corr.columns)
ax.set_yticks(range(4), corr.columns)
ax.set_title("Cinema Correlations")
plt.show()

vmin=-1, vmax=1 pin the colors: red is 1, white 0, blue -1. fig.colorbar(im) adds the key, and set_xticks(positions, labels) names the cells.

Tickets and popcorn glow red together; warm days are blue against both. Staffing barely matters.

challenge icon

Challenge

Medium

A smartwatch logged 30 days in watch.csv: a text weekday column and five number columns.

  1. Compute the correlation matrix of the number columns.
  2. Draw it as a coolwarm heatmap from -1 to 1, with a color bar.
  3. Label the ticks across and down with the column names, title it Watch Data, and show it.

Your program prints nothing. For another owner's three columns, the chart looks like this:

A 3 by 3 heatmap titled Watch Data with steps, sleep_h and stress as tick labels and a color bar from -1 to 1: red diagonal, sleep_h and stress deep blue, steps and sleep_h light red

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt

watch = pd.read_csv("watch.csv")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler