%2类总体数据,每行为样品
clc
clear all
A1=xlsread('face.xlsx','A2:F268');
%A1=sortrows(A,column); %按照分类排序
[row,column]=size(A1); % 行列
class=A1(:,column);
classcount = histcounts(class); % 统计分类的样本数,注意是分列展示
[~,classnum]=size(classcount);
G1=A1(find(class==1),1:column-1);
G2=A1(find(class==2),1:column-1);
G3=A1(find(class==3),1:column-1);
G4=A1(find(class==4),1:column-1);
G5=A1(find(class==5),1:column-1);
G6=A1(find(class==6),1:column-1);
G7=A1(find(class==7),1:column-1);
G8=A1(find(class==8),1:column-1);
G9=A1(find(class==9),1:column-1);
G10=A1(find(class==10),1:column-1);
G11=A1(find(class==11),1:column-1);
G12=A1(find(class==12),1:column-1);
x=xlsread('predface.xlsx','B2:F3216'); %待判样品数据
[xr,xc]=size(x);
freedom=0.95; %自由度
m1=mean(G1); m2=mean(G2); m3=mean(G3);m4=mean(G4);
m5=mean(G5); m6=mean(G6); m7=mean(G7);m8=mean(G8);
m9=mean(G9); m10=mean(G10); m11=mean(G11);m12=mean(G12);
%m4=mean(G4);
n1=size(G1,1); %总体G1的样本数
n2=size(G2,1); %总体G2的样本数
n3=size(G3,1); %总体G3的样本数
n4=size(G4,1); %总体G4的样本数
n5=size(G5,1); %总体G1的样本数
n6=size(G6,1); %总体G2的样本数
n7=size(G7,1); %总体G3的样本数
n8=size(G8,1); %总体G4的样本数
n9=size(G9,1); %总体G1的样本数
n10=size(G10,1); %总体G2的样本数
n11=size(G11,1); %总体G3的样本数
n12=size(G12,1); %总体G4的样本数
%n=n1+n2; %两个总体合并的样本数
p=column-1; %p为变量个数
k=classnum;%类别总数
d1=0;
f=p*(p+1)*(classnum-1)/2; %自由区间
for i=1 : classnum
d1=d1+1/(classcount(i)-1);
end
d=(2*p^2+3*p-1)*(d1-1/(row-k))/(6*(p+1)*(k-1));
s1=cov(G1); s2=cov(G2); s3=cov(G3);s4=cov(G4);
s5=cov(G5); s6=cov(G6); s7=cov(G7);s8=cov(G8);
s9=cov(G9); s10=cov(G10); s11=cov(G11);s12=cov(G12);
s=((n1-1)*s1+(n2-1)*s2+(n3-1)*s3+(n4-1)*s4+(n5-1)*s5+(n6-1)*s6+(n7-1)*s7+(n8-1)*s8+(n9-1)*s9+(n10-1)*s10+(n11-1)*s11+(n12-1)*s12)/(row-classnum); %联合协方差矩阵
M=(row-k)*log(det(s))-((n1-1)*log(det(s1))+(n2-1)*log(det(s2))+(n3-1)*log(det(s3))+(n4-1)*log(det(s4))+(n5-1)*log(det(s5))+(n6-1)*log(det(s6))+(n7-1)*log(det(s7))+(n8-1)*log(det(s8))+(n9-1)*log(det(s9))+(n10-1)*log(det(s10))+(n11-1)*log(det(s11))+(n12-1)*log(det(s12)));
T=(1-d)*M; %计算统计量T观测值
%if T<chi2inv(freedom,f)
disp('所有数据协方差相等');
nright=0; %统计预测正确的数量
for i=1 : xr
w(1)=m1*inv(s)*x(i,:)'-1/2*m1*inv(s)*m1'+log(classcount(1)/row);
w(2)=m2*inv(s)*x(i,:)'-1/2*m2*inv(s)*m2'+log(classcount(2)/row);
w(3)=m3*inv(s)*x(i,:)'-1/2*m3*inv(s)*m3'+log(classcount(3)/row);
w(4)=m4*inv(s)*x(i,:)'-1/2*m4*inv(s)*m4'+log(classcount(4)/row);
w(5)=m5*inv(s)*x(i,:)'-1/2*m5*inv(s)*m5'+log(classcount(5)/row);
w(6)=m6*inv(s)*x(i,:)'-1/2*m6*inv(s)*m6'+log(classcount(6)/row);
w(7)=m7*inv(s)*x(i,:)'-1/2*m7*inv(s)*m7'+log(classcount(7)/row);
w(8)=m8*inv(s)*x(i,:)'-1/2*m8*inv(s)*m8'+log(classcount(8)/row);
w(9)=m9*inv(s)*x(i,:)'-1/2*m9*inv(s)*m9'+log(classcount(9)/row);
w(10)=m10*inv(s)*x(i,:)'-1/2*m10*inv(s)*m10'+log(classcount(10)/row);
w(11)=m11*inv(s)*x(i,:)'-1/2*m11*inv(s)*m11'+log(classcount(11)/row);
w(12)=m12*inv(s)*x(i,:)'-1/2*m12*inv(s)*m12'+log(classcount(12)/row);
for j=1:classnum %计算样品xi的判别函数W1(xi)和W2(xi)
if w(j)==max(w)
pred(i,1)=j; %保留预测结果
disp(['第',num2str(i),'个样品属于第',num2str(j),'类']); %归1类
% if j== class(i)
% nright=nright+1; % 统计正确率
% end
end
end
end
disp(['正确率为',num2str(nright/xr*100)]);
%%%%%%%%%%%%计算误判率
% n11=0; n22=0;n33=0; %初始误判率
% for i=1:n1 %计算G1总体样本线性判别函数结果
% w1(i,1)=m1*inv(s)*G1(i,:)'-1/2*m1*inv(s)*m1'+log(classcount(1)/row);
% w1(i,2)=m2*inv(s)*G1(i,:)'-1/2*m2*inv(s)*m2'+log(classcount(2)/row);
% w1(i,3)=m3*inv(s)*G1(i,:)'-1/2*m3*inv(s)*m3'+log(classcount(3)/row);
% for j=1:3 %计算G1总体中误判个数
% if w1(i,j)==max(w1(i,:))&j~=1
% n11=n11+1;
% end
% end
% end
%
% for i=1:n2
% w2(i,1)=m1*inv(s)*G2(i,:)'-1/2*m1*inv(s)*m1'+log(classcount(1)/row);
% w2(i,2)=m2*inv(s)*G2(i,:)'-1/2*m2*inv(s)*m2'+log(classcount(2)/row);
% w2(i,3)=m3*inv(s)*G2(i,:)'-1/2*m3*inv(s)*m3'+log(classcount(1)/row);
% for j=1:3
% if w2(i,j)==max(w2(i,:))&j~=2
% n22=n22+1;
% end %计算G2总体中误判个数
% end
% end
% for i=1:n3 %计算G3总体样本线性判别函数结果
% w3(i,1)=m1*inv(s)*G3(i,:)'-1/2*m1*inv(s)*m1'+log(classcount(1)/row);
% w3(i,2)=m2*inv(s)*G3(i,:)'-1/2*m2*inv(s)*m2'+log(classcount(2)/row);
% w3(i,3)=m3*inv(s)*G3(i,:)'-1/2*m3*inv(s)*m3'+log(classcount(3)/row);
% for j=1:3
% if w3(i,j)==max(w3(i,:))&j~=3
% n33=n33+1;
% end
% end
% end
% p00=(n11+n22+n33)/(n1+n2+n3)*100;
% disp(['正确率为',num2str(100-p00)]);
% else %采用马氏距离计算
% disp('所有数据协方差不全相等');
%
% for i=1:xr %4个样品Bayes判别函数
% w(1)=mahal(x(i,:),G1)+log(det(s1))-2*log(classcount(1)/row);
% w(2)=mahal(x(i,:),G2)+log(det(s2))-2*log(classcount(2)/row);
% w(3)=mahal(x(i,:),G3)+log(det(s3))-2*log(classcount(3)/row);
% % w(4)=mahal(x(i,:),G4)+log(det(s4))-2*log(classcount(4)/row);
% for j=1:classnum %计算样品xi的判别函数W1(xi)和W2(xi)
% if w(j)==min(w)
% disp(['第',num2str(i),'个样品属于第',num2str(j),'类']); %归1类
%
% end;
% end;
% end;
%
% %%%%%%%%%%%%%计算误判率
% n11=0; n22=0;n33=0; %初始误判率
% for i=1:n1 %计算G1总体样本线性判别函数结果
% w1(i,1)=mahal(x(i,:),G1)+log(det(s1))-2*log(classcount(1)/row);
% w1(i,2)=mahal(x(i,:),G2)+log(det(s2))-2*log(classcount(2)/row);
% w1(i,3)=mahal(x(i,:),G3)+log(det(s3))-2*log(classcount(3)/row);
% for j=1:3 %计算G1总体中误判个数
% if w1(i,j)==max(w1(i,:))&j~=1
% n11=n11+1;
% end
% end
% end
% for i=1:n2
% w2(i,1)=mahal(x(i,:),G1)+log(det(s1))-2*log(classcount(1)/row);
% w2(i,2)=mahal(x(i,:),G2)+log(det(s2))-2*log(classcount(2)/row);
% w2(i,3)=mahal(x(i,:),G3)+log(det(s3))-2*log(classcount(3)/row);
% for j=1:3
% if w2(i,j)==max(w2(i,:))&j~=2
% n22=n22+1;
% end %计算G2总体中误判个数
% end
% end
% for i=1:n3 %计算G3总体样本线性判别函数结果
% w3(i,1)=mahal(x(i,:),G1)+log(det(s1))-2*log(classcount(1)/row);
% w3(i,2)=mahal(x(i,:),G2)+log(det(s2))-2*log(classcount(2)/row);
% w3(i,3)=mahal(x(i,:),G3)+log(det(s3))-2*log(classcount(3)/row);
% for j=1:3
% if w3(i,j)==max(w3(i,:))&j~=3
% n33=n33+1;
% end
% end
% end
% p00=(n11+n22+n33)/(n1+n2+n3); %计算回代误判率
% disp(['误判率为',num2str(p00)]);
%end;
评论0