线性判别分析(fisher)
线性判别分析
线性判别分析中有降维,把数据都投影到同一条线上,然后在直线上取一个阈值,将直线分成两条射线,每一条代表一个分类。会损失一些数据信息,但如果这些信息是一些干扰信息,丢失也未尝不是好事。
线性判别分析之后的结果是一个向量,其他的不行吗?
主要指导思想(目标):类内小,类间大。
公式推导
我们得到的是向量,为了方便计算损失,不妨设
两个不同类别分别命名为
类间:
类内:
目标函数:
设
此时
求导
令导数等于零
求任意一个点的投影
求阈值
依赖
import numpy as np import pandas as pd import matplotlib.pyplot as plt
人工数据集
n = 100 X = np.random.multivariate_normal((1, 1), [[0.64, 0], [0, 0.64]], size = int(n/2)) X = np.insert(X, 50, np.random.multivariate_normal((3, 3), [[0.64, 0], [0,0.64]], size = int(n/2)),0) #X = np.insert(X, 0, 1, 1) m = X.shape[1] y = np.array([1]*50+[-1]*50).reshape(-1,1) plt.scatter(X[:50, -2], X[:50, -1]) plt.scatter(X[50:, -2], X[50:, -1], c = "#ff4400")
<matplotlib.collections.PathCollection at 0x7f2b50e680d0>
X1 = X[(y==1).reshape(-1)] X0 = X[(y==-1).reshape(-1)] n1 = np.array([[X1.shape[0]]]) n0 = np.array([[X0.shape[0]]]) mu1 = X1.mean(axis = 0).reshape(-1,1) mu0 = X0.mean(axis = 0).reshape(-1,1) Sigma1 = np.cov(X1.T) Sigma0 = np.cov(X0.T) theta = (Sigma1 + Sigma0) @ (mu1 - mu0) threshold = (n1*mu1 + n0*mu0).T@theta/(n1 + n0) def getForecast(x): return x.T @ theta threshold
array([[-10.45793931]])
预测
print(f'{ 1 if getForecast(np.array([[1],[1]])) > threshold else 0}')
1
分界展示
plt.scatter(X[:50, -2], X[:50, -1]) plt.scatter(X[50:, -2], X[50:, -1], c = "#ff4400") for i in np.arange(-1,5,0.02): for j in np.arange(-1,5,0.02): if abs(getForecast(np.array([[i],[j]])) - threshold) <0.01: plt.scatter(i,j,c="#000000")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异