⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irisfisher.m

📁 利用经典的iris数据进行核fisher分类
💻 M
字号:
function y=fisher(train,test,n,k)
%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%
%load d:\iris\train.txt;
switch k
    case 1
        a=train;
    case 2
        a=train(26:50,:);
        a(26:50,:)=train(1:25,:);
        a(51:75)=train(51:75);
    case 3
        a=train(51:75,:);
        a(26:75,:)=train(1:50,:);
end

m=mean(a(1:25,1:n));
m(2,:)=mean(a(26:75,1:n));
sw=zeros(n,n);
for i=1:25
    sw=sw+(a(i,1:n)-m(1,:))'*(a(i,1:n)-m(1,:));
end
for i=26:75
    sw=sw+(a(i,1:n)-m(2,:))'*(a(i,1:n)-m(2,:));
end
w=inv(sw)*(m(1,:)-m(2,:))';
result=a(:,1:n)*w;
%theta 取两个类别均值在w方向投影的简单均值
theta=w'*(m(1,:)+m(2,:))'/2;
for i=1:75
    result(i,2)=theta;
end
result(:,3)=train(:,6);
%theta 考虑样本数的两个类别均值在w方向投影的平均
theta(2)=w'*(25*m(1,:)+50*m(2,:))'/75;
theta(3)=w'*(50*m(1,:)+25*m(2,:))'/75;

s1=std(result(1:25,1));
s1(2)=std(result(26:75,1));
s1(3)=std(result(:,1))
%theta 考虑样本方差的两个类别均值在w方向投影的平均
theta(4)=w'*(s1(1)*m(1,:)+s1(2)*m(2,:))'/(s1(3));
theta(5)=w'*(s1(2)*m(1,:)+s1(1)*m(2,:))'/(s1(3));
theta(6)=w'*((25*m(1,:)+50*m(2,:))'/75+(s1(1)*m(1,:)+s1(2)*m(2,:))'/(s1(3)))/2;
theta(7)=w'*((50*m(1,:)+25*m(2,:))'/75+(s1(2)*m(1,:)+s1(1)*m(2,:))'/(s1(3)))/2;

k=0;
for i=1:25
    if result(i,1)<result(i,2)
        k=k+1;
        fprintf('%g %g %g\n',result(i,1),result(i,2),result(i,3));
    end
end
for i=26:75
    if result(i,1)>result(i,2)
        k=k+1;
        fprintf('%g %g %g\n',result(i,1),result(i,2),result(i,3));
    end
end
k

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -