以下是我的Bayes分类器设计的思路,程序原理,运行结果:
设计思路:
基于最小错误率的Bayes分类器
基于最小错误率的Bayes公式
基于最小风险的Bayes分类器
基于最小风险的Bayes公式
程序原理:
最小错误率的多二维特征训练函数
if isempty(get(handles.MaleError_edit,'String')) || isempty(get(handles.FemaleError_edit,'String'))
errordlg('Please input the parameter of Male and Female!','Parameter Error');
return;
end
%获取先验概率
ErrorHeightButton_Status = get(handles.ErrorHeight_radiobutton,'Value');
ErrorWeightButton_Status = get(handles.ErrorWeight_radiobutton,'Value');
if isequal(ErrorHeightButton_Status,0) & isequal(ErrorWeightButton_Status,0)
errordlg('Please Select the option for Training!','Parameter Error');
return;
else
%根据所选择的特征来进行训练
if isequal(ErrorHeightButton_Status,1)
if isequal(ErrorWeightButton_Status,0)
ErrorTrainParameter = 1;
else
ErrorTrainParameter = 3;
end
else
ErrorTrainParameter = 2;
end
end
% Read Files
counter = 1;
while feof(testfile) == 0
%get the data by lines
str = fgetl(testfile);
[str1 str2 str3] = strread(str,'%s %s %s','delimiter',' ');
Test_File(counter) = [str1 str2 str3];
counter = counter + 1;
end
% %get the information of the trained samples
female = handles.female;
male = handles.male;
%test as the matrix of the testfile
test = dlmread(Filename);
[length, width] = size(test);
male_parameter = str2num(get(handles.MaleError_edit,'String'));
female_parameter = str2num(get(handles.FemaleError_edit,'String'));
error1 = 0;
error2 = 0;
%get the mu and sigma of normal distribution
if isequal(ErrorTrainParameter,1)
%Trained by the Parameter of Height
%正态分布估计值函数normfit
[favg, fsig] = normfit(female(:,1));
[mavg, msig] = normfit(male(:,1));
for i=1:length
%正态分布概率值函数normpdf,按照先前的最小错误率公式计算后验概率
Px=normpdf(test(i,1),favg,fsig) * female_parameter + normpdf(test(i,1),mavg,msig) * male_parameter;
P_male=normpdf(test(i,1),mavg,msig) * male_parameter / Px;
P_female=normpdf(test(i,1),favg,fsig) * female_parameter / Px;
if P_male > P_female
if test(i,3) == 2
%错误率计算
error1 = error1 + 1;
test(i,3) = 21;
end
else
if test(i,3) == 1
%错误率计算
error2 = error2 + 1;
test(i,3) = 12;
end
end
end
else
if isequal(ErrorTrainParameter,2)
%Trained by the Parameter of Weight
[favg, fsig] = normfit(female(:,2));
[mavg, msig] = normfit(male(:,2));
[length, width] = size(test);
for i = 1:length
Px = normpdf(test(i,2),favg,fsig) * female_parameter + normpdf(test(i,2),mavg,msig) * male_parameter;
P_male = normpdf(test(i,2),mavg,msig) * male_parameter / Px;
P_female = normpdf(test(i,2),favg,fsig) * female_parameter / Px;
if P_male > P_female
if test(i,3) == 2
error1 = error1 + 1;
test(i,3) = 21;
end
else
if test(i,3) == 1
error2 = error2 + 1;
test(i,3) = 12;
end
end
end
else
if isequal(ErrorTrainParameter,3)
%Trained by the Parameter of Height and Weight
female = female(:,1:2);
male = male(:,1:2);
favg = mean(female);
fsig = cov(female);
mavg = mean(male);
msig = cov(male);
if isequal(get(handles.Irrelated_radiobutton,'Value'),1)
msig(1,2) = 0;
msig(2,1) = 0;
fsig(1,2) = 0;
fsig(2,1) = 0;
end
for i = 1:length
Px = mvnpdf(test(i,1:2),favg,fsig)*female_parameter + mvnpdf(test(i,1:2),mavg,msig)*male_parameter;
P_male = mvnpdf(test(i,1:2),mavg,msig) * male_parameter / Px;
P_female = mvnpdf(test(i,1:2),favg,fsig) * female_parameter / Px;
if P_male > P_female
if test(i,3) == 2
error1 = error1 + 1;
test(i,3) = 21;
end
else
if test(i,3) == 1
error2 = error2 + 1;
test(i,3) = 12;
end
end
end
end
end
end
%平均错误率计算
errorratio = (error1+error2)/length;
set(handles.filename_text,'string',Filename);
set(handles.ErrorResult_text,'string',num2str(errorratio));
Result = mat2cell(test(:,3),length);
set(handles.Test_edit,'string',Test_File);
set(handles.Result_edit,'string',Result);
handles.Result_edit = handles.Test_edit;
fclose(testfile);
guidata(hObject,handles);
基于最小风险的多维特征训练函数
if isempty(get(handles.MaleRisk_edit,'String')) || isempty(get(handles.FemaleRisk_edit,'String'))
errordlg('Please input the parameter of Male and Female!','Parameter Error');
return;
end
%读取先验概率
RiskHeightButton_Status = get(handles.RiskHeight_radiobutton,'Value');
RiskWeightButton_Status = get(handles.RiskWeight_radiobutton,'Value');
if isequal(RiskHeightButton_Status,0) & isequal(RiskWeightButton_Status,0)
errordlg('Please Select the option for Training!','Parameter Error');
return;
else
%多维特征的选择
if isequal(RiskHeightButton_Status,1)
if isequal(RiskWeightButton_Status,0)
RiskTrainParameter = 1;
else
RiskTrainParameter = 3;
end
else
RiskTrainParameter = 2;
end
end
female = handles.female;
male = handles.male;
test = dlmread(Filename);
%设置风险矩阵的初始值
Decision = [0 5;1 0];
[length, width] = size(test);
male_parameter = str2num(get(handles.MaleRisk_edit,'String'));
female_parameter = str2num(get(handles.FemaleRisk_edit,'String'));
error1 = 0;
error2 = 0;
% get the parameters of the normal distribution
if isequal(RiskTrainParameter,1)
% Trained by the parameter of Height
[favg, fsig] = normfit(female(:,1));
[mavg, msig] = normfit(male(:,1));
for i = 1:length
%按照最小风险的公式计算
Px = normpdf(test(i,1),favg,fsig) * female_parameter + normpdf(test(i,1),mavg,msig) * male_parameter;
P_male = normpdf(test(i,1),mavg,msig) * male_parameter / Px;
P_female = normpdf(test(i,1),favg,fsig) * female_parameter / Px;
Pisk_male = Decision(1,1) * P_male + Decision(1,2) * P_female;
Pisk_female = Decision(2,1) * P_male + Decision(2,2) * P_female;
if Pisk_male > Pisk_female
if test(i,3) == 1
%错误率计算
error1 = error1 + 1;
test(i,3) = 12;
end
else
if test(i,3) == 2
%错误率计算
error2 = error2 + 1;
test(i,3) = 21;
end
end
end
else
if isequal(RiskTrainParameter,2)
% Trained by the parameter of Weight
[favg, fsig] = normfit(female(:,2));
[mavg, msig] = normfit(male(:,2));
for i = 1:length
Px = normpdf(test(i,2),favg,fsig) * female_parameter + normpdf(test(i,2),mavg,msig) * male_parameter;
P_male = normpdf(test(i,2),mavg,msig) * male_parameter / Px;
P_female = normpdf(test(i,2),favg,fsig) * female_parameter / Px;
Pisk_male = Decision(1,1)*P_male+Decision(1,2) * P_female;
Pisk_female = Decision(2,1)*P_male+Decision(2,2) * P_female;
if Pisk_male > Pisk_female
if test(i,3) == 1
error1 = error1 + 1;
test(i,3) = 12;
end
else
if test(i,3)==2
error2 = error2 + 1;
test(i,3) = 21;
end
end
end
else
if isequal(RiskTrainParameter,3)
% Trained by the parameter of Height and Weight
female = female(:,1:2);
male = male(:,1:2);
favg = mean(female);
fsig = cov(female);
mavg = mean(male);
msig = cov(male);
if isequal(get(handles.Irrelated_radiobutton,'Value'),1)
msig(1,2) = 0;
msig(2,1) = 0;
fsig(1,2) = 0;
fsig(2,1) = 0;
end
for i = 1:length
Px = mvnpdf(test(i,1:2),favg,fsig) * female_parameter + mvnpdf(test(i,1:2),mavg,msig) * male_parameter;
P_male = mvnpdf(test(i,1:2),mavg,msig) * male_parameter / Px;
P_female = mvnpdf(test(i,1:2),favg,fsig) * female_parameter / Px;
Risk_male = Decision(1,1)*P_male+Decision(1,2) * P_female;
Risk_female = Decision(2,1)*P_male+Decision(2,2) * P_female;
if Risk_male > Risk_female
if test(i,3) == 1
error1 = error1 + 1;
test(i,3) = 12;
end
else
if test(i,3) == 2
error2 = error2 + 1;
test(i,3) = 21;
end
end
end
end
end
end
%平均错误率计算
errorratio = (error1+error2)/length;
set(handles.filename_text,'string',Filename);
set(handles.ErrorResult_text,'string',num2str(errorratio));
set(handles.Test_edit,'string',Test_File);
Result = mat2cell(test(:,3),length);
set(handles.Result_edit,'string',Result);
handles.Result_edit = handles.Test_edit;
fclose(testfile);
guidata(hObject,handles);
运行结果:
基于最小错误率的结果
表4.1条件:相关,训练样本为Test1.txt。
男女概率 身高 体重 两者
0.9 Vs 0.1 0.11429 0.2 0.11429
0.5 Vs 0.5 0.057143 0.057143 0.028571
0.75 Vs 0.25 0.085714 0.057143 0.085714
表4.2条件:相关,训练样本为Test2.txt。
男女概率 身高 体重 两者
0.9 Vs 0.1 0.056667 0.086667 0.056667
0.5 Vs 0.5 0.09 0.14667 0.10667
0.75 Vs 0.25 0.066667 0.11 0.046667
表4.3条件:不相关,训练样本为Test1.txt。
男女概率 身高 体重 两者
0.9 Vs 0.1 0.11429 0.2 0.085714
0.5 Vs 0.5 0.057143 0.057143 0.028571
0.75 Vs 0.25 0.085714 0.057143 0.028571
表4.4条件:不相关,训练样本为Test2.txt。
男女概率 身高 体重 两者
0.9 Vs 0.1 0.056667 0.086667 0.053333
0.5 Vs 0.5 0.09 0.14667 0.096667
0.75 Vs 0.25 0.066667 0.11 0.063333
基于最小风险的结果
表4.5条件:相关,训练样本为Test1.txt
男女概率 身高 体重 两者
0.9 Vs 0.1 0.085714 0.028571 0.028571
0.5 Vs 0.5 0.14286 0.17143 0.14286
0.75 Vs 0.25 0.085714 0.11429 0.028571
表4.6条件:相关,训练样本为Test2.txt
男女概率 身高 体重 两者
0.9 Vs 0.1 0.063333 0.12 0.08
0.5 Vs 0.5 0.34667 0.29667 0.26333
0.75 Vs 0.25 0.10667 0.22333 0.14333
表4.7条件:不相关,训练样本为Test1.txt
男女概率 身高 体重 两者
0.9 Vs 0.1 0.085714 0.028571 0.028571
0.5 Vs 0.5 0.14286 0.17143 0.16333
0.75 Vs 0.25 0.085714 0.11429 0
表4.8条件:不相关,训练样本为Test2.txt
男女概率 身高 体重 两者
0.9 Vs 0.1 0.063333 0.12 0.076667
0.5 Vs 0.5 0.34667 0.29667 0.16333
0.75 Vs 0.25 0.10667 0.22333 0.11667
总结:
个人的第一次matlab程序,界面设计上还有诸多问题,有些显示不正常,对matlab较为生疏,而作为如今在 模拟仿真方面有绝对优势的matlab,日后必然和我的编程有诸多相关,需要尽快学起。
对于本次编程,老师也说过现在给我们做的一些程序算法都是最基础的,强度还是较低,自己在算法理解和函数调用方面感觉有较大的缺陷,对于matlab的函数包括基本语法还不熟练,种种不完善的地方还有待日后加强。
而本次程序的结果分析报告则已连通程序等内容一同上交,这里的文字只是作为个人总结所用。
尽力而为,做好每次的matlab编程。
抱歉各位,格式对齐我已经设置过,但是没有找到正确的方法,故而委屈各位