许多书籍使用下图说明了Fisher线性判别分析的概念(这个特别来自Pattern Recognition and Machine Learning,第188页)
我想知道如何用R(或任何其他语言)重现这个数字。下面粘贴的是我在R中的初始努力。我模拟两组数据并使用function multiply(first){
var value = +first.value;
var textboxes = document.getElementsByClassName("multiply-this");
for(var i = 0; i < textboxes.length; i++){
var textbox = textboxes[i];
if(textbox.attributes.initialValue){
textbox.value = textbox.attributes.initialValue.value;
} else {
textbox.setAttribute("initialValue", textbox.value);
}
textbox.value = +textbox.value * value;
}
}
window.onload = function(){
var textboxes = document.getElementsByClassName("multiply-this");
for(var i = 0; i < textboxes.length; i++){
var textbox = textboxes[i];
textbox.onkeyup = function(){
this.setAttribute("initialValue", this.value);
}
}
}
函数绘制线性判别式。欢迎任何建议。
textbox.onblur = function(){
multiply(document.getElementById("multiplyer"));
}
MY(UNFINISHED)工作
我在下面粘贴了我当前的解决方案。主要问题是如何根据决策边界旋转(和移动)密度图。任何建议仍然欢迎。
abline()
答案 0 :(得分:4)
基本上,您需要沿分类器的方向投影数据,为每个类绘制直方图,然后旋转直方图,使其x轴与分类器平行。为了获得良好的结果,需要对缩放直方图进行一些反复试验。这是一个如何在Matlab中实现的例子,用于天真的分类器(类的差异&#39;表示)。对于Fisher分类器,它当然是类似的,您只需使用不同的分类器w
。我更改了代码中的参数,因此绘图与您给出的绘图更相似。
rng('default')
n = 1000;
mu1 = [1,3]';
mu2 = [4,1]';
rho = 0.3;
s1 = .8;
s2 = .5;
Sigma = [s1^2,rho*s1*s1;rho*s1*s1, s2^2];
X1 = mvnrnd(mu1,Sigma,n);
X2 = mvnrnd(mu2,Sigma,n);
X = [X1; X2];
Y = [zeros(n,1);ones(n,1)];
scatter(X1(:,1), X1(:,2), [], 'b' );
hold on
scatter(X2(:,1), X2(:,2), [], 'r' );
axis equal
m1 = mean(X(1:n,:))';
m2 = mean(X(n+1:end,:))';
plot(m1(1),m1(2),'bx','markersize',18)
plot(m2(1),m2(2),'rx','markersize',18)
plot([m1(1),m2(1)], [m1(2),m2(2)],'g')
%% classifier taking only means into account
w = m2 - m1;
w = w / norm(w);
% project data onto w
X1_projected = X1 * w;
X2_projected = X2 * w;
% plot histogram and rotate it
angle = 180/pi * atan(w(2)/w(1));
[hy1, hx1] = hist(X1_projected);
[hy2, hx2] = hist(X2_projected);
hy1 = hy1 / sum(hy1); % normalize
hy2 = hy2 / sum(hy2); % normalize
scale = 4; % set manually
h1 = bar(hx1, scale*hy1,'b');
h2 = bar(hx2, scale*hy2,'r');
set([h1, h2],'ShowBaseLine','off')
% rotate around the origin
rotate(get(h1,'children'),[0,0,1], angle, [0,0,0])
rotate(get(h2,'children'),[0,0,1], angle, [0,0,0])