理论
代码实现
class LDA():
def __init__(self):
# 初始化权重矩阵
self.w = None
# 计算协方差矩阵
def calc_cov(self, X, Y=None):
m = X.shape[0]
# 数据标准化
X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)
Y = X if Y == None else (Y - np.mean(Y, axis=0)) / np.std(Y, axis=0)
return 1 / m * np.matmul(X.T, Y)
# 对数据进行投影
def project(self, X, y):
self.fit(X, y)
X_projection = X.dot(self.w)
return X_projection