Thuta Learning
AdvancedData & Databasesintermediate

Dimensionality Reduction (PCA)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Dimensionality Reduction (PCA) without being intimidated by it
  • Be able to run scikit-learn code yourself
  • Apply this concept in a real project right away

Let's Think About This for a Second

The Curse of Dimensionality refers to how, as the number of features (dimensions) increases, model training gets harder (because data points become 'sparse') — it's also impossible to visualize a dataset with 100+ features on a 2D/3D graph. PCA (Principal Component Analysis) is a technique that compresses a large number of features (columns) into a small number of features (Principal Components) that retain 'as much information as possible' — if you can reduce 100 features down to 10 Principal Components (retaining 95%+ of the information), it helps with both training speed and visualization.

Connecting to a Real Scenario

Writing `from sklearn.decomposition import PCA; pca = PCA(n_components=2); X_reduced = pca.fit_transform(X)` compresses a dataset with 20 features down to 2 Principal Components, which you can then visualize as a 2D scatter plot with `matplotlib` — looking at `pca.explained_variance_ratio_` shows you what percentage of the original information each Principal Component retains.

Let's Look at It Together

python
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

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

print(f"Explained variance ratio: {pca.explained_variance_ratio_}")
print(f"Total variance retained: {sum(pca.explained_variance_ratio_):.2%}")

plt.scatter(X_reduced[:, 0], X_reduced[:, 1])
plt.show()
You should see
Explained variance ratio: [0.62 0.24]
Total variance retained: 86.00%

Try It in 5 Minutes

Compress a sample dataset with 5+ features down to 2 components using PCA, and check `explained_variance_ratio_` to write down how much information was retained.

A Quick Word of Caution

Don't assume PCA will 'increase' classification/regression accuracy — PCA's purpose is dimensionality reduction (for speed/visualization), and since some information is lost, accuracy can actually decrease.

Easy traps

  • Using PCA directly without feature scaling — since PCA is a variance-based algorithm, features with different scales can skew the results (you should always run StandardScaler before PCA)
  • Assuming you can 'directly interpret' PCA results (Principal Components) the same way you would original features like age or income — since a Principal Component is a mathematical combination of all the original features, direct interpretation is difficult

Now Try It Yourself

Compress a sample dataset with 5+ features down to 2 components using PCA, and check `explained_variance_ratio_` to write down how much information was retained.

You'll know it worked when: Explained variance ratio: [0.62 0.24] Total variance retained: 86.00%

Dimensionality Reduction (PCA) | Thuta Learning