0Pricing
Learn AI with Python · Lesson

Correlation and Covariance

Pearson/Spearman correlation, covariance matrix, interpreting correlation in ML context.

Correlation and Covariance is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Measuring Relationships

Covariance and correlation quantify how two variables move together. They are the basis for feature selection, portfolio theory, and exploratory analysis.

Covariance

Covariance is positive when variables rise together, negative when one rises as the other falls. Its magnitude depends on units, making raw covariance hard to interpret.

import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 6])
print(np.cov(x, y, ddof=1))   # 2x2 covariance matrix

The Covariance Matrix

np.cov returns a matrix: diagonals are variances, off-diagonals the covariance between pairs. It generalizes to many variables and underlies PCA.

data = np.array([[1, 2, 3], [2, 4, 5], [3, 6, 7]], dtype=float)
print(np.cov(data.T))   # 3x3 covariance matrix of the columns

Correlation: Scaled Covariance

Pearson correlation rescales covariance to the range -1 to 1 by dividing by the product of standard deviations, so it is unit-free and comparable.

print(np.corrcoef(x, y))   # 2x2 correlation matrix, diagonal is 1

Reading Correlation

+1 is a perfect positive linear relationship, -1 perfect negative, 0 no LINEAR relationship. Values near 0 can still hide a strong nonlinear pattern.

Correlation in pandas

df.corr() computes pairwise correlations across all numeric columns at once, the quickest way to scan a dataset for relationships.

import pandas as pd
df = pd.DataFrame({"a": [1,2,3,4,5], "b": [2,4,5,4,6], "c": [9,7,6,5,3]})
print(df.corr())   # Pearson by default

Pearson vs Spearman

Pearson measures LINEAR association on raw values. Spearman works on RANKS, so it captures any monotonic relationship and resists outliers.

from scipy import stats
x = np.array([1, 2, 3, 4, 100])
y = np.array([1, 2, 3, 4, 5])
print(stats.pearsonr(x, y).statistic)    # distorted by outlier
print(stats.spearmanr(x, y).statistic)   # 1.0 (perfectly monotonic)

Choosing the Method

df.corr(method="spearman") switches pandas to rank correlation. Prefer Spearman with outliers or nonlinear-but-monotonic relationships; Pearson for clean linear data.

print(df.corr(method="spearman"))

Correlation is NOT Causation

A strong correlation does not mean one variable causes the other. A hidden confounder may drive both. Always ask what else could explain the link.

Spurious Correlation

With enough variables, some will correlate by pure chance. Ice cream sales and drownings correlate, both driven by summer heat, not each other. Treat unexpected correlations with skepticism.

Visualizing with a Heatmap

A correlation heatmap makes patterns pop. Strongly correlated feature pairs may be redundant and candidates for removal before modeling.

# import seaborn as sns
# sns.heatmap(df.corr(), annot=True, cmap="coolwarm")

Quick Check

Test your correlation knowledge.

Recap

Correlation toolkit:

  • Covariance shows direction but is unit-dependent; np.cov gives the matrix
  • Correlation rescales to -1..1: np.corrcoef, df.corr()
  • Pearson (linear) vs Spearman (rank, robust, monotonic)
  • Correlation is not causation; beware confounders and spurious links

Frequently asked questions

Is the “Correlation and Covariance” lesson free?

Yes — the full text of “Correlation and Covariance” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Correlation and Covariance”?

Pearson/Spearman correlation, covariance matrix, interpreting correlation in ML context. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Correlation and Covariance” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Descriptive Statistics and Distributions
  2. Probability and Bayes Theorem
  3. Hypothesis Testing
  4. Correlation and Covariance
← Back to Learn AI with Python