Concepts / Variance Explained by Principal Components

Variance Explained by Principal Components

The principal components of a fitted PCA object are stored in components_, with one row per component ordered by importance and one column per original feature.

  • Programming

Why the New Axes Need Reading

Once you have fitted PCA and projected your data onto the new axes, a scatter plot of the first two principal components can look clean and informative. The trouble is that the axes themselves rarely come with a label you can read off directly. Each axis points along a direction in the original data space, and that direction is not one of the measurements you originally collected. It is a mixture of them.

This article closes that gap. You will learn where the information describing each axis is stored, how to lay it out so the numbers can be read, and why the mixing of features is the source of most of the difficulty. The goal is not simply to print the numbers. It is to know what they say about the data and to recognize when a reading of them is wrong.

Finding the Components in a Fitted Object

After a PCA object has been fitted, the directions it found are kept in an attribute called components_. You do not need to compute them again. You read them from the fitted object. Each direction is one principal component, and the components are ordered by importance: the first captures the largest share of the variance in the data, the second captures the next largest, and so on.

components_[0, 0]component 1, feature 1components_[0, 29]component 1, feature 30components_[1, 0]component 2, feature 1components_[1, 29]component 2, feature 30same row: one component across all featuressame column: one feature across all components
Which row and which column of components_ hold the weight of a given feature in a given component?

The layout of the array is the first thing to check. Its shape tells you how it is organized before you look at a single value. In the example the source works through, the first number is the count of components, which is 2, and the second number is the count of original features, which is 30. The array therefore has one row per component and one column per feature. Each row is a direction, and each column is a feature that the direction draws on.

Counting Rows and Columns Together

Two questions about the layout come up constantly: how many components there are, and which features a given component covers. The answers come straight from the shape of the array and from the row and column that you are reading.

Worked example

Locating a single weight in components_

A PCA object was fitted to a dataset with 30 original features and kept 2 components. How do you find the weight that feature 30 contributes to the second component, and what does that weight mean?

  1. 1
    Read the shape

    The first number is the count of components (2) and the second is the count of features (30). So the array has 2 rows and 30 columns.

  2. 2
    Pick the row

    The second component is the second row. Rows are counted from zero in Python, so it sits at row index 1.

  3. 3
    Pick the column

    Feature 30 is the last column. Counting from zero, it sits at column index 29.

  4. 4
    Read the cell

    The value at row 1, column 29 is the weight of feature 30 in the second component. A large weight in absolute value means that feature matters more to that direction.

Result

components_[1, 29] is the weight of feature 30 in component 2. Reading it the other way, feature 30 is a column that touches both components, and component 2 is a row that touches all 30 features.

What do you think happens?

If the array has 2 rows and 30 columns, how many weights are stored in the second row?

  • 2

  • 30

  • 60

Reveal answer
Answer:

30

Each row holds one weight per original feature. The second row is one component, so it holds 30 weights, one for each of the 30 features. The total of 60 counts both rows together, not one row.

A Component as a Weighted Mix of Features

A principal component is never built from a single measurement. Its direction is spread across all of the original features at once, so every feature has some weight in it. This is why the axes are hard to name. An axis called after one measurement would be easy to explain. An axis built from thirty interacting measurements is not, because its meaning is a pattern across all of them.

In this notation, $x_1$ through $x_{30}$ are the original features, and each weight $w$ is one entry of components_. The first row of the array holds the 30 weights for the first component. Each weight says how strongly that feature pulls the component's direction. A large weight in absolute value means the feature matters more to that direction, and a weight near zero means the feature barely contributes to it.

Formula
Feature 1positive weightPrincipal component 1one direction over allfeaturesFeature 2positive weightFeature 3negative weightFeature 30small weightpushes uppushes uppushes downbarely moves
Every original feature contributes to the first component through its own weight, and the sign of that weight decides whether it pushes the component up or down.

The weights can also disagree with one another in sign. Some features may push the component in one direction while others push it the other way. A mixed-sign pattern tells you the component is contrasting groups of features rather than measuring one overall quantity. A component whose weights all share one sign is closer to a general measure that rises or falls across the features together.

Reading a Heat Map of Coefficients

A table of 60 numbers is hard to scan. A heat map turns each coefficient into a coloured cell, so you can see the sign pattern and the relative size of the weights at a glance. The source demonstrates this with the first two components of a breast cancer dataset, where the columns are the 30 features and the two rows are the components.

In that heat map, the first component has a single sign across its row, so its coloured cells are uniform in tone. That uniformity marks a general correlation among the features. The second component's row is split between two tones, which reflects its mixed signs. Both rows touch every column, which is the visual version of the point that every feature participates.

