parzen.m

来自「用于分类的一个工具箱」· M 代码 · 共 41 行

M
41
字号
function test_targets = parzen(train_patterns, train_targets, test_patterns, hn)

% Classify using the Parzen windows algorithm
% Inputs:
% 	train_patterns	- Train patterns
%	train_targets	- Train targets
%   test_patterns   - Test  patterns
%	hn      	    - Normalizing factor for h
%
% Outputs
%	test_targets	- Predicted targets

N       = size(test_patterns, 2);
Uc      = unique(train_targets);
V		= zeros(length(Uc), N);
x_i     = train_patterns;

for j = 1:length(Uc),
    indices = find(train_targets == Uc(j));
    P(j)    = length(indices)/size(x_i,2);
    n		= length(indices);
    
    for i = 1:n,
        temp      = sum((test_patterns - x_i(:,indices(i))*ones(1,N)).^2);
        V(j,:)    = V(j,:) + phi(temp./hn);
    end
    
    V(j,:) = V(j,:) / sum(V(j,:)) * P(j);
end

test_targets = zeros(1, N);
for i = 1:N,
    [m, best]       = max(V(:,i));
    test_targets(i) = Uc(best);
end
%END Parzen

function p = phi(val)

%The window function for the Parzen window
p = (abs(val) <= 0.5);

⌨️ 快捷键说明

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