mirror of
https://github.com/gradio-app/gradio.git
synced 2024-12-21 02:19:59 +08:00
removed scikit learn dependency
This commit is contained in:
parent
2d7370e4f3
commit
6d3940d51a
@ -7,7 +7,6 @@ Flask-BasicAuth
|
||||
paramiko
|
||||
scipy
|
||||
IPython
|
||||
scikit-learn
|
||||
scikit-image
|
||||
analytics-python
|
||||
pandas
|
||||
|
@ -1,8 +1,38 @@
|
||||
import numpy as np
|
||||
from sklearn.decomposition import PCA
|
||||
|
||||
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):
|
||||
"""
|
||||
Scores the similarity between two embeddings by taking the cosine similarity
|
||||
|
Loading…
Reference in New Issue
Block a user