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:
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
MediumA smartwatch logged 30 days in watch.csv: a text weekday column and five number columns.
- Compute the correlation matrix of the number columns.
- Draw it as a
coolwarmheatmap from -1 to 1, with a color bar. - 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:
Try it yourself
import pandas as pd
import matplotlib.pyplot as plt
watch = pd.read_csv("watch.csv")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler