ZestCalc

PCA Calculator

Principal component analysis on your own data: the covariance or correlation matrix, its eigenvalues and loadings, the share of variance each component explains, and the data in the new coordinates, with a cloud you can flatten onto PC1.

Data

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

Features2
8 samples

Principal components

Updates as you type.

8 samples and 2 features.

PCA of the covariance matrix. A feature measured in bigger numbers carries more weight.

Covariance matrix

≈73.26885.585.5≈105.14

Variance by component

ComponentVarianceShareRunning total
PC1≈176.1898.7%98.7%
PC2≈2.23261.3%100.0%
Components kept1

Keeping 1 of 2 components keeps 98.7% of the variance.

Loadings

Each row is one component's direction as a unit vector: how much of each feature it's made of. A sign is arbitrary; flipping a whole row gives the same direction.

≈0.639≈0.769≈0.769≈-0.639

Data in the new coordinates

Each sample's position along the kept components: its centered row dotted with each loading.

≈-19.54≈-10.96≈-10.58≈-0.3343≈0.04445≈8.884≈13.75≈18.74

Values marked ≈ are rounded. Shares, loadings, and coordinates are always rounded.

What PCA does

Principal component analysis finds new axes for your data. The first, PC1, points the way the samples spread out the most. The second is the direction of most remaining spread at a right angle to the first, and so on. Each component's variance is how much the data spread along it, and the components together hold exactly the variance the original features had.

Often the first two or three components hold almost all of it. Keeping only those turns many features into a few, with little lost. That's dimensionality reduction: for plotting high-dimensional data, cutting noise, or feeding a smaller input to another model.

Where the components come from

The directions are the eigenvectors of the covariance matrix, which the Covariance Matrix Calculator builds:

C=1n−1XcTXcC = \frac{1}{n-1} X_c^{T} X_c

where XcX_c is the data with each column's mean subtracted. Because CC is symmetric, its eigenvectors are at right angles to each other and its eigenvalues are never negative. The variance along a unit direction vv is vTCvv^{T} C v, and the largest that can be is the top eigenvalue, reached at its eigenvector. The Eigenvalue and Eigenvector Calculator shows the same decomposition for any matrix.

Libraries such as scikit-learn get the same directions from the SVD of XcX_c instead: the right singular vectors are the eigenvectors of CC, and σi2/(n−1)=λi\sigma_i^2 / (n-1) = \lambda_i. The SVD Calculator computes that factorization.

Reading the result

  • Share of variance (scikit-learn's explained_variance_ratio_) is each eigenvalue divided by their sum. The running total says how much a few components keep.
  • Loadings are the eigenvectors: PC1 = 0.71⋅0.71 \cdot height +0.70⋅+ 0.70 \cdot weight reads as "overall size". A component's sign is arbitrary, so a whole row of loadings can flip without changing anything.
  • The data in the new coordinates are the projections XcVkX_c V_k, where VkV_k holds the first kk loadings as columns. Their variances are exactly the eigenvalues, and they're uncorrelated with each other.
  • The scree chart shows the shares falling off. A common rule is to keep components up to where the running total reaches 90% or so, or up to the "elbow" where the bars flatten out.

Center only, or standardize?

PCA of the covariance matrix weighs a feature by its variance, which depends on its units. Measure area in square meters next to the number of rooms and area takes nearly all of PC1, because its numbers are bigger, not because it matters more. The "Different units" example shows this.

Standardizing divides each feature by its standard deviation first, so every feature has variance 1 and PCA works on the correlation matrix. Use it when the features are in different units. Leave it off when they share units and their spread is meaningful, like pixel brightness or scores on the same scale.

Exact and rounded values

With the data only centered, the covariance matrix has exact fractions, and when its eigenvalues can be written exactly (whole numbers, fractions, or square roots for two features) this calculator shows them that way in fractions view. Standardizing divides by square roots, so those results are rounded. Loadings, shares, and coordinates are always rounded, since unit vectors almost always involve square roots.