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

📄 knnrloo.m

📁 一个关于数据聚类和模式识别的程序,在生物化学,化学中因该都可以用到.希望对大家有用,谢谢支持
💻 M
字号:
function [recogRate, computed, nearestIndex] = knnrLoo(DS, param, plotOpt)
%knnrLoo: Leave-one-out recognition rate of KNNR
%	Usage: [recogRate, computed, nearestIndex] = knnrLoo(DS, param, plotOpt)
%		recogRate: recognition rate
%		computed: Computed output
%		nearestIndex: Nearest sample index of all data points
%		DS: Design set
%			DS.input: Input data (each column is a feature vector)
%			DS.output: Output class (ranging from 1 to N)
%		param: Classifier parameters
%			param.k: The "k" in k-nearest neighbor rule
%			param.smallSet = 0 for small data set (vectorized operation based), 1 for large data set (for-loop based)
%		plotOpt: 1 for ploting data (2D only)
%
%	For example:
%		DS=prData('random2');
%		param.k=1;
%		param.smallSet=1;	% small dataset
%		plotOpt=1;
%		[recogRate, computed, nearestIndex] = knnrLoo(DS, param, plotOpt);

%	Roger Jang, 19970628, 20040928

if nargin<1, selfdemo; return; end
if nargin<2, param.k=1;	param.smallSet=size(DS.input, 2)<=1000; end
if isempty(param), param.k=1;	param.smallSet=size(DS.input, 2)<=1000; end
if nargin<3, plotOpt=0; end
if isempty(param), param.k=1;	param.smallSet=size(DS.input, 2)<=1000; end

output=unique(DS.output);
if ~isequal(1:length(output), output)
	error('DS.output has wrong format! (It should have a value from 1 to no. of classes.)\n');
end

[dim, dataNum] = size(DS.input);
if param.smallSet			% Vectorized operation; suitable for small dataset
	classLabel = elementCount(DS.output);
	classNum = length(classLabel);
	distMat = pairwiseSqrDistance(double(DS.input));
	distMat(1:(dataNum+1):dataNum^2) = inf;	% Set diagonal elements to inf

	% knnrMat(i,j) = class of i-th nearest point of j-th input vector
	[junk, nearestIndex] = sort(distMat, 1);
	knnrMat = DS.output(nearestIndex(1:param.k,:));
	% classCount(i,j) = count of class-i points within j-th input vector's neighborhood
	classCount = zeros(classNum, dataNum);
	for i = 1:dataNum,
		[sortedElement, count] = elementCount(knnrMat(:,i));
		classCount(sortedElement, i) = count;
	end
	[junk, computed] = max(classCount, [], 1);
else						% For-loop version; suitable for large dataset
	nearestIndex = zeros(1, dataNum);
	computed = zeros(size(DS.output));
	for i=1:dataNum
	%	if rem(i, 100)==0, fprintf('%d/%d\n', i, dataNum); end
		looData = DS;
		looData.input(:,i) = [];
		looData.output(:,i) = [];
		TS.input=DS.input(:,i);
		TS.output=DS.output(:,i);
		[computed(i), junk, tmp] = knnr(looData, TS, param.k);
		nearestIndex(i) = tmp(1);
		if nearestIndex(i)>=i,
			nearestIndex(i)=nearestIndex(i)+1;
		end
	end
end
hitIndex = find(DS.output==computed);
recogRate = length(hitIndex)/dataNum;

if plotOpt & dim==2
	dcprDataPlot(DS);
	axis image; box on
	missIndex=1:dataNum;
	missIndex(hitIndex)=[];
	% display these points
	for i=1:length(missIndex),
		line(DS.input(1,missIndex(i)), DS.input(2,missIndex(i)), 'marker', 'x', 'color', 'k');
	end
	titleString = sprintf('%d leave-one-out error points denoted by "x".', length(missIndex));
	title(titleString);
end

% ====== Self demo ======
function selfdemo
DS=prData('random2');
param.k=1;
param.smallSet=1;	% small dataset
plotOpt=1;
[recogRate, hitIndex, nearestIndex] = feval(mfilename, DS, param, plotOpt);

⌨️ 快捷键说明

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