Skip to content

Added Principal Component Analysis #9610

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
doctest fixed for fit function
  • Loading branch information
kausthub-kannan committed Oct 3, 2023
commit 739f389940bab8c51fa967bdba4ea16381b214d8
6 changes: 6 additions & 0 deletions machine_learning/principal_component_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def fit(self, vector: np.ndarray[float]) -> None:
3. Calculate the eigenvalues and eigenvectors of the covariance matrix
4. Sort the eigenvalues and eigenvectors in descending order of the eigenvalues
5. Choose the first n eigenvectors, where n is the number of components

>>> test_data = np.array([
... [1.2, 2.3, 3.4], [4.5, 5.6, 6.7], [7.8, 8.9, 9.0], [10.1, 11.2, 12.3]
... ])
>>> test_pca = PCA(2)
>>> test_pca.fit(test_data)
"""
self.mean = np.mean(vector, axis=0)
vector = vector - self.mean
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
vector = vector - self.mean
vector -= self.mean

Expand Down