Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions digital_image_processing/Principal_Component_Analysis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Principal Component Analysis (PCA) with Digits Dataset

This is a demonstration of Principal Component Analysis (PCA) using the Digits dataset from scikit-learn.

## About the Project

This project demonstrates how to apply Principal Component Analysis (PCA) on the Digits dataset using scikit-learn. PCA is a dimensionality reduction technique that helps visualize and reduce the dimensionality of a dataset while preserving its important information. It's widely used in machine learning and data analysis.

## Getting Started

### Prerequisites

To run this project, you need the following Python libraries installed:

- scikit-learn
- numpy
- matplotlib

You can install them using pip:

```bash


pip install scikit-learn numpy matplotlib


```

#### Dataset:
[Digits Dataset](https://scikit-learn.org/stable/auto_examples/datasets/plot_digits_last_image.html)


### Usage
Run the provided Jupyter Notebook (digits_pca.ipynb) to see PCA applied to the Digits dataset. The code will load the Digits dataset, apply PCA, and display the reduced-dimension data as output.


### Author

[Aswin P Kumar](https://github.com/AswinPKumar01)
<br>
[Connect with me](https://www.linkedin.com/in/aswinpkumarvit/)







63 changes: 63 additions & 0 deletions digital_image_processing/Principal_Component_Analysis/pca_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
"""PCA_Code.ipynb

Automatically generated by Colaboratory.

Original file is located at
https://colab.research.google.com/drive/1lzFebVtljdyiu1hHlRfE4mpxITUmH8bV

### **1. Installing Packages**
"""

!pip install -U scikit-learn

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: digital_image_processing/Principal_Component_Analysis/pca_code.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 13:1.
parser error: error at 12:1: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~

!pip install -U scikit-learn
                           ^

"""### **2. Importing Necessary Modules**"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.datasets import load_digits

"""### **3. Loading the datatset**"""

# Load the Digits dataset
digits = load_digits()
X = digits.data
y = digits.target

"""### **4. Applying PCA and reducing the image**"""

# Randomly select an image for demonstration
random_image_index = np.random.randint(0, X.shape[0])

# Define the number of principal components you want to keep
n_components = int(input("Enter the number of components you want to keep (in the range of 0 to 64): "))

# Apply PCA to the image data
pca = PCA(n_components=n_components)
X_pca = pca.fit_transform(X)

# Inverse transform to get the reduced-dimension image
X_inverse = pca.inverse_transform(X_pca)

"""### **5. Displaying the reduced image**"""

# Original image
original_image = X[random_image_index].reshape(8, 8)

# Reduced-dimension image
reduced_image = X_inverse[random_image_index].reshape(8, 8)

# Plot the original and reduced images
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.imshow(original_image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(reduced_image, cmap='gray')
plt.title(f'Reduced to {n_components} Components')
plt.axis('off')

plt.show()