Correlation Matrix
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 31 of 64.
Choosing features means comparing every column with the target at once. df.corr(numeric_only=True) builds the correlation matrix, skipping text columns:
laptops = pd.DataFrame({
"model": ["Arc", "Bolt", "Cove", "Dune", "Echo", "Flux", "Gale"],
"ram_gb": [8, 16, 8, 32, 16, 16, 32],
"weight_kg": [2.3, 1.4, 2.0, 1.1, 1.8, 1.2, 1.6],
"battery_h": [7, 9, 10, 13, 6, 11, 8],
"price": [650, 1100, 700, 1600, 900, 1350, 1200],
})
corr = laptops.corr(numeric_only=True)Each cell is the r of its row and column. To find the strongest feature, rank the price column by size, ignoring signs:
ranked = np.abs(corr["price"]).sort_values(ascending=False)
print(ranked.index[1])This prints:
weight_kgIndex 0 is price itself, at 1. Lighter laptops cost more: -0.95 is the strongest link.
Challenge
MediumAn energy company surveyed 24 homes in winter. homes.csv has a home code and the number columns area_m2, insulation_cm, residents, outside_temp_c and heating_kwh. The program reads a target column.
- Print the target's column of the correlation matrix, rounded to 2 decimals.
- Print
Strongest:, the other column most strongly related to it, and itsr, as below.
For another survey and the target heating_kwh:
windows 0.72
floors 0.68
age_years 0.23
heating_kwh 1.00
Name: heating_kwh, dtype: float64
Strongest: windows (r = 0.72)Try it yourself
import numpy as np
import pandas as pd
homes = pd.read_csv("homes.csv")
target = input()
# 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