-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
feat: Implement Principal Component Analysis (PCA) #12595
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
Changes from all commits
44cb411
ac605b5
e735cbe
75faa9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Principal Component Analysis (PCA) is a dimensionality reduction technique | ||
commonly used in machine learning. It transforms high-dimensional data into | ||
lower dimensions while retaining most of the information. | ||
|
||
Here,we use a dataset (Iris dataset) and apply PCA to reduce the | ||
dimensionality. We compute the principal components and transform the dataset | ||
into a lower-dimensional space. | ||
|
||
We reduce the number of columns form 4 to 2 | ||
|
||
""" | ||
|
||
import numpy as np | ||
from sklearn.decomposition import PCA | ||
from sklearn.preprocessing import StandardScaler | ||
from sklearn.datasets import load_iris | ||
|
||
|
||
def collect_dataset(): | ||
"""Collect dataset (Iris dataset) | ||
:return: Feature matrix and target values | ||
""" | ||
data = load_iris() | ||
return np.array(data.data), np.array(data.target) | ||
|
||
|
||
def apply_pca(data_x, n_components): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
"""Apply Principal Component Analysis (PCA) | ||
:param data_x: Original dataset | ||
:param n_components: Number of principal components | ||
:return: Transformed dataset and explained variance | ||
""" | ||
# Standardizing the features | ||
scaler = StandardScaler() | ||
data_x_scaled = scaler.fit_transform(data_x) | ||
|
||
# Applying PCA | ||
pca = PCA(n_components=n_components) | ||
principal_components = pca.fit_transform(data_x_scaled) | ||
|
||
# Explained variance ratio | ||
explained_variance = pca.explained_variance_ratio_ | ||
|
||
return principal_components, explained_variance | ||
|
||
|
||
def main(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: |
||
"""Driver function""" | ||
data_x, data_y = collect_dataset() | ||
# Set number of principal components | ||
n_components = 3 | ||
|
||
# Apply PCA | ||
transformed_data, variance_ratio = apply_pca(data_x, n_components) | ||
|
||
print("Transformed Dataset (First 5 rows):") | ||
print(transformed_data[:5]) | ||
|
||
print("\nExplained Variance Ratio:") | ||
print(variance_ratio) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
As there is no test file in this pull request nor any test function or class in the file
machine_learning/principle_component_analysis.py
, please provide doctest for the functioncollect_dataset
Please provide return type hint for the function:
collect_dataset
. If the function does not return a value, please provide the type hint as:def function() -> None: