标签:print image cat 线性 lang rand tps you marker
求解最佳投影方向,使得同类投影点尽可能的进,异类投影点尽可能的远
同类投影点距离用同类样本协方差矩阵表示
异类投影点距离
\(\mu_i \, {第i类样本均值}\)
优化函数
求上述函数极大值,解出\(\omega\)
则
利用拉格朗日乘数法,可得
import numpy as np
import matplotlib.pyplot as plt
M = 2 #属性个数
N = 50#二分类。每类样本N个
#随机生成两个属性的N个第一类样本
feature11 = np.random.randint(0, 7, size = N)
feature12 = np.random.randint(0, 7, size= N)
temp_X1 = np.row_stack((feature11, feature12))
X1 = np.mat(temp_X1)
#随机生成两个属性的N个第二类样本
feature21 = np.random.randint(5,11, size= N)
feature22 = np.random.randint(7, 14, size= N)
temp_X2 = np.row_stack((feature21, feature22))
X2 = np.mat(temp_X2)
#求投影向量omega
mu1 = np.mat(np.zeros((2,1)))
mu2 = np.mat(np.zeros((2,1)))
X_1t = np.array(X1)
X_2t = np.array(X2)
for i in range(M):
mu1[i, 0] = sum([j for j in X_1t[i,:]])/N
for i in range(M):
mu2[i, 0] = sum([j for j in X_2t[i,:]])/N
#print(mu1, mu2)
s_w1 = np.mat(np.zeros(M))
s_w2 = np.mat(np.zeros(M))
for i in range(N):
s_w1 = s_w1 + (X1[:, i] - mu1)*(X1[:, i] - mu1).T
for i in range(N):
s_w2 = s_w2 + (X2[:, i] - mu2)*(X2[:, i] - mu2).T
s_w = s_w1 + s_w2
Omega = np.linalg.pinv(s_w)*(mu1 - mu2)
#print(Omega)
#画出散点图、投影面
fig = plt.figure(1)
plt.scatter(feature11, feature12, marker=‘+‘)
plt.scatter(feature21, feature22, marker=‘*‘)
xx_1 = np.linspace(0,10,num=50)
yy_1 = Omega[1,0]/Omega[0,0]*xx_1
plt.plot(xx_1,yy_1,color=‘r‘)
plt.show()
《机器学习》 周志华老师
标签:print image cat 线性 lang rand tps you marker
原文地址:https://www.cnblogs.com/liudianfengmang/p/12822990.html