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

📄 nearestneighborediting.m

📁 模式分类matlab工具箱
💻 M
字号:
function [patterns, targets] = NearestNeighborEditing(train_patterns, train_targets, Nmu, plot_on)

%Reduce the number of data points using the nearest neighbor editing algorithm
%Inputs:
%	train_patterns	- Input patterns
%	train_targets	- Input targets
%	Nmu				- Unused
%   plot_on         - Unused
%
%Outputs
%	patterns		- New patterns (Prototypes)
%	targets			- New targets

N   = size(train_patterns,2);
options = optimset('Display','off');

% In this implementation we find only adjacent Voronoi cells to each example, in order to reduce 
% the computational costs. This is done through linear programming (LP)
b   = sum(train_patterns.^2);
A   = 2*train_patterns;
A(end+1, :) = -ones(1,N);

neighbors = zeros(N);
for i = 1:N-1,
    for j = i+1:N,
        b_tag       = b; 
        b_tag(j)    = b(j) + 1;
        constraintA = [A, -A(:,i)]';
        constraintB = [b_tag, -b(i)]';
        sol         = linprog(-A(:,j)', constraintA, constraintB, [], [], [], [], [], options);
        f           = b(j) - A(:,j)'*sol;
        
        if (f < 0)
            neighbors(i,j) = 1;
            neighbors(j,i) = 1;
        end
    end
    disp (['Finished ' num2str(i) ' of ' num2str(N-1) ' loops.'])
end
        
% For each pattern, check if all it's neighbors are the same. If so, delete it.
keep = ones(1,N);
for i = 1:N,
    in  = find(neighbors(i,:) == 1);
    if (length(unique(train_targets(in))) == 1)
        keep(i) = 0;
    end
end

keep_in = find(keep);

patterns = train_patterns(:,keep_in);
targets  = train_targets (keep_in);

⌨️ 快捷键说明

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