只使用numpy如何实现Fisher线性判别?
时间: 2023-03-25 17:02:23 浏览: 141
可以使用numpy中的线性代数函数和统计函数来实现Fisher线性判别。具体步骤包括:
1. 计算每个类别的均值向量和总体均值向量。
2. 计算类内散度矩阵和类间散度矩阵。
3. 计算Fisher判别准则函数的系数。
4. 对新样本进行分类。
具体实现可以参考以下代码:
import numpy as np
def fisher_lda(X, y):
# 计算每个类别的均值向量和总体均值向量
class_mean = []
overall_mean = np.mean(X, axis=)
for i in np.unique(y):
class_mean.append(np.mean(X[y==i], axis=))
# 计算类内散度矩阵和类间散度矩阵
S_w = np.zeros((X.shape[1], X.shape[1]))
S_b = np.zeros((X.shape[1], X.shape[1]))
for i, mean_vec in enumerate(class_mean):
class_scatter = np.zeros((X.shape[1], X.shape[1]))
for row in X[y == i]:
row, mean_vec = row.reshape(X.shape[1], 1), mean_vec.reshape(X.shape[1], 1)
class_scatter += (row - mean_vec).dot((row - mean_vec).T)
S_w += class_scatter
overall_mean = overall_mean.reshape(X.shape[1], 1)
mean_vec = mean_vec.reshape(X.shape[1], 1)
S_b += (mean_vec - overall_mean).dot((mean_vec - overall_mean).T)
# 计算Fisher判别准则函数的系数
eig_vals, eig_vecs = np.linalg.eig(np.linalg.inv(S_w).dot(S_b))
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]
eig_pairs = sorted(eig_pairs, key=lambda k: k[], reverse=True)
w = np.hstack((eig_pairs[][1].reshape(X.shape[1], 1), eig_pairs[1][1].reshape(X.shape[1], 1)))
# 对新样本进行分类
return X.dot(w)
# 示例
X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]])
y = np.array([, , , 1, 1, 1])
fisher_lda(X, y)
阅读全文