removed scikit learn dependency

This commit is contained in:
Abubakar Abid 2021-01-24 23:37:33 -06:00
parent 2d7370e4f3
commit 6d3940d51a
3 changed files with 31 additions and 3 deletions

View File

@ -7,7 +7,6 @@ Flask-BasicAuth
paramiko paramiko
scipy scipy
IPython IPython
scikit-learn
scikit-image scikit-image
analytics-python analytics-python
pandas pandas

View File

@ -1,8 +1,38 @@
import numpy as np import numpy as np
from sklearn.decomposition import PCA
SMALL_CONST = 1e-10 SMALL_CONST = 1e-10
class PCA:
"""
Credit: https://www.python-engineer.com/courses/mlfromscratch/11_pca/
"""
def __init__(self, n_components, random_state):
self.n_components = n_components
self.components = None
self.mean = None
self.random_state = random_state
def fit(self, X):
np.random.seed(self.random_state)
self.mean = np.mean(X, axis=0)
X = X - self.mean
cov = np.cov(X.T)
eigenvalues, eigenvectors = np.linalg.eig(cov)
eigenvectors = eigenvectors.T
idxs = np.argsort(eigenvalues)[::-1]
eigenvalues = eigenvalues[idxs]
eigenvectors = eigenvectors[idxs]
self.components = np.real(eigenvectors[0:self.n_components])
def transform(self, X):
X = X - self.mean
return np.dot(X, self.components.T)
def fit_transform(self, X):
self.fit(X)
return self.transform(X)
def calculate_similarity(embedding1, embedding2): def calculate_similarity(embedding1, embedding2):
""" """
Scores the similarity between two embeddings by taking the cosine similarity Scores the similarity between two embeddings by taking the cosine similarity

View File

@ -23,7 +23,6 @@ setup(
'paramiko', 'paramiko',
'scipy', 'scipy',
'IPython', 'IPython',
'scikit-learn',
'scikit-image', 'scikit-image',
'analytics-python', 'analytics-python',
'pandas', 'pandas',