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
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Dimensionality Reduc | Master Data Science with Python lesso | BraveProgrammer
  1. Courses
  2. /
  3. Master Data Science with Python

Lesson 52 of 60

·python

Dimensionality Reduction – Principal Component Analysis (PCA)

20 minutes

Course progress87%

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

 sklearn.decomposition  PCA

pca = PCA(n_components=)   
X_pca = pca.fit_transform(X_scaled)
(, X_scaled.shape)
(, X_pca.shape)
from
import
0.95
# keep 95% variance
print
'Original shape:'
print
'Reduced 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.
Make it stickPractice what you've learned1 quiz · 4 practical tests · Optional activities from Master Data Science with PythonBrowse activities
Previous lessonUnsupervised Learning – K‑Means ClusteringContinue to lesson 53Model Selection – Cross‑ValidationBack to Master Data Science with Python curriculum