diff --git a/digital_image_processing/Principal_Component_Analysis/README.md b/digital_image_processing/Principal_Component_Analysis/README.md new file mode 100644 index 000000000000..feb3418594d8 --- /dev/null +++ b/digital_image_processing/Principal_Component_Analysis/README.md @@ -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) +
+[Connect with me](https://www.linkedin.com/in/aswinpkumarvit/) + + + + + + + diff --git a/digital_image_processing/Principal_Component_Analysis/pca_code.py b/digital_image_processing/Principal_Component_Analysis/pca_code.py new file mode 100644 index 000000000000..47ee60d0d751 --- /dev/null +++ b/digital_image_processing/Principal_Component_Analysis/pca_code.py @@ -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 + +"""### **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()