clc clear [a,b,c,d,e] = textread('H:\Example\Iris\iris.data','%f%f%f%f%s','delimiter',','); setosa = [a(1:50),b(1:50),c(1:50),d(1:50)]; versicolor = [a(51:100),b(51:100),c(51:100),d(51:100)]; clear a b c d %% �����������ֵ��Э������� p = 0.3; [m_c1,cov_c1 ] = bayes_train(setosa); [m_c2,cov_c2 ] = bayes_train(versicolor); %% ���� test_x = [versicolor(1:41,:);setosa(1:30,:)]; %% ������Լ��� P1 = post_prob(test_x,p,m_c1,cov_c1); P2 = post_prob(test_x,p,m_c2,cov_c2); threshold = P1 - P2; t1 = 0; t2 = 0; for k = 1:length(threshold) if threshold(k) > 0 t2 = t2 + 1; else t1 = t1 + 1; end end display(['��',num2str(t2),'��setosa����']) display(['��',num2str(t1),'��versicolor����']) 复原matlab代码
时间: 2023-09-10 19:05:53 浏览: 115
matlab中常用的函数简介
clc;
clear;
[a,b,c,d,e] = textread('H:\Example\Iris\iris.data','%f%f%f%f%s','delimiter',',');
setosa = [a(1:50),b(1:50),c(1:50),d(1:50)];
versicolor = [a(51:100),b(51:100),c(51:100),d(51:100)];
clear a b c d;
p = 0.3;
[m_c1,cov_c1] = bayes_train(setosa);
[m_c2,cov_c2] = bayes_train(versicolor);
test_x = [versicolor(1:41,:);setosa(1:30,:)];
P1 = post_prob(test_x,p,m_c1,cov_c1);
P2 = post_prob(test_x,p,m_c2,cov_c2);
threshold = P1 - P2;
t1 = 0;
t2 = 0;
for k = 1:length(threshold)
if threshold(k) > 0
t2 = t2 + 1;
else
t1 = t1 + 1;
end
end
display(['有',num2str(t2),'个setosa类']);
display(['有',num2str(t1),'个versicolor类']);
阅读全文