import numpy as np
class LDA:
def __init__(self, n_components):
self.n_components = n_components
self.linear_discriminants = None
def fit(self, x, y):
n_features = x.shape[1]
class_labels = np.unique(y)
mean_overall = np.mean(x, axis=0)
S_W = np.zeros((n_features,n_features))
S_B = np.zeros((n_features,n_features))
for c in class_labels:
X_c = x[y==c]
mean_c = np.mean(X_c, axis=0)
S_W+= (X_c - mean_c).T.dot(X_c-mean_c)
n_c = X_c.shape[0]
mean_diff = (mean_c - mean_overall).reshape(n_features,1)
S_B += n_c*(mean_diff).dot(mean_diff.T)
A = np.linalg.inv(S_W).dot(S_B)
eigenvalues, eigenvectors = np.linalg.eig(A)
eigenvectors = eigenvectors.T
idxs= np.argsort(abs(eigenvalues))[::-1]
eigenvalues = eigenvalues[idxs]
eigenvectors = eigenvectors[idxs]
self.linear_discriminants = eigenvectors[0:self.n_components]
def transform (self, x):
return np.dot(x, self.linear_discriminants.T)
#### pra ser igual do PCA
from sklearn import datasets
#pip install lda
import matplotlib.pyplot as plt
#import numpy as np
#from lda import LDA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
data = datasets.load_iris()
x=data.data
y=data.target
lda= LDA(2) #(dois exios)
lda.fit (x,y)
X_projected = lda.transform(x)
print ('shape of x', x.shape)
print ('shape of transformed x:', X_projected.shape)
x1 = X_projected[:,0]
x2 = X_projected[:,1]
plt.scatter (x1,x2,c=y, edgecolor='none', alpha=0.8, cmap=plt.cm.get_cmap('viridis',3))
plt.xlabel('linear discriminant 1')
plt.ylabel('linear discriminant 2')
plt.colorbar()
plt.show()