step1:估计分类样本的各个属性的概率分布
step2:估计先验概率
step3:估计属于该类别的概率并取最大值
这里以正态分布为例
clc;clear;
% 风险表
f=ones(4,4);
% 读数据
X=xlsread('数据.xls');
x=X(1:15,2:end);
x_test=X(16:end,2:4);
x1=x(find(x(:,4)==1),1:3);
[n1,~]=size(x1);
x2=x(find(x(:,4)==2),1:3);
[n2,~]=size(x2);
x3=x(find(x(:,4)==3),1:3);
[n3,~]=size(x3);
x4=x(find(x(:,4)==4),1:3);
[n4,~]=size(x4);
mu1=mean(x1);
mu2=mean(x2);
mu3=mean(x3);
mu4=mean(x4);
sig1=cov(x1);
sig2=cov(x2);
sig3=cov(x3);
sig4=cov(x4);
d1=det(sig1);
d2=det(sig2);
d3=det(sig3);
d4=det(sig4);
s1_=inv(sig1);
s2_=inv(sig2);
s3_=inv(sig3);
s4_=inv(sig4);
% 正态分布
for i=1:44
p1(i)=1/sqrt(4*pi*pi*d1)*exp(-0.5*(x_test(i,:)-mu1)*s1_*(x_test(i,:)-mu1)');
p2(i)=1/sqrt(4*pi*pi*d2)*exp(-0.5*(x_test(i,:)-mu2)*s2_*(x_test(i,:)-mu2)');
p3(i)=1/sqrt(4*pi*pi*d3)*exp(-0.5*(x_test(i,:)-mu3)*s3_*(x_test(i,:)-mu3)');
p4(i)=1/sqrt(4*pi*pi*d4)*exp(-0.5*(x_test(i,:)-mu4)*s4_*(x_test(i,:)-mu4)');
[~,index]=max([n1*p1(i),n2*p2(i),n3*p3(i),n4*p4(i)]./15);
P(i)=index;
p(i,:)=[n1*p1(i),n2*p2(i),n3*p3(i),n4*p4(i)]./15;
end
即乘以对应的风险表算出各自的风险,取最小值即可
clc;clear;
% 风险表
f=ones(4,4);
% 读数据
X=xlsread('数据.xls');
x=X(1:15,2:end);
x_test=X(16:end,2:4);
x1=x(find(x(:,4)==1),1:3);
[n1,~]=size(x1);
x2=x(find(x(:,4)==2),1:3);
[n2,~]=size(x2);
x3=x(find(x(:,4)==3),1:3);
[n3,~]=size(x3);
x4=x(find(x(:,4)==4),1:3);
[n4,~]=size(x4);
mu1=mean(x1);
mu2=mean(x2);
mu3=mean(x3);
mu4=mean(x4);
sig1=cov(x1);
sig2=cov(x2);
sig3=cov(x3);
sig4=cov(x4);
d1=det(sig1);
d2=det(sig2);
d3=det(sig3);
d4=det(sig4);
s1_=inv(sig1);
s2_=inv(sig2);
s3_=inv(sig3);
s4_=inv(sig4);
% 正态分布
for i=1:44
p1(i)=1/sqrt(4*pi*pi*d1)*exp(-0.5*(x_test(i,:)-mu1)*s1_*(x_test(i,:)-mu1)');
p2(i)=1/sqrt(4*pi*pi*d2)*exp(-0.5*(x_test(i,:)-mu2)*s2_*(x_test(i,:)-mu2)');
p3(i)=1/sqrt(4*pi*pi*d3)*exp(-0.5*(x_test(i,:)-mu3)*s3_*(x_test(i,:)-mu3)');
p4(i)=1/sqrt(4*pi*pi*d4)*exp(-0.5*(x_test(i,:)-mu4)*s4_*(x_test(i,:)-mu4)');
[~,index]=max([n1*p1(i),n2*p2(i),n3*p3(i),n4*p4(i)]./15);
P(i)=index;
p(i,:)=[n1*p1(i),n2*p2(i),n3*p3(i),n4*p4(i)]./15;
for j=1:4
l(j)=sum(p(i,j).*f(:,j));
end
k(i)=find(l==min(l));
end