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

📄 oc_set.m

📁 数据挖掘的工具箱,最新版的,希望对做这方面研究的人有用
💻 M
字号:
function [a,I] = oc_set(a,clnr)% OC_SET  makes an one-class dataset%%     A = oc_set(A,clnr)%% Change a normal dataset A into an one-class dataset: the class% indicated by the classnr (clnr) is made target class and all other% data becomes outliers class. The labels are changed to 'target' and% 'outlier'.%% It is also possible to use a class label:%%    A = oc_set(A,label)%% but then the type of the label should be a char (else you cannot% distinguish it from a class number, can you?:-))%%    [A,I] = oc_set(A,label)%% As a second output argument an index vector I is returned, indicating% which objects are target (I=1) or outlier (I=2).%%% See also: target_class, find_target, gendatoc, isocset% Copyright: D. Tax, R.P.W. Duin, davidt@ph.tn.tudelft.nl% Faculty of Applied Physics, Delft University of Technology% P.O. Box 5046, 2600 GA Delft, The Netherlandsif (nargin<2)	clnr = 1;end% I have to check Prtools somewhere...:checkprversion;if ~isdataset(a)	% If a is just a Matlab array, everything becomes target	% data:	m = size(a,1);	a = dataset(a,ones(m,1),'lablist','target');	I = ones(m,1);	return;else	if is_ocset(a)		% If we are given an one-class dataset, the only possibility is		% that we have to switch the target-outlier labels:		if (isa(clnr,'char') & clnr(1)=='o') | ...			(isa(clnr,'double') & clnr<0)			a = set(a,'nlab',3-getnlab(a));		end		%else we have already a correct oneclass dataset		I = repmat(2,size(a,1),1);		I(find_target(a)) = 1;		return;	end	% Now we have to work, detect the label in the dataset:	[nlab,lablist] = getnlab(a);	if isempty(lablist)		warning('This dataset is unlabeled: all data is considered target!');		[a,I] = oc_set(+a);		return	end		[m,k,c] = getsize(a,1);	% This is a hack:	% If the lablist contains characters, but all character values are	% smaller than 32 (smaller ASCI value), than something went probabily	% wrong, and then we assume it were just doubles. This happens for	% instance in the NIST dataset:-(	if isa(lablist,'char')		if length(find(lablist<32))==size(lablist,1)			a = set(a,'lablist',double(lablist));		end	end	% Depending on the type of label, we have to do other things:	if isa(clnr,'char')		% We have to string-match with the lablist		if isa(lablist,'double')			% If the lablist is a number, we'd better change the clnr also			% to a number			clnr = str2num(clnr);			if ~isempty(clnr)				clnr = find(lablist==clnr);			%else the clnr was a label-string and not a number, so it will			%not match anyway			end		else % Lablist is character			% Ok, we get a bit inconsistent behaviour here. It might be			% that when the dataset contains just target objects, the label			% becomes 'target' without trailing ' '. Then the strmatch has			% to be switched. When also outliers are present, there will be			% always this trailing ' ', so then we don't have a problem.			% (Thanks Piotr!:-)			if (c==1)				clnr = strmatch(lablist,clnr);			else				clnr = strmatch(clnr,lablist);			end		end	end	% Make the new labels, target objects get 1, outlier objects get 2:	if isempty(clnr)		% everything becomes outlier		I = repmat(2,m,1);	else		% Otherwise apply the hard fought for clnr:		I = 2-(nlab==clnr);	end		% Now construct the dataset:	labelnames = str2mat('target', 'outlier');	a = set(a,'labels',labelnames(I,:));	% and fix the new dataset name:	clname = lablist(clnr,:);	if isa(clname,'double')		clname = num2str(clname);	end	if ~isempty(clname)		a = setname(a,[getname(a),' (targetcl. ',clname,')']);	end	% Maybe we should give some message?	if isempty(clname)		fprintf('No target class found.\n');	else		fprintf('Class %s is used as target class.\n',clname);	end	endreturn

⌨️ 快捷键说明

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