Professor Turing

Study guides

What is principal component analysis?

Last updated: 2026-11-23

Principal component analysis (PCA) replaces your original variables with a new set of variables, called components, that are ordered by how much of the variation in the data they hold. The first component is the direction along which the data spread out the most, and each later component is the next most spread-out direction that is at right angles to the ones before it.

Start learning No account needed

People use it to describe a dataset with fewer numbers than it has variables, and to look at many-variable data in two dimensions. If you want the idea of spread first, learn statistics on your own covers it.

1. The pieces

The scikit-learn documentation describes PCA as a way to decompose a multivariate dataset into a set of successive orthogonal components that explain a maximum amount of the variance. Orthogonal means at right angles, so the components do not repeat each other. PCA first centers each variable by subtracting its mean. It then finds the components with a matrix method called the singular value decomposition. The documentation says the variance along each component equals one of the largest eigenvalues of the covariance matrix of the data.

You can read that in three steps. Build the covariance matrix of the centered variables. Find its eigenvalues, each of which is the variance along one component, and its eigenvectors, each of which gives the weights that combine the original variables into that component. Then rank the components by eigenvalue. The documentation notes that each component is a linear combination of the original variables, which means a weighted sum of them. A component is therefore not one of your original columns; it blends several of them.

Each component has an explained variance ratio, which is its share of the total variance. If you keep every component, the ratios add up to 1. If you keep only the first few, the ratios tell you how much of the variation the shorter description still holds. The documentation shows this on the iris data, which has 4 variables, by projecting them onto the 2 dimensions that explain the most variance.

2. A worked example: four points in two variables

Take four points with two variables, x and y: (1, 1), (2, 3), (4, 3) and (5, 5). The numbers are made up for this page. The mean of x is 3 and the mean of y is 3, so centering gives (−2, −2), (−1, 0), (1, 0) and (2, 2).

The sample covariance matrix divides sums by n − 1 = 3. The variance of x is (4 + 1 + 1 + 4) / 3 = 10/3, the variance of y is (4 + 0 + 0 + 4) / 3 = 8/3, and the covariance of x and y is (4 + 0 + 0 + 4) / 3 = 8/3. Multiplying by 3 to clear the fractions gives the matrix with rows 10, 8 and 8, 8, and its eigenvalues are 9 + √65 and 9 − √65. Divide by 3 to return to variances: 5.687 for the first component and 0.313 for the second.

The total variance is 10/3 + 8/3 = 6, which is also 5.687 + 0.313. So the first component holds 5.687 / 6 = 94.8 percent of the variance and the second holds 5.2 percent. The first component's weights are 0.750 on x and 0.662 on y, which points up and to the right along the line the four points follow. The second component's weights are −0.662 and 0.750, at right angles to the first.

1 1 2 2 3 3 4 4 5 5 x y mean (3, 3) first component

Four points, the first component line, and the short gaps it leaves out.

Each point gets a score on each component, which is its centered values times those weights. The point (−2, −2) scores −2 × 0.750 + (−2) × 0.662 = −2.823 on the first component. The four scores on the first component are −2.823, −0.750, 0.750 and 2.823, and their sample variance is 5.687, as the eigenvalue says. If we keep only that first score, one number per point replaces two, and we keep 94.8 percent of the variance.

3. Why the units you use change the answer

The scikit-learn documentation says PCA centers but does not scale the input data for each feature. That matters because variance depends on units. Suppose y in our example is measured in units ten times as large, so the points become (1, 10), (2, 30), (4, 30) and (5, 50). Nothing about the relationship between the variables has changed, but y now has the larger variance, so the first component lines up almost entirely with y. Its weights become 0.100 on x and 0.995 on y, and it holds 99.8 percent of the variance instead of 94.8.

One way to remove the effect of units is to standardize each variable first, by subtracting its mean and dividing by its standard deviation. For two standardized variables the covariance matrix is the correlation matrix, so the eigenvalues are 1 plus r and 1 minus r, where r is the correlation. Here r = 8 / √80 = 0.894, so the eigenvalues are 1.894 and 0.106 and the first component holds 94.7 percent. That figure is the same whether y is in the original units or ten times larger. Whether to standardize depends on the question; this page only shows that the choice changes the result, and the script checks the arithmetic.

4. A common misconception: the first component is the most important variable

It is natural to read component 1 as a pick of one of your original variables. The documentation describes each component as a linear combination of the original variables, and in our example the first component is 0.750 of x plus 0.662 of y, so it is a blend of both. Component 1 is also not a claim about importance to some outcome. It ranks directions by how much the data vary, and PCA does not use a target variable at all.

The same documentation also lists a limit that follows from this. The components PCA finds have dense expressions, meaning nonzero weights on most of the original variables, which the documentation says can make them harder to interpret, and it offers sparse PCA as a variant that produces more interpretable components.

5. Test yourself

Close this page and take the four points (0, 0), (1, 1), (2, 2) and (3, 3). Without computing a covariance matrix, say what share of the variance the first component holds and what the second component holds, and why. Reopen the page and compare. The first component should hold all of it, a share of 1, and the second should hold none, because every point sits on one line, so a single direction describes the data fully. The first step where you slipped is the part to study again. For the general method, see how to test yourself.

What the sources found, and their limits

scikit-learn user guide, section 2.5 Decomposing signals in components, subsection 2.5.1 Principal component analysis (PCA), and the API page for sklearn.decomposition.PCA. Documentation for the scikit-learn library. They describe PCA as successive orthogonal components that explain a maximum amount of the variance, that the input data is centered but not scaled for each feature before the singular value decomposition, that the explained variance equals the largest eigenvalues of the covariance matrix, that the explained variance ratios add to 1.0 when all components are stored, that the components are linear combinations of the original variables, and that PCA components are dense, with sparse PCA as a more interpretable variant. Source: scikit-learn.org, decomposing signals in components and the PCA class page, both read on 2026-11-23.

Limits: the sources are software documentation, not a textbook, so we read them for what PCA does and not for proofs. We did not read the sections on incremental, kernel or probabilistic PCA beyond their headings. The three-step reading of the covariance matrix, the four points, the weights and scores, the effect of ten times larger units and the standardized shares are our own arithmetic in the script and are not from the documentation. The data in the worked example are made up.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.

Bring a topic you are studying, such as why the units of a variable change what PCA finds. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through.

It does not read or mark a written answer, and it does not write your explanation for you, so the explanation has to be yours. You must be 18 or older, and conversations are deleted after 7 days.

FAQ

How many components does PCA give you? As many as there are variables, though you usually keep only the first few. In our example there are 2 variables, so there are 2 components, and the first one holds 94.8 percent of the variance. If you keep all of them, their shares of the variance add up to 1.

Do I need to standardize the data before PCA? scikit-learn's PCA centers each variable but does not scale it, so scaling is a choice you make before calling it. In our example, writing the second variable in units ten times as large moves the first component's share of the variance from 94.8 percent to 99.8 percent, and standardizing both variables gives 94.7 percent whichever units were used.

Start with what you're working on

Tell the tutor which topic you are studying and let it ask you the questions.

Start learning No account needed

See also: all study guides