⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 discrete_bayes.m

📁 Duda的《模式分类》第二版的配套的Matlab源代码
💻 M
字号:
function test_targets = Discrete_Bayes(train_patterns, train_targets, test_patterns, cost)

% Classify discrete patterns using the Bayes decision theory
% Inputs:
% 	train_patterns	- Train patterns
%	train_targets	- Train targets
%   test_patterns   - Test  patterns
%	cost			- Cost for class 0 (Optional, Unused yet)
%
% Outputs
%	test_targets	- Predicted targets

%First, find out if the patterns are indeed discrete 
%Of course, since this program is in Matlab, we define discrete as having 
%no more than one decimal place 
if (sum(sum(train_patterns*10~=floor(train_patterns*10))) ~= 0),
   error('Patterns are not discrete (See the definition of discrete in the m-file)')
end

Uc  = unique(train_targets);

%Find how the patterns are distributed
N	            = 0;
unique_patterns = [];
counts			= [];
for i = 1:size(train_patterns,2),
    data    = train_patterns(:,i);
    indices = find(sum((data*ones(1,size(train_patterns,2))-train_patterns).^2)==0);
    if isempty(unique_patterns),
        unique_patterns(:,1) = data;
        for j = 1:length(Uc),
            counts(j,1) = length(find(train_targets(indices) == Uc(j)));
        end
        N   = 2;
    else
        if isempty(find(sum((data*ones(1,size(unique_patterns,2))-unique_patterns).^2)==0)),
            %Add this pattern to the bank
            unique_patterns(:,N) = data;
            for j = 1:length(Uc),
                counts(j,N) = length(find(train_targets(indices) == Uc(j)));
            end
            N	= N + 1;
        end
    end
end

Px_given_w = (counts ./ (ones(length(Uc),1)* sum(counts)));
p          = hist(train_targets, Uc)' / length(train_targets);

Pw_given_x = Px_given_w .* (p*ones(1,N-1));
Pw_given_x = Pw_given_x ./ (ones(length(Uc),1)*sum(Pw_given_x));

[m, unique_targets] = max(Pw_given_x);
unique_targets      = unique_targets - 1;

test_targets = Nearest_Neighbor(unique_patterns,unique_targets,test_patterns,1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -