-
-
Notifications
You must be signed in to change notification settings - Fork 49.9k
Added the code for Principal Component Analysis in Digital Image Processing Directory #10606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
54ffe66
Create README.md for PCA
The-APK 03159a9
Update README.md
The-APK 5c2dcaa
Added the code for PCA
The-APK 8f257c0
Merge branch 'TheAlgorithms:master' into master
The-APK 4b706af
Delete digital_image_processing/Principal_Component_Analysis/PCA_Code…
The-APK e9b2ca7
added code for pca
The-APK 527f751
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
digital_image_processing/Principal_Component_Analysis/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
63
digital_image_processing/Principal_Component_Analysis/pca_code.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| """### **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() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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