How to use this PCA calculator
- Pick a dataset, or paste a table under Paste your own data (or use Open a CSV file). Numeric columns become the variables. A text column such as a species or a diagnosis colours the points and serves as the class label.
- Choose whether to standardise: on uses the correlation matrix, off uses the covariance matrix.
- Read the scree plot and drag Cumulative threshold to see which k the rule picks.
- Plot the scores in 2D (with loading arrows for a biplot), as histograms by class, or in 3D.
- Open Step-by-step calculation, then download the scores, loadings or eigenvalues as CSV.
Everything is computed in your browser. Data you paste or open is never uploaded.
What the calculator computes
PCA finds the direction in which the data is most spread out, then the most spread-out direction at right angles to it, and so on. The steps panel shows each stage of the textbook recipe:
- Centre each column, and if standardising, divide by its standard deviation (with
n − 1):z = (x − mean) / sd. - Form
C = ZᵀZ / (n − 1): the covariance matrix, or the correlation matrix when scaled. A correlation matrix has a trace equal to the number of columns. - Diagonalise: solve
C v = λ vwith Jacobi rotations (C is symmetric), and sort the eigenvalues from largest to smallest. Signs are arbitrary, so each eigenvector is flipped to make its largest entry positive. - Explained variance ratio =
λₖ / Σλ. The cumulative explained variance is the running total. - Scores
T = Z Vare each row’s coordinates on the new axes. Keeping every component rebuilds the data exactly, and the steps panel prints that check.
On Iris the explained variance ratios are 92.46%, 5.31%, 1.71% and 0.52% without scaling, and 72.96%, 22.85%, 3.67% and 0.52% with it: the values scikit-learn reports. The eigenvalues agree with NumPy’s to within 10⁻¹⁴ of the largest one.
Covariance or correlation: why standardising matters
PCA looks for large variance, and variance has units. If one column is measured in large numbers, it wins by default. Iris shows this mildly: in raw centimetres petal length has the biggest spread, so the first component takes 92.46% of the variance. Standardise, and it drops to 72.96%.
The breast cancer data shows it severely. In the Japanese experiment behind this page, PCA on the 30 unstandardised columns gave a first component with 98.2% of the variance. That looks like one axis explaining nearly everything. The loadings told a different story: the component was worst area (weight 0.852) and mean area (0.517), and every other column had a weight of 0.06 or less. Those two columns have standard deviations of 568.9 and 351.6, two orders of magnitude above most of the rest. PCA had found the units, not the structure. After standardising, the first component falls to 44.3% and its weight spreads over many columns: mean concave points 0.261, mean concavity 0.258, worst concave points 0.251.
The two breast cancer presets reproduce those figures exactly, because PCA is deterministic: 98.20% and 44.27%. In the unstandardised biplot, only two arrows are long enough to label, and they are the two area columns. The cost is real as well. In the article, cutting at 80% kept one component without standardising and five with it, and classification accuracy went from 0.9069 to 0.9719, a difference of 6.5 points (logistic regression, 5-fold cross-validation). This page uses a different classifier, described below, and gets 0.870 and 0.953 for the same two cuts.
The 80% rule, and the trap in it
The usual rule is to keep components until the cumulative explained variance passes 80% (or 90%): “we kept 80% of the information.” But explained variance only measures how much of the spread a component reproduces. It says nothing about whether that spread relates to what you want to predict.
The article tested this with a deliberately hostile dataset of 20 columns. One strong common factor moves all 20 columns up and down together and produces nearly all the variance, much as the day’s temperature moves every sensor in a factory. The two classes differ only by a small shift along a direction at right angles to that factor: the first ten columns minus the last ten. Results:
- PC1 explained 88.57% of the variance and classified at 0.486, the same as a coin toss.
- PC2 explained 1.88% and classified at 0.952 on its own.
- The 80% rule stops at k = 1, so it keeps the useless component. Accuracy falls from 0.947 with all 20 components to 0.486.
The explained-variance trap preset builds the same kind of data in your browser: 1,200 rows, the common factor multiplied by 3, and a class shift of ±0.35. It uses this page’s own seeded random number generator rather than NumPy’s, so the numbers are close to the article’s but not identical. Sample #1 gives PC1 89.20% and PC2 1.82%. Used alone, PC1 classifies at 0.529 against a most-common-class baseline of 0.517, and PC2 classifies at 0.948. Across 200 generated samples, PC2 was the most useful single component every time. Its accuracy ranged from 0.92 to 0.96, while PC1 stayed between 0.46 and 0.54. When the article’s exact NumPy data is fed through this page’s code, it gives 88.57% and 1.88% to the digit.
Use the sliders to see where the trap comes from. On sample #1, set Common factor strength to 0 and the class signal becomes PC1, with 16.06% of the variance and accuracy 0.954 on its own. With nothing dominating, the 80% rule now wants 15 components.
On real data, 80% usually survives, and the reason matters
The article also ran the rule on two standard datasets, measuring accuracy at every k. On handwritten digits (64 columns), 80% is reached at k = 21 with accuracy 0.9054, against 0.9188 using every column. On breast cancer (30 columns), it is reached at k = 5 with 0.9719, against 0.9807. In both, accuracy levels off before the variance curve reaches 80%: breast cancer reaches 0.953 at k = 2 (63.2% cumulative), and digits reach 0.904 at k = 15 (70.9%).
So the rule did not fail there. But it did not succeed because it is sound. It succeeded because, in those datasets, the directions of large variance also carried the label. It breaks when a strong shared factor has nothing to do with the label, such as lighting, sensor drift, batch effects or each department’s way of filling in a form. That is common in practice, and the scree plot cannot warn you, because explained variance never looks at the label.
The practical rule follows from that. If the goal is reconstruction (compression, denoising, visualising without labels), choose k from explained variance. If the goal is prediction, measure prediction: the orange line on the scree plot does this for every k at once.
How the accuracy numbers here are measured
Each component alone is scored with a one-dimensional linear discriminant (class means, pooled variance, class priors), and the first k components together with a k-dimensional one. Both use stratified 5-fold cross-validation with fixed folds, so the numbers do not change on reload. PCA is fitted to all rows first, as in the article. It never sees the labels. The article used logistic regression, so expect differences of a few hundredths: on the article’s exact trap data, this page gives 0.507 for PC1, 0.953 for PC2 and 0.951 for all 20 components, against the article’s 0.486, 0.952 and 0.947. The dotted line is the accuracy of always answering the most common class (0.627 on breast cancer). Compare against that, not against 0.5.
Reading the biplot and 3D views
The loading arrows are each variable’s eigenvector coefficients on the two plotted components, scaled to fit. Arrows that point the same way belong to correlated variables. Up to eight are drawn, and very short ones are left unlabelled. Each axis is stretched to fill the plot unless you tick Same scale on every axis. Do that on the trap data and the separating PC2 shrinks to a thin band, since its spread is about a seventh of PC1’s. The 3D view is an orthographic projection of any three components. It turns slowly until you drag it.
Limits
- Up to 10,000 rows and 60 numeric columns. Rows with a blank,
NA,NaNor?in a numeric column are skipped, and the page reports how many. - Plain linear PCA. There is no rotation (varimax), no factor analysis and no kernel PCA.
- Component signs are arbitrary. Flipping one changes nothing about the analysis, so do not read meaning into a negative loading by itself.
- Shared links keep the settings and built-in datasets. Pasted data is never put into a URL.
Data sources
Iris: R. A. Fisher (1936), 150 flowers of three species, UCI Machine Learning Repository (doi:10.24432/C56C76), licensed CC BY 4.0. Breast Cancer Wisconsin (Diagnostic): W. Wolberg, O. Mangasarian, N. Street and W. Street, UCI Machine Learning Repository (doi:10.24432/C5DW2B), licensed CC BY 4.0. It has 569 tumours, 30 measurements and a malignant/benign diagnosis, and this page loads it only when you select it.