資源簡介
使用Matlab實現,包括一維特征最小錯誤率bayes分類器;二維特征最小錯誤率bayes分類器;二維特征最小風險bayes分類器以及使用的數據集合。

代碼片段和文件信息
%%%Bayes?Classifier
%%%貝葉斯分類器根據二維特征(身高和體重)
%%%date:2011-6-5
clear?all
%訓練數據
[mstature(:1)mstature(:2)]=textread(‘male.txt‘‘%f%f‘‘headerlines‘0);
[fstature(:1)fstature(:2)]=textread(‘female.txt‘‘%f%f‘‘headerlines‘0);
[row1col1]=size(mstature);
[row2col2]=size(fstature);
%測試數據
[test1(:1)test1(:2)test1(:3)]=textread(‘test1.txt‘‘%f%f%d‘‘headerlines‘0);
[test2(:1)test2(:2)test2(:3)]=textread(‘test2.txt‘‘%f%f%d‘‘headerlines‘0);
[test1_row?test1_col]=size(test1);
[test2_row?test2_col]=size(test2);
%畫出測試樣本數據的二維直角坐標圖
figure(1)
for?i=1:test1_row
????if(test1(i3)==1)
????????plot(test1(i1)test1(i2)‘bx‘);
????elseif(test1(i3)==-1)
????????plot(test1(i1)test1(i2)‘g+‘);
????end
????hold?on;
end
for?i=1:test2_row
????if(test2(i3)==1)
????????plot(test2(i1)test2(i2)‘bx‘);
????elseif(test2(i3)==-1)
????????plot(test2(i1)test2(i2)‘g+‘);
????end
????hold?on;
end
%%最大似然法(正態分布假設)(以身高和體重為特征)
%male
m1=mean(mstature);
d1=cov(mstature);
%female
m2=mean(fstature);
d2=cov(fstature);
%初始化風險函數
r11=0;
r12=1;
r21=1;
r22=0;
%對測試樣本test1和test2分類
right=0;%分類正確次數
wrong=0;%分類錯誤次數
%初始化先驗概率
pw1=0.5;??%0.5??0.75????0.9
pw2=0.5;??%0.5??0.25????0.1
LR=((r12-r22)/(r21-r11))*(pw2/pw1)
%測試數據集1
fprintf(‘******************************************\n‘);
fprintf(‘測試數據集test1\n‘);
fprintf(‘******************************************\n‘);
for?i=1:test1_row
????px1=mvnpdf([test1(i1)test1(i2)]m1d1);
????px2=mvnpdf([test1(i1)test1(i2)]m2d2);
????
????
????if?(px1/px2)>=LR
????????fprintf(‘測試數據(%f%f)屬于w1類(男)‘test1(i1)test1(i2));
????????plot(test1(i1)test1(i2)‘rs‘);?
????????if(test1(i3)==1)
????????????right=right+1;
????????????fprintf(‘??正確\n‘);
????????else
????????????wrong=wrong+1;
????????????fprintf(‘??錯誤\n‘);
????????end
????else
????????fprintf(‘測試數據(%f%f)屬于w2類(女)‘test1(i1)test1(i2));
????????plot(test1(i1)test1(i2)‘ko‘);?
????????if(test1(i3)==-1)
????????????right=right+1;
????????????fprintf(‘??正確\n‘);
????????else
????????????wrong=wrong+1;
????????????fprintf(‘??錯誤\n‘);
????????end
????end
end
????????
%測試數據集2
fprintf(‘******************************************\n‘);
fprintf(‘測試數據集test2\n‘);
fprintf(‘******************************************\n‘);
for?i=1:test2_row
????px1=mvnpdf([test2(i1)test2(i2)]m1d1);
????px2=mvnpdf([test2(i1)test2(i2)]m2d2);????
????????
????if?(px1/px2)>=LR
????????fprintf(‘測試數據(%f%f)屬于w1類(男)‘test2(i1)test2(i2));
????????plot(test2(i1)test2(i2)‘rs‘);?
????????if(test2(i3)==1)
????????????right=right+1;
????????????fprintf(‘??正確\n‘);
????????else
????????????wrong=wrong+1;
????????????fprintf(‘??錯誤\n‘);
????????end
????else
????????fprintf(‘測試數據(%f%f)屬于w2類(女)‘test2(i1)test2(i2));
????????plot(test2(i1)test2(i2)‘ko‘);?
????????if(test2(i3)==-1)
????????????right=right+1;
????????????fprintf(‘??正確\n‘);
????????else
????????????wrong=wro
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3386??2011-10-14?00:11??Bayes\bayes.m
?????文件???????3450??2011-10-14?00:12??Bayes\bayes1D.m
?????文件???????3440??2011-10-14?00:12??Bayes\bayes2D.m
?????文件????????426??2004-10-08?09:03??Bayes\FEMALE.TXT
?????文件????????424??2004-10-08?09:04??Bayes\MALE.TXT
?????文件????????350??2004-10-08?09:03??Bayes\test1.txt
?????文件???????3020??2004-10-08?09:10??Bayes\test2.txt
?????目錄??????????0??2011-10-14?00:17??Bayes
-----------?---------??----------?-----??----
????????????????14496????????????????????8
- 上一篇:TCT算法功率譜
- 下一篇:EMD matlab實現源碼
評論
共有 條評論