VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/implementing-pca-in-python-with-scikit-learn/

⇱ Implementing PCA in Python with scikit-learn - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementing PCA in Python with scikit-learn

Last Updated : 23 Jul, 2025

Principal Component Analysis (PCA) is a dimensionality reduction technique. It transform high-dimensional data into a smaller number of dimensions called principal components and keeps important information in the data. In this article, we will learn about how we implement PCA in Python using scikit-learn. Here are the steps:

Step 1: Import necessary libraries

We import all the libraries needed like numpy , pandas, matplotlib, seaborn and scikit learn.

Step 2: Load the Data

We will use breast cancer dataset. This dataset has 569 data items with 30 input attributes. There are two output classes-benign and malignant. This reads the dataset file and displays the first 5 rows. You can download the dataset from here.

Output:

👁 Dataset
Dataset

Step 3: Data Cleaning and Preprocessing

It drops unnecessary columns like id, Unnamed: 32 and converts diagnosis column: Malignant to 1 and Benign to 0.

Step 4: Separate Features and Target

In this separate features X contains input features (30 columns) and y contains the target labels (0 or 1)

Step 5: Standardize the Data

StandardScaler transforms features so they all have a mean = 0 and standard deviation = 1 which helps PCA to treat all features equally.

Output:

👁 Standard-Scaler
Standard Scaler

Step 6: Apply PCA Algorithm

It reduces the data to 2 principal components. PCA finds combinations of original features that explain the most variation in the data.

Output:

[[ 9.19283683 1.94858307]
[ 2.3878018 -3.76817174]]

We reduce 30 features to 2 components. Each row now has 2 values (PC1, PC2) instead of 30. These components contain the most variation from original data.

Step 7: Explained Variance

It tells how much information each principal component holds.

Output:

  • Explained variance: [0.44272026 0.18971182]
  • Cumulative: [0.44272026 0.63243208]

PC1 explains 44% of data and PC2 explains 19%. Combined these 2 components explain 63% of all data variation.

Step 8: Visualization Before vs After PCA

First plot shows original scaled data using first 2 features and second plot shows reduced data using PCA's 2 components. Colors represent diagnosis Benign or Malignant.

Output:

👁 Original_data
Original Data
👁 PCA
PCA Transformed Data

Step 9: Train a Model on PCA Data

It splits PCA data into training and test sets. Train aLogistic Regression model to classify tumors and predicts and evaluate the model.

Output:

👁 Confusion_matrix
Classification report

Step 10: Confusion Matrix

It shows how many predictions were correct or incorrect and helps to visualize true vs. false predictions.

Output:

👁 cmp
Confusion matrix

Step 11: Reconstruct Data and Check Information Loss

PCA reduces data size but some information is lost. This step converts reduced data back to its original shape and measures how much data was lost in the reduction process.

Output:

Reconstruction Loss: 0.3676

As shows how much info was lost during PCA. A loss of 0.3676 means PCA with 2 components retains good structure.

Complete Code: click here

Comment