📄 test_classify.m
字号:
% Test_Classifiy: the main module for MATLABArsenal
%
% Parameter:
% classifier: the classifier description string
% Example: test_classify('classify -t DataExample1.txt -sf 1 -- LibSVM
% -Kernel 0 -CostFactor 3');
% input_data: input data from MATLAB internal codes
% Example: A = load('DataExample1.txt');
% test_classify('classify -sf 1 -- LibSVM -Kernel 0 -CostFactor 3', A);
%
% Output:
% run: include all the classification results
function run = Test_Classify(classifier, input_data)
warning('off','MATLAB:colon:operandsNotRealScalar');
if (nargin < 2), input_data = []; end;
% clear global preprocess;
global preprocess;
global temp_train_file temp_test_file temp_output_file temp_model_file weka_dir mySVM_dir libSVM_dir SVMLight_dir;
preprocess = [];
if (~isfield(preprocess, 'Verbosity')), preprocess.Verbosity = 1; end;
if (~isfield(preprocess, 'Message')), preprocess.Message = ''; end;
if (~isfield(preprocess, 'NumCrossFolder')), preprocess.NumCrossFolder = 3; end;
if (~isfield(preprocess, 'TrainTestSplitBoundary')), preprocess.TrainTestSplitBoundary = 100; end;
if (~isfield(preprocess, 'Normalization')), preprocess.Normalization = 1; end;
if (~isfield(preprocess, 'SizeFactor')), preprocess.SizeFactor = 0.5; end;
if (~isfield(preprocess, 'ShotAvailable')), preprocess.ShotAvailable = 0; end;
if (~isfield(preprocess, 'DataSampling')), preprocess.DataSampling = 0; end;
if (~isfield(preprocess, 'Sparse')), preprocess.Sparse = 0; end;
if (~isfield(preprocess, 'Shuffled')), preprocess.Shuffled = 0; end;
if (~isfield(preprocess, 'OutputFlag')), preprocess.OutputFlag = 'a'; end;
if (~isfield(preprocess, 'SVD')), preprocess.SVD = 0; end;
if (~isfield(preprocess, 'FLD')), preprocess.FLD = 0; end;
if (~isfield(preprocess, 'CHI')), preprocess.ChiSquare = 0; end;
if (~isfield(preprocess, 'ValidateByShot')), preprocess.ValidateByShot = 0; end;
if (~isfield(preprocess, 'ComputeMAP')), preprocess.ComputeMAP = 0; end;
if (~isfield(preprocess, 'Evaluation')), preprocess.Evaluation = 0; preprocess.TrainTestSplitBoundary = -2; end;
if (~isfield(preprocess, 'MultiClassType')), preprocess.MultiClassType = 0; end;
if (~isfield(preprocess, 'MultiClass') | (preprocess.MultiClassType == 0)),
preprocess.MultiClass.LabelType = 1; preprocess.MultiClass.CodeType = -1; preprocess.MultiClass.LossFuncType = -1;
preprocess.MultiClass.UncertaintyFuncType = -1; preprocess.MultiClass.ProbEstimation = -1;
end;
if (~isfield(preprocess, 'ConstraintAvailable')), preprocess.ConstraintAvailable = 0; end;
if (~isfield(preprocess, 'ConstraintFileName')), preprocess.ConstraintFileName = ''; end;
if (~isfield(preprocess, 'input_file')), preprocess.input_file = ''; end;
if (~isfield(preprocess, 'output_file')), preprocess.output_file = ''; end;
if (~isfield(preprocess, 'pred_file')), preprocess.pred_file = ''; end;
if (~isfield(preprocess, 'model_file')), preprocess.model_file = ''; end;
if (~isfield(preprocess, 'WorkingDir')), preprocess.WorkingDir = ''; end;
if (~isfield(preprocess, 'NormalizePred')), preprocess.NormalizePred = 0; end;
if (~isfield(preprocess, 'ActualNumClass')), preprocess.ActualNumClass = 0; end;
if (~isfield(preprocess, 'TestOnly')), preprocess.TestOnly = 0; end;
if (~isfield(preprocess, 'TrainOnly')), preprocess.TrainOnly = 0; end;
if (~isfield(preprocess, 'CurModelCount')), preprocess.CurModelCount = 0; end;
if (nargin < 1), Report_Error; end;
[header, para, rem] = ParseCmd(classifier, '--', 0);
if (strcmpi(header, 'classify')),
p = str2num(char(ParseParameter(para, {'-v'; '-sf'; '-n'; '-sh'; '-vs'; '-ds'; '-dsr'; '-svd'; '-fld'; '-map'; '-if'; '-chi'; '-of'; '-sp'; '-np'; '-ac'; '-ldir'}, ...
{'1'; '0'; '1'; '0'; '1'; '0'; '0'; '0'; '0'; '0'; '0'; '0'; '0'; '0'; '0'; '1'; '0'}, 0)));
preprocess.Verbosity = p(1);
preprocess.Shuffled = p(2);
preprocess.Normalization = p(3);
preprocess.ShotAvailable = p(4);
preprocess.ValidateByShot = p(5);
preprocess.DataSampling = p(6);
preprocess.DataSamplingRate = p(7);
preprocess.SVD = p(8);
preprocess.FLD = p(9);
preprocess.ComputeMAP = p(10);
preprocess.InputFormat = p(11);
preprocess.ChiSquare = p(12);
preprocess.OutputFormat = p(13);
% preprocess.PredFormat = p(14);
preprocess.Sparse = p(14);
preprocess.NormalizePred = p(15);
preprocess.ActualNumClass = p(16);
preprocess.LDirAlloc = p(17);
p = ParseParameter(para, {'-t'; '-o'; '-p'; '-oflag'; '-dir'; '-drf' }, {''; ''; ''; 'a'; ''; ''}, 0);
preprocess.input_file = char(p{1, :});
preprocess.output_file = char(p{2, :});
preprocess.pred_file = char(p{3, :});
preprocess.OutputFlag = char(p{4, :});
preprocess.WorkingDir = char(p{5, :});
preprocess.DimReductionFile = char(p{6, :});
classifier = rem;
else
Report_Error;
end;
% Setup the environmental varaible for directory information
if (isempty(preprocess.WorkingDir)),
% preprocess.WorkingDir = cd;
filename = 'Classify.m';
if (~exist(filename)),
error('Cannot find the files in MATLABArsenal!');
end;
cur_dir = which(filename);
sep_pos = findstr(cur_dir, filesep);
preprocess.WorkingDir = cur_dir(1:sep_pos(length(sep_pos))-1);
end;
root = preprocess.WorkingDir;
temp_dir = sprintf('%s/temp', root);
if (~exist(temp_dir)),
% eval(sprintf('!md \"%s\"', temp_dir));
s = mkdir(root, 'temp');
if (s ~= 1), error('Cannot create temp directory!'); end;
end;
temp_train_file = sprintf('%s/temp.train.txt', temp_dir);
temp_test_file = sprintf('%s/temp.test.txt', temp_dir);
temp_output_file = sprintf('%s/temp.output.txt', temp_dir);
temp_model_file = sprintf('%s/temp.model.txt', temp_dir);
weka_dir = sprintf('%s/weka-3-4/weka.jar', root);
mySVM_dir = sprintf('%s/svm', root);
libSVM_dir = sprintf('%s/svm', root);
SVMLight_dir = sprintf('%s/svm', root);
[header, para, rem] = ParseCmd(classifier, '--', 0);
if (strcmpi(header, 'train_test_validate')),
preprocess.Evaluation = 0;
p = str2num(char(ParseParameter(para, {'-t'; '-test'; '-train'}, {'-2'; '0'; '0'}, 0)));
preprocess.TrainTestSplitBoundary = p(1);
preprocess.TestOnly = p(2);
preprocess.TrainOnly = p(3);
p = ParseParameter(para, {'-m' }, {''}, 0);
preprocess.model_file = char(p{1, :});
classifier = rem;
elseif (strcmpi(header, 'cross_validate')),
preprocess.Evaluation = 1;
p = str2num(char(ParseParameter(para, {'-t'}, {'3'}, 0)));
preprocess.NumCrossFolder = p(1);
classifier = rem;
elseif (strcmpi(header, 'test_file_validate')),
preprocess.Evaluation = 2;
p = char(ParseParameter(para, {'-t'}, {''}, 0));
preprocess.test_file = p(1, :);
classifier = rem;
elseif (strcmpi(header, 'train_only')),
preprocess.Evaluation = 3;
p = char(ParseParameter(para, {'-m'}, {''}, 0));
preprocess.TrainTestSplitBoundary = -1;
preprocess.TrainOnly = 1;
preprocess.model_file = p(1, :);
classifier = rem;
elseif (strcmpi(header, 'test_only')),
preprocess.Evaluation = 4;
p = char(ParseParameter(para, {'-m'}, {''}, 0));
preprocess.TestOnly = 1;
preprocess.model_file = p(1, :);
classifier = rem;
end;
if (preprocess.TrainOnly == 1),
if (isempty(preprocess.model_file)),
error('Error: the model file is empty!');
end;
end;
if (preprocess.TestOnly == 1),
if (isempty(preprocess.model_file)),
error('Error: the model file is empty!');
end;
% Load the dataset information
fprintf('Loading model file %s.mat \n', preprocess.model_file);
setting = load(sprintf('%s.mat', preprocess.model_file));
preprocess.OrgClassSet = setting.class_set;
classifier = setting.classifier;
fprintf('Classifier: %s\n', classifier);
end;
if (isempty(classifier)),
fprintf('Warning: the classfier is empty, use ZeroR instead!\n');
classifier = 'ZeroR';
end;
% Initialize the message string
preprocess.Message = '';
if (preprocess.Evaluation == 0)
msg = sprintf(' Train-Test Split, Boundary: %d, ', preprocess.TrainTestSplitBoundary);
preprocess.Message = [preprocess.Message msg];
elseif (preprocess.Evaluation == 1)
msg = sprintf(' Cross Validation, Folder: %d, ', preprocess.NumCrossFolder);
preprocess.Message = [preprocess.Message msg];
elseif (preprocess.Evaluation == 2)
msg = sprintf(' Testing on File %s, ', preprocess.test_file);
preprocess.Message = [preprocess.Message msg];
elseif (preprocess.Evaluation == 3)
msg = sprintf(' Training on File %s, ', preprocess.input_file);
preprocess.Message = [preprocess.Message msg];
elseif (preprocess.Evaluation == 4)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -