bhylu.com

Advertisements

Data Science Correlation Matrix

Read each matrix cell as a separate relationship with a known sample policy.

How it works

A correlation matrix compares multiple variables pairwise. Its diagonal is one for nonconstant variables, and the matrix is symmetric. Pairwise missing-value deletion can use a different number of observations for each cell, which complicates comparisons. Correlated predictors may also contain overlapping information, so a colourful matrix is not a feature-selection decision by itself.

Run the code

Try in playground opens the correct numpy editor and preloads the complete source. Choose Run to execute; loading does not execute it. The isolated browser runtime does not read personal PC files.

Local review: Python 3.12.14, NumPy 2.5.4, Pandas 3.0.6 and SciPy 1.18.0. Browser review: Pyodide 0.27.7, Python 3.12.7, NumPy 2.0.2, Pandas 2.2.3 and SciPy 1.14.1 in their respective editors. Save the download as correlation-matrix.py and run python correlation-matrix.py in your separate environment. Apache and MySQL are not needed. Environment setup.

Download the complete source

Example

correlation-matrix.py

import numpy as np
rows = np.array([[1., 2., 8.], [2., 4., 6.], [3., 6., 4.], [4., 8., 2.]])
matrix = np.corrcoef(rows, rowvar=False)
assert matrix.shape == (3, 3)
assert np.allclose(matrix, [[1,1,-1],[1,1,-1],[-1,-1,1]], atol=1e-12)
print("Three-variable matrix: first two move together; third moves oppositely")

Expected result

Three-variable matrix: first two move together; third moves oppositely

Rows are observations and columns are variables, so rowvar=False is important. The exact relationships make the expected signs clear. In real data, label each axis and report missingness and sample counts. Strong correlations among predictors should prompt rank and stability checks before interpreting regression coefficients independently.

Technical references: NumPy, Pandas and SciPy. For evaluation design see the scikit-learn guide to leakage; these examples do not require scikit-learn.