Pattern in the heat mapWhat it suggestsWhat to check in the printed numbers
One tone across a whole rowA general correlation: the features tend to rise and fall together on this componentConfirm that the weights share a sign and note which features have the largest magnitudes
Two tones split along a rowA contrast between two groups of features on this componentIdentify which features carry each sign and compare their sizes
Large cells of the same tone in one rowFeatures that move together on that axisCheck that the printed weights are large and share a sign
Large cells of opposite tones in one rowFeatures that trade off against each other along that axisCheck that the weights have opposite signs and comparable size
Pale cells in a rowFeatures that barely contribute to that componentCheck that the weights are close to zero before dismissing them

Reading patterns in a heat map of components_

The heat map is a guide, not the final answer. A colour can suggest a sign and a rough size, but it does not give you the exact weight. The source is explicit that interpretations should always be checked against the printed numbers.

Features That Move Together

Two features that carry the same sign in a component tend to move together in the data. If one feature is high, the others sharing its sign are likely to be high too. Features with opposite signs trade off against each other along that axis. The worked example below applies this reading to a small set of weights.

Worked example

Reading one component's weights

The first component has these weights for four features: feature A is 0.6, feature B is 0.5, feature C is -0.4, and feature D is -0.1. Which features move together, which trade off, and which barely matter?

  1. 1
    Group by sign

    Features A and B are positive. Features C and D are negative.

  2. 2
    Find the large weights

    A (0.6) and B (0.5) are large in absolute value. C (-0.4) is moderate. D (-0.1) is near zero.

  3. 3
    Read same-sign pairs

    A and B share a sign and both carry large weights, so they tend to rise and fall together along this component.

  4. 4
    Read opposite-sign pairs

    A and B trade off against C, because C has the opposite sign and a moderate size.

  5. 5
    Discount the small weight

    D is close to zero, so it barely shapes this component despite its negative sign.

Result

On the first component, A and B move together, C trades off against them, and D contributes little. The sign alone does not decide importance. The size of the weight matters too.

Where an Interpretation Goes Wrong

When an interpretation disagrees with what you expected, the useful question is where in the chain the mismatch enters. The chain runs from the fitted object, to the components_ attribute, to the printed numbers, to the written description. Most errors happen at one of two links: the orientation of the array, or the text you write about the numbers.

Columns read ascomponentsRows read ascomponentsSigns ignoredSigns checked inprinted numbersColour read as exactvalueWeights read asrelative sizesthenthenthenthen
The same weights can be misread in three ways that sound plausible. Each correction returns the reading to the orientation of the array and to the printed signs and sizes.
  • Common mistake

    Reading a column as if it were a component

    What it looks like

    Looking down feature 1's column and calling the 2 values in it the components' names

    Why it fails

    A column answers how much one feature contributes to each component. Components are the rows.

    Do this instead

    Read across a row to see which combination of features makes up a component.

  • Common mistake

    Naming a component after one feature

    What it looks like

    Saying the first component is simply the feature with the largest weight

    Why it fails

    Every feature has some weight in every component, so the axis is a pattern across all of them.

    Do this instead

    Describe the component as a pattern, identifying the groups of features that share a sign.

  • Common mistake

    Treating a shared sign as proof of cause

    What it looks like

    Concluding that feature A causes feature B to rise because both have positive weights

    Why it fails

    The source says same-sign features tend to move together in the data. That is a statement about co-movement, not a causal link.

    Do this instead

    Say the features tend to rise and fall together along that axis.

  • Common mistake

    Ignoring the sign of a mixed component

    What it looks like

    Reporting only the large positive weights of the second component

    Why it fails

    A mixed-sign component contrasts two groups, so dropping one group hides half of the pattern.

    Do this instead

    Report both sides of the contrast and their sizes.

  • Common mistake

    Trusting the heat map colours over the printed numbers

Practice Reading a Component

MEDIUM

A fitted PCA object has a components_ array with 2 rows and 30 columns. The first row holds these weights for features 1 to 4: 0.31, 0.28, -0.22, 0.02. Describe which features move together on the first component, which trade off, and what you can conclude from the weight for feature 4. Then state how you would check your reading against the printed numbers.

Hints
  • Identify the row first, then the column for each feature.
  • Group the features by the sign of their weights before comparing sizes.
  • A weight near zero contributes little regardless of its sign.

Carrying the Reading Forward

Reading PCA output is a sequence of checks. Confirm the orientation of components_, read across the row for a component, group the features by sign, and then verify the description against the printed numbers. Each step keeps the interpretation tied to the data rather than to the picture.

Key Takeaways

  • The principal components of a fitted PCA object are stored in components_, with one row per component ordered by importance and one column per original feature.
  • Each principal component is a direction built from all of the original features, so its weights describe a pattern across the whole dataset rather than a single measurement.
  • Mixed signs in a component mean its features contrast with one another, while a shared sign means the features tend to rise and fall together.
  • A heat map of components_ shows the sign pattern and relative weight sizes at a glance, but interpretations should always be checked against the printed numbers.
  • Most errors in reading PCA output come from misreading the orientation of the array or from the text written about the numbers.

Related concepts

More concepts starting with V