ZestCalc

Covariance Matrix Calculator

Paste a table of data and see how every pair of features moves together: the means, the centered data, the covariance matrix kept exact, and the correlation matrix, with a scatter you can drag.

Data

One sample per row and one feature per column, up to 100 samples and 6 features. Type, paste from a spreadsheet or CSV, or start from an example.

Features2
8 samples

Covariance

Updates as you type.

8 samples of 2 features.

Dividing by n − 1 = 7 makes up for measuring spread around the sample's own mean. Use it when the rows are a sample from something larger.

Mean of each feature

height (cm)
173.125
weight (kg)
69.5

Covariance matrix

The diagonal holds each feature's variance: its covariance with itself.

Correlation matrix

Covariance divided by both standard deviations, so every entry is between −1 and 1 and units no longer matter.

Values marked ≈ are rounded to fit. Select an entry to see it exactly.

height (cm) and weight (kg)

Covariance
Cov⁡(height (cm),weight (kg))=85.5\operatorname{Cov}(\text{height (cm)}, \text{weight (kg)}) = 85.5
Standard deviations
σheight (cm)≈8.559665,σweight (kg)≈10.253919\sigma_{\text{height (cm)}} \approx 8.559665,\quad \sigma_{\text{weight (kg)}} \approx 10.253919
Correlation
r=Cov⁡(height (cm),weight (kg))σheight (cm) σweight (kg)≈0.974136r = \frac{\operatorname{Cov}(\text{height (cm)}, \text{weight (kg)})}{\sigma_{\text{height (cm)}}\,\sigma_{\text{weight (kg)}}} \approx 0.974136

height (cm) and weight (kg) rise together, closely.

What the covariance matrix holds

Put the data in a matrix XX with one sample per row and one feature per column. Subtract each column's mean to get the centered data XcX_c, so every column averages 0. Then the covariance matrix is one matrix product:

Σ=1n−1XcTXc\Sigma = \frac{1}{n - 1} X_c^{T} X_c

Entry (i,j)(i, j) is the dot product of centered columns ii and jj, divided by n−1n - 1. It's positive when the two features tend to sit above their means together, negative when one is high while the other is low, and near 0 when knowing one tells you little about the other. The diagonal holds each feature's variance, its covariance with itself, and the matrix is always symmetric. The product XcTXcX_c^{T} X_c is worked through entry by entry in the Matrix Multiplication Calculator.

Sample or population, and what correlation adds

Dividing by n−1n - 1 gives the sample covariance, the usual choice when the rows are a sample from something larger: measuring spread around the sample's own mean slightly underestimates the true spread, and n−1n - 1 makes up for it. Dividing by nn gives the population covariance. NumPy's np.cov divides by n−1n - 1 by default and treats each *row* as a feature, so pass rowvar=False for data laid out as here.

Covariance carries units: height in centimeters against weight in kilograms gives cm·kg. Correlation removes them by dividing by both standard deviations:

rij=ΣijΣii Σjjr_{ij} = \frac{\Sigma_{ij}}{\sqrt{\Sigma_{ii}\,\Sigma_{jj}}}

That's the cosine of the angle between the two centered columns, the same quantity as in the Cosine Similarity Calculator, so it always lies between −1-1 and 11. Correlation only sees straight-line relationships. The "A curve" example has y=x2y = x^2, a perfect relationship, with correlation exactly 0.

Why machine learning cares

  • The shape of the data. The covariance ellipse in the picture is drawn from the eigenvectors and eigenvalues of Σ\Sigma. Its long axis is the direction the data spreads most. Principal component analysis is exactly that: the eigenvectors of the covariance matrix, sorted by eigenvalue. Send the matrix to the Eigenvalue and Eigenvector Calculator to find them.
  • Redundant features. Two features with correlation near ±1\pm 1 carry almost the same information. That makes regression coefficients unstable, and it's a common reason to drop a feature or reduce dimensions first.
  • Scaling. Features measured in large units dominate the covariance matrix. Standardizing each column first turns the covariance matrix into the correlation matrix, which is why many pipelines scale features before PCA.