LDA理论推导
代码实现
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.datasets import make_classification
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
%matplotlib inline
X, y = make_classification(n_samples=1000, n_features=3, n_redundant=0, n_classes=3, n_informative=2,
n_clusters_per_class=1,class_sep =0.5, random_state =10)
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=30, azim=20)
ax.scatter(X[:, 0], X[:, 1], X[:, 2],marker='o',c=y)
pos=np.array([X[i] for i in range(y.size) if y[i]==1])
neg=np.array([X[i] for i in range(y.size) if y[i]==0])
mid=np.array([X[i] for i in range(y.size) if y[i]==2])
means0=np.mean(pos,axis=0).reshape(1,3)
means1=np.mean(neg,axis=0).reshape(1,3)
means2=np.mean(mid,axis=0).reshape(1,3)
means=np.mean(X,axis=0).reshape(1,3)
pos_norm=pos-means0
neg_norm=neg-means1
mid_norm=mid-means2
Sb=(mid.size)*(means2-means).T.dot(means2-means)+(pos.size)*(means0-means).T.dot(means0-means)+(neg.size)*(means1-means).T.dot(means1-means)
Sw=pos_norm.T.dot(pos_norm)+mid_norm.T.dot(mid_norm)+neg_norm.T.dot(neg_norm)
mat=np.linalg.inv(Sw).dot(Sb)
U,S,V=np.linalg.svd(mat)
W=U[:,:2]
z_pos=pos.dot(W)
z_neg=neg.dot(W)
z_mid=mid.dot(W)
plt.figure(figsize=(10,6))
plt.plot(z_pos[:,0],z_pos[:,1],'ro',label='good')
plt.plot(z_neg[:,0],z_neg[:,1],'bo',label='bad')
plt.plot(z_mid[:,0],z_mid[:,1],'yo',label='mid')
plt.legend()
plt.grid(True)
plt.show()
ok,我们对数据进行了LDA降维,使得类内散度最小,类间散度最大,便于进行分类