scikit-learn
PCA
percentage value
machine learning
data analysis

Explanation of the percentage value in scikit-learn PCA method

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In scikit-learn PCA, the percentage value usually refers to explained variance ratio: the fraction of total variance captured by a principal component or by a chosen set of components. It is not model accuracy and it is not "percentage of features kept." It is a measure of how much variation in the original data is preserved.

What explained variance ratio means

Each principal component captures some amount of the dataset's variance. Scikit-learn exposes that as:

python
pca.explained_variance_ratio_

If the first component has value 0.62, that means:

  • the first principal component explains 62 percent of the total variance in the original dataset

If the first two values are 0.62 and 0.21, then together they explain 83 percent of the variance.

A small example

python
1from sklearn.datasets import load_iris
2from sklearn.decomposition import PCA
3from sklearn.preprocessing import StandardScaler
4
5X, _ = load_iris(return_X_y=True)
6X = StandardScaler().fit_transform(X)
7
8pca = PCA(n_components=2)
9X_reduced = pca.fit_transform(X)
10
11print(pca.explained_variance_ratio_)
12print(pca.explained_variance_ratio_.sum())

The first print shows the per-component percentages as fractions. The second print shows how much total variance the chosen components explain together.

The percentage is about variance preserved

PCA finds directions that retain as much variance as possible under a lower-dimensional representation. So the percentage answers:

  • how much of the original spread of the data is preserved in this reduced representation

It does not answer:

  • how predictive the representation will be for a supervised task
  • how important an original feature is by itself

That distinction matters because high explained variance does not automatically mean better downstream classification performance.

n_components=0.95 means keep enough components to explain 95 percent

Scikit-learn allows:

python
pca = PCA(n_components=0.95)

In that case, PCA chooses as many components as needed so that the cumulative explained variance ratio reaches at least 95 percent.

That is often what people mean when they ask about "the percentage value" in PCA. It is a threshold on preserved variance, not a literal count of components.

Cumulative explained variance is often the real selection tool

When deciding how many components to keep, cumulative variance is usually more useful than the individual percentages:

python
1import numpy as np
2
3cumulative = np.cumsum(pca.explained_variance_ratio_)
4print(cumulative)

This tells you how many components are needed to reach targets such as 90 percent, 95 percent, or 99 percent of total variance.

Standardization usually matters

PCA is scale-sensitive. If one feature has a much larger numeric scale than the others, it can dominate the variance and distort the explained-variance percentages.

That is why PCA is often preceded by standardization:

python
from sklearn.preprocessing import StandardScaler

X_scaled = StandardScaler().fit_transform(X)

Without scaling, the percentage values may reflect feature units more than meaningful structure.

Common Pitfalls

  • Thinking explained variance ratio is the same as prediction accuracy.
  • Treating the percentage as "percentage of original features kept."
  • Choosing PCA dimensions without looking at cumulative explained variance.
  • Forgetting to scale features before PCA when scales differ substantially.
  • Assuming a high explained-variance percentage guarantees the best supervised-model performance.

Summary

  • The percentage value in scikit-learn PCA usually refers to explained variance ratio.
  • It measures how much of the dataset's total variance a component or set of components preserves.
  • 'n_components=0.95 means keep enough components to preserve about 95 percent of the variance.'
  • Cumulative explained variance is the usual tool for choosing dimensionality.
  • Explained variance is about data representation, not directly about model accuracy.

Course illustration
Course illustration

All Rights Reserved.