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

📄 test_classify.m

📁 一个matlab的工具包,里面包括一些分类器 例如 KNN KMEAN SVM NETLAB 等等有很多.
💻 M
📖 第 1 页 / 共 2 页
字号:
    msg = sprintf(' Loss: Exp ');
    preprocess.Message = [preprocess.Message msg]; 
elseif (preprocess.MultiClass.LossFuncType == 2)
    msg = sprintf(' Loss: (1-Y)+ ');
    preprocess.Message = [preprocess.Message msg];
end;    

if (preprocess.MultiClass.UncertaintyFuncType == 0) 
    msg = sprintf(' Uncertainty: L1 ');
    preprocess.Message = [preprocess.Message msg];
elseif (preprocess.MultiClass.UncertaintyFuncType == 1)
    msg = sprintf(' Uncertainty: Exp ');
    preprocess.Message = [preprocess.Message msg]; 
elseif (preprocess.MultiClass.UncertaintyFuncType == 2)
    msg = sprintf(' Uncertainty: (1-Y)+ ');
    preprocess.Message = [preprocess.Message msg];
elseif (preprocess.MultiClass.UncertaintyFuncType == 3)
    msg = sprintf(' Uncertainty: Min Margin ');
    preprocess.Message = [preprocess.Message msg];
elseif (preprocess.MultiClass.UncertaintyFuncType == 4)
    msg = sprintf(' Uncertainty: -ln(1+x) ');
    preprocess.Message = [preprocess.Message msg];
elseif (preprocess.MultiClass.UncertaintyFuncType == 5)
    msg = sprintf(' Uncertainty: Random ');
    preprocess.Message = [preprocess.Message msg];
end;    

if (preprocess.MultiClass.ProbEstimation == 0) 
    msg = sprintf(' Best Worse ');
    preprocess.Message = [preprocess.Message msg];
elseif (preprocess.MultiClass.ProbEstimation == 1)
    msg = sprintf(' Uniform Guess ');
    preprocess.Message = [preprocess.Message msg]; 
elseif (preprocess.MultiClass.ProbEstimation == 2)
    msg = sprintf(' Binary Class ');
    preprocess.Message = [preprocess.Message msg];     
end;

if (preprocess.DataSampling == 1)
    msg = sprintf(' Sampling Rate: %d', preprocess.DataSamplingRate);
    preprocess.Message = [preprocess.Message msg];    
end;

%num_folder = preprocess.NumFolder;
%num_cross_folder = preprocess.NumCrossFolder;; 

% load in the data
if ((~isfield(preprocess, 'input_file')) | (isempty(preprocess.input_file))), 
    error('The input file is not provided!');
end;

input_file = preprocess.input_file;
output_file = preprocess.output_file;
pred_file = preprocess.pred_file;

%if (~isempty(output_file)) 
fid = fopen(output_file, preprocess.OutputFlag);
if (fid < 0),
    if ((~isfield(preprocess, 'test_file')) | (isempty(preprocess.test_file))), 
        output_file = sprintf('%s.result', input_file);
    else
        output_file = sprintf('%s.result', preprocess.test_file);
    end;
    preprocess.output_file = output_file;
    fid = fopen(output_file, preprocess.OutputFlag);
end;
if (preprocess.OutputFormat == 0),
    fprintf(fid, '\nProcessing Filename: %s\n', preprocess.input_file); 
    fprintf(fid, 'Classifier:%s\nMessage:%s\n',classifier, preprocess.Message);
end;
fclose(fid);    
%end;

fid = fopen(pred_file, preprocess.OutputFlag);
if (fid < 0),
    if ((~isfield(preprocess, 'test_file')) | (isempty(preprocess.test_file))), 
        pred_file = sprintf('%s.pred', input_file);
    else
        pred_file = sprintf('%s.pred', preprocess.test_file);
    end;
    preprocess.pred_file = pred_file;
    fid = fopen(pred_file, preprocess.OutputFlag);
end;
if (preprocess.PredFormat == 0),
    fprintf(fid, '\nProcessing Filename: %s\n', preprocess.input_file); 
    fprintf(fid, 'Classifier:%s\nMessage:%s\n',classifier, preprocess.Message);
end;
fclose(fid);    

if (preprocess.InputFormat == 2),
    %strcmd = sprintf('!perl %s/ConvertFileInput.pl %s %s.stdout', root, input_file, input_file);
    %fprintf('!perl %s/ConvertFileInput.pl %s %s.stdout\n', root, input_file, input_file); 
    strcmd = sprintf('!%s/ConvertSparse.exe %s %s.stdout', root, input_file, input_file);
    fprintf('!%s/ConvertSparse.exe %s %s.stdout\n', root, input_file, input_file);    
    eval(strcmd);
    preprocess.Sparse = 1;
    D = dlmread(sprintf('%s.stdout', input_file));
elseif (preprocess.InputFormat == 1),
    preprocess.Sparse = 1;
    D = dlmread(input_file);    
else
    D = dlmread(input_file);
end;

if (preprocess.Sparse == 1),
    D = spconvert(D);
end;

% Automatically judge whether the shot information is available
if (preprocess.ShotAvailable < 0),
		preprocess.ShotAvailable = 0;
%   if (length(unique(D(:, size(D, 2)))) > 10), 
%       preprocess.ShotAvailable = 1;
%   else
%       preprocess.ShotAvailable = 0;
%   end;
%   fprintf('Automatically detect preprocess.ShotAvailable to be %d \n', preprocess.ShotAvailable );
end;

fprintf('Finished loading %s.............\n', input_file);
fprintf('Output Results to %s.............\n', output_file);
fprintf('Output Predictions to %s.............\n', pred_file);

switch (preprocess.Evaluation)
    case 0
        % Train Test
        EvaluationHandle = @Train_Test_Validate;
    case 1
        % Cross Validate
        EvaluationHandle = @Cross_Validate;
    case 2
        % Test File Validate
        if ((~isfield(preprocess, 'test_file')) | (isempty(preprocess.test_file))), 
            error('The test file is not provided!');
        end;
        D_test = dlmread(preprocess.test_file, ',');
        fprintf('Finished loading the test file %s.............\n', preprocess.test_file);
        preprocess.TrainTestSplitBoundary = size(D, 1);
        preprocess.Shuffled = 0;
        D = [D; D_test];
        EvaluationHandle = @Train_Test_Validate;   
    case 3
        % Training Only
        EvaluationHandle = @Train_Validate;
    case 4
        % Testing Only
        EvaluationHandle = @Test_Validate;
end;

switch (preprocess.MultiClassType)
    case 0
        fhandle = @Train_Test_Simple;
    case 1
        fhandle = @Train_Test_Multiple_Class;
    case 2
        fhandle = @Train_Test_Multiple_Label;
    case 3
        fhandle = @Train_Test_Multiple_Class_AL;
end;
       
fprintf('Classifier:%s\nMessage:%s\n',classifier, preprocess.Message);
if ((preprocess.Evaluation >= 3) & (preprocess.Evaluation <= 4))
    %if (preprocess.Evaluation == 3),
    %    run = Train_Validate(D, classifier);
    %elseif (preprocess.Evaluation == 4),
    %    run = Test_Validate(D, classifier);            
    %end;
    run = feval(EvaluationHandle, D, classifier);
else
    %if (preprocess.Evaluation == 0),
    %    run = Train_Test_Validate(D, fhandle, classifier);
    %elseif (preprocess.Evaluation == 1),
    %    run = Cross_Validate(D, fhandle, classifier);            
    %elseif (preprocess.Evaluation == 2),
    %    run = Train_Test_Validate(D, fhandle, classifier);            
    %end;
    run = feval(EvaluationHandle, D, fhandle, classifier);
end;
run.D = D;

% Cross validation and shuffled 
if ((preprocess.Evaluation == 1) && (preprocess.Shuffled == 1)), 
    run.Y_pred(preprocess.shuffleindex, :) = run.Y_pred;
    run.Y_pred(:,1) = 1:size(run.Y_pred, 1);
end;

OutputResult = [];
if (isfield(run, 'Err')), OutputResult = [OutputResult sprintf('Error = %f, ', run.Err)]; end;
if (isfield(run, 'Prec')), OutputResult = [OutputResult sprintf('Precision = %f, ', run.Prec)]; end;
if (isfield(run, 'Rec')), OutputResult = [OutputResult sprintf('Recall = %f, ', run.Rec)]; end;
if (isfield(run, 'F1')), OutputResult = [OutputResult sprintf('F1 = %f, ', run.F1)]; end;
if (isfield(run, 'Micro_Prec')), OutputResult = [OutputResult sprintf('Micro_Precision = %f, ', run.Micro_Prec)]; end;
if (isfield(run, 'Micro_Rec')), OutputResult = [OutputResult sprintf('Micro_Recall = %f, ', run.Micro_Rec)]; end;
if (isfield(run, 'Micro_F1')), OutputResult = [OutputResult sprintf('Micro_F1 = %f, ', run.Micro_F1)]; end;
if (isfield(run, 'Macro_Prec')), OutputResult = [OutputResult sprintf('Macro_Precision = %f, ', run.Macro_Prec)]; end;
if (isfield(run, 'Macro_Rec')), OutputResult = [OutputResult sprintf('Macro_Recall = %f, ', run.Macro_Rec)]; end;
if (isfield(run, 'Macro_F1')), OutputResult = [OutputResult sprintf('Macro_F1 = %f, ', run.Macro_F1)]; end;
if (isfield(run, 'AvgPrec')), OutputResult = [OutputResult sprintf('MAP = %f, ', run.AvgPrec)]; end;
if (isfield(run, 'BaseAvgPrec')), OutputResult = [OutputResult sprintf('MBAP = %f, ', run.BaseAvgPrec)]; end;

fprintf('%s\n', OutputResult);
if (~isempty(output_file)) 
    if ((preprocess.OutputFormat == 0) | (preprocess.OutputFormat == 1)),
        fid = fopen(output_file, 'a');
        fprintf(fid, '%s\n', OutputResult);
        fclose(fid);    
    end;
end;
if (~isempty(pred_file)) 
    fid = fopen(pred_file, 'w');
    if (preprocess.OutputFormat == 0),
        if (size(run.Y_pred, 2) == 4), 
            fprintf(fid, 'Index\tProb\t\tPred\tTruth\n');
            fprintf(fid, '%d\t%f\t%d\t%d\n', run.Y_pred');
        else
            fprintf(fid, 'Index\tProb\t\tPred\tTruth\tShotID\n');
            fprintf(fid, '%d\t%f\t%d\t%d\t%d\n', run.Y_pred');
        end;
    elseif (preprocess.OutputFormat == 1), % Only work when number of classes is 2
        fprintf(fid, 'Label: '); fprintf(fid, '%d ', preprocess.OrgClassSet); fprintf(fid, '\n');
        ProbClass1 = run.Y_pred(:, 2) .* (run.Y_pred(:, 3) == 1)  + (1 - run.Y_pred(:, 2)) .* (run.Y_pred(:, 3) ~= 1);
        fprintf(fid, '%d\t%f\t%f\n', [preprocess.OrgClassSet(run.Y_pred(:, 3)) ProbClass1 (1 - ProbClass1)]');    
    end;
    fclose(fid);    
end;

function Report_Error()

fprintf(' Example: Suppose you are in root directory\n');
fprintf(' Example: test_classify.exe \"classify -t demo/DataExample1.train.txt -sh 1 -- train_only -m demo/DataExample1.libSVM.model -- LibSVM -Kernel 0 -CostFactor 3\" \n');
fprintf(' Example: test_classify.exe \"classify -t demo/DataExample1.test.txt -sh 1 -- test_only -m demo/DataExample1.libSVM.model -- LibSVM -Kernel 0 -CostFactor 3\" \n');
fprintf(' Please refer to http://finalfantasyxi.inf.cs.cmu.edu/tmp/MATLABArsenal.htm for more examples \n');
error('  The command must begin with Classify     ');

⌨️ 快捷键说明

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