Skip to content

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

Closed
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
65 changes: 65 additions & 0 deletions machine_learning/principle_component_analysis.py
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

Check failure on line 17 in machine_learning/principle_component_analysis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/principle_component_analysis.py:14:1: I001 Import block is un-sorted or un-formatted


def collect_dataset():

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 function collect_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:

"""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):

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 function apply_pca

Please provide return type hint for the function: apply_pca. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data_x

Please provide type hint for the parameter: n_components

"""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():

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 function main

Please provide return type hint for the function: main. If the function does not return a value, please provide the type hint as: def function() -> None:

"""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()
Loading