PCA (Dimensionality Reduction)

Python · Machine Learning (Scikit-Learn)

Theory

Principal Component Analysis (PCA) replaces many correlated columns with a few new ones called principal components. The first component is the direction in the data with the most variance, the second is the direction with the most remaining variance at right angles to the first, and so on. Each component is a weighted mix of the original columns (pca.components_), and explained_variance_ratio_ says what share of the total variance each one keeps.

If maths, physics and chemistry marks rise and fall together, and English and Hindi do too, five subjects collapse into about two components, roughly 'science' and 'language', keeping most of the information. Standardise first, because PCA is driven by variance and a column with big units would dominate. PCA(n_components=2) keeps two, and PCA(n_components=0.95) keeps as many as needed for 95% of the variance.

Why we use it

Fewer columns make data easier to plot (two components fit a scatter plot), speed up models, reduce noise and remove redundancy between correlated features, which can help models that struggle with many inputs.

When to use it

Use PCA to visualise high-dimensional data, compress many correlated numeric features or speed up distance-based models. Avoid it when you must explain predictions in terms of original columns, since components are mixtures; try feature selection instead.

How it works

  1. Many correlated numeric features
  2. Standardise every column
  3. PCA finds the direction of greatest variance (PC1)
  4. Next components: most remaining variance, at right angles
  5. Enough cumulative variance kept (e.g. 90-95%)? Yes: Keep those components. No: Add another component.
  6. Smaller feature matrix for plotting or modelling

Syntax

from sklearn.decomposition import PCA
X_scaled = StandardScaler().fit_transform(X)
pca = PCA(n_components=2)
X_2d = pca.fit_transform(X_scaled)
pca.explained_variance_ratio_  pca.components_

Example

pca = PCA(n_components=0.95)
X_reduced = pca.fit_transform(X_scaled)
print(pca.n_components_, pca.explained_variance_ratio_.round(3))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic