Menu
CoddyTech

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)

The correlation matrix of four laptop columns as a grid of numbers. The diagonal holds 1.00 in gray; the price column is highlighted: ram_gb 0.81, weight_kg -0.95, battery_h 0.70

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_kg

Index 0 is price itself, at 1. Lighter laptops cost more: -0.95 is the strongest link.

challenge icon

Challenge

Medium

An 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.

  1. Print the target's column of the correlation matrix, rounded to 2 decimals.
  2. Print Strongest:, the other column most strongly related to it, and its r, 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
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