A list,
Bayesian classifier is the general name of classifier based on Bayesian principle. It is a generative model, among which naive Bayesian classifier is the simplest one. To fully understand the principle of Bayesian classifier, you must first understand some basic concepts. 1 Basic concepts Prior probability: the probability of something happening based on statistics/experience, such as the probability of rain in Beijing, which can be obtained from previous experience or statistical results. Posteriori probability: The probability that something will happen under certain conditions, such as the probability that it will rain if there are black clouds in the sky in Beijing. Conditional probability: The probability that certain conditions will occur when an event occurs, such as the probability that clouds will appear when it rains (if) in Beijing.
Bayes’ formula
2. Simple Bayesian classifier
Ii. Source code
function varargout = Classification(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Classification_OpeningFcn, ...
'gui_OutputFcn', @Classification_OutputFcn, ...
'gui_LayoutFcn', [],...'gui_Callback'[]);if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Classification is made visible.
function Classification_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
%--------------------------------------------------------------------------
global flag
flag=0;
if exist('template.mat'.'file')~=0
load template pattern;
else
pattern(1.1).num=0;
pattern(1.1).feature=[];
pattern(1.2).num=0;
pattern(1.2).feature=[];
pattern(1.3).num=0;
pattern(1.3).feature=[];
pattern(1.4).num=0;
pattern(1.4).feature=[];
pattern(1.5).num=0;
pattern(1.5).feature=[];
pattern(1.6).num=0;
pattern(1.6).feature=[];
pattern(1.7).num=0;
pattern(1.7).feature=[];
pattern(1.8).num=0;
pattern(1.8).feature=[];
pattern(1.9).num=0;
pattern(1.9).feature=[];
pattern(1.10).num=0;
pattern(1.10).feature=[];
save template pattern;
end
%------------------------------------------------------------------------
% --- Outputs from this function are returned to the command line.
function varargout = Classification_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output; % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- % tablets implementation - press the left key began to draw a straight linefunction figure1_WindowButtonDownFcn(hObject, eventdata, handles)% CLC Global flag Global POS0 % Global X0 y0 POS0=get(handles.WritingAxes,'currentpoint');
x0=pos0(1.1);
y0=pos0(1.2);
if (pos0(1.1) > =0&pos0(1.1) < =100) && (pos0(1.2) > =0&pos0(1.2) < =100)
flag=1; end %-------------------------------------------------------------------------- % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)% CLC % tablet implementation -- move the mouse to draw line implementation program Global Flag global POS0 global X0 y0 pos=get(handles.WritingAxes,'currentpoint');
x=pos(1.1);
y=pos(1.2);
if flag && (pos(1.1) > =0&pos(1.1) <100) && (pos(1.2) > =0&pos(1.2) <100)
line(x,y, 'marker'.'. '.'markerSize'.18.'LineStyle'.The '-'.'LineWidth'.2.'Color'.'Black');
if x>x0
stepX=0.1;
else
stepX=0.1;
end
if y>y0
stepY=0.1;
else
stepY=0.1;
end
X=x0:stepX:x;
if abs(x-x0)< 0.01 Y=y0:stepY:y;
else
Y=(y-y0)*(X-x0)/(x-x0)+y0;
end
line(X ,Y, 'marker'.'. '.'markerSize'.18.'LineStyle'.The '-'.'LineWidth'.2.'Color'.'Black');
x0=x;
y0=y;
pos0=pos;
else
flag=0; end %------------------------------------------------------------------------- % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --function figure1_WindowButtonUpFcn(hObject, eventdata, handles)% CLC % tablet implementation procedures -- release the left mouse button to end the drawing process global Flag Flag=0;
%global data
data=[];
Img=getframe(handles.WritingAxes);
imwrite(Img.cdata,'Current handwritten numeral.bmp'.'bmp');
I=imread('Current handwritten numeral.bmp');
I=rgb2gray(I);
I=im2bw(I);
imwrite(I,'Current handwritten numeral.bmp'.'bmp');
I=imread('Current handwritten numeral.bmp');
data=GetFeature(I);
%--------------------------------------------------------------------------
function y=BayesLeastRisk(data)
clc;
load templatepattern; % converts the numeric characteristics to0,1Two numeric representationfor i=1:10
for j=1:25
for k=1:pattern(1,i).num
if pattern(1,i).feature(j,k)>0.1
pattern(1,i).feature(j,k)=1;
else
pattern(1,i).feature(j,k)=0; end end end end [pc_template,pc_data]=pcapro(data); % Principal component analysis temp=0;
for i=1:10
pattern(1,i).feature=pc_template(:,temp+1:temp+pattern(1,i).num);
temp=temp+pattern(1,i).num; Covariance matrix, covariance matrix inverse, covariance matrix determinant s_cov=[]; s_inv=[]; s_det=[];for i=1:10
s_cov(i).data=cov(pattern(1,i).feature');
s_inv(i).data=inv(s_cov(i).data);
s_det(i)=det(s_cov(i).data);
end
Copy the code
3. Operation results
Fourth, note
Version: 2014 a