Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Master Data Science with Python

Lesson 52 of 60 · python

Dimensionality Reduction – Principal Component Analysis (PCA)

Duration: 20 minutes

Principal Component Analysis (PCA)

PCA reduces dimensionality by projecting data onto orthogonal axes (principal components) that capture maximal variance.

Why it matters

  • Visualization: reduce to 2‑D/3‑D.
  • Noise reduction: discard low‑variance components.
  • Speed up downstream algorithms.

Performing PCA with scikit‑learn

from sklearn.decomposition import PCA

pca = PCA(n_components=0.95)   # keep 95% variance
X_pca = pca.fit_transform(X_scaled)
print('Original shape:', X_scaled.shape)
print('Reduced shape:', X_pca.shape)

Explained variance ratio

explained = pca.explained_variance_ratio_
plt.bar(range(1, len(explained)+1), explained)
plt.xlabel('Principal Component')
plt.ylabel('Explained Variance Ratio')
plt.title('Scree Plot')
plt.show()

Visualizing first two components

plt.scatter(X_pca[:,0], X_pca[:,1], c=y, cmap='coolwarm', edgecolor='k')
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.title('PCA Projection')
plt.show()

When to use PCA

  • High‑dimensional datasets (e.g., image pixels, text TF‑IDF).
  • Before clustering or classification to reduce noise.

Caveats

  • Linear method – cannot capture non‑linear structure.
  • Components are linear combinations, not directly interpretable.

Info

Standardize data before PCA – otherwise scale dominates the components.

Previous: Unsupervised Learning – K‑Means ClusteringNext: Model Selection – Cross‑Validation