PCA

Python · Advanced Python & AI

Theory

Principal Component Analysis (PCA) reduces many correlated features to a few new ones called principal components. Each component is a weighted combination of the original features. The first component points in the direction where the data varies the most, the second captures the most remaining variation while being uncorrelated with the first, and so on.

Keeping only the first few components gives a smaller dataset that still holds most of the information. PCA(n_components=2).fit_transform(X_scaled) turns, say, 10 features into 2, which you can plot on a scatter chart. explained_variance_ratio_ reports the share of total variance each component keeps; you can also pass a fraction, PCA(n_components=0.95), to keep enough components for 95% of the variance. Scale features first, because PCA is driven by variance and large-unit columns would dominate.

Why we use it

High-dimensional data is hard to visualise, slow to train on and often redundant. PCA compresses it, removes correlation between features and can reduce noise, while keeping most of the signal.

When to use it

Use PCA to visualise high-dimensional data in 2-D/3-D, or to shrink many correlated numeric features before modelling. Avoid it when you need to explain results in terms of the original features, since components are mixtures.

How it works

  1. Numeric feature matrix X
  2. Scale features with StandardScaler
  3. PCA finds directions of maximum variance
  4. Enough variance explained? Yes: Use the chosen components. No: Increase n_components.
  5. Project the data onto the components
  6. Reduced features for plotting or modelling

Syntax

from sklearn.decomposition import PCA
X_reduced = PCA(n_components=2).fit_transform(X)

Example

pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X_scaled)

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