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

📄 test_classify.m

📁 一款数据挖掘的软件
💻 M
📖 第 1 页 / 共 2 页
字号:
    msg = sprintf(' Testing on File %s, ', preprocess.input_file);
    preprocess.Message = [preprocess.Message msg];     
else 
    msg = sprintf(' Train-Test Split, Boundary: %d, ', preprocess.TrainTestSplitBoundary);
    preprocess.Message = [preprocess.Message msg]; 
end;

if (preprocess.MultiClassType == 0)
    msg = sprintf(' Classification, ', preprocess.TrainTestSplitBoundary);
    preprocess.Message = [preprocess.Message msg]; 
elseif (preprocess.MultiClassType == 1) 
    msg = sprintf(' Multiclass Classification Wrapper, ', preprocess.NumCrossFolder);
    preprocess.Message = [preprocess.Message msg];     
elseif (preprocess.MultiClassType == 2) 
    msg = sprintf(' Multilabel Classification Wrapper, ', preprocess.NumCrossFolder);
    preprocess.Message = [preprocess.Message msg];     
elseif (preprocess.MultiClassType == 3) 
    msg = sprintf(' Multiclass Active Learning Wrapper, ', preprocess.NumCrossFolder);
    preprocess.Message = [preprocess.Message msg];     
end;

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

% load the data
D = [];
output_file = [];
pred_file = [];
if (isfield(preprocess, 'input_file') & (~isempty(preprocess.input_file))), 
    % process the filenames
    input_file = preprocess.input_file;
    output_file = preprocess.output_file;
    pred_file = preprocess.pred_file;

    D = dlmread_format(input_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, Data: %d, Feature: %d, Class: %d\n', ...
            preprocess.input_file, size(D,1), size(D,2)-1, length(unique(D(:, size(D,2))))); 
        fprintf(fid, 'Classifier:%s\nMessage:%s\n',classifier, preprocess.Message);
    end;
    fclose(fid);    

    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;
    fclose(fid);    

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

switch (preprocess.Evaluation)
    case {0,3,4}
        % 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_format(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;   
end;

if (isempty(D)),
    if (~isempty(input_data)),
        D = input_data;
    else
        error('The input data are not provided!');
    end;
end;
fprintf('Message:%s\n', preprocess.Message);
run = feval(EvaluationHandle, D, classifier);

% 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, '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;

% Save the dataset information
if (preprocess.TrainOnly == 1),
    fprintf('Saving data info to the model file %s.mat\n', preprocess.model_file);
    class_set = preprocess.OrgClassSet;
    save(sprintf('%s.mat', preprocess.model_file), 'class_set', 'classifier');
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     ');

function D = dlmread_format(input_file)

global preprocess;

if (preprocess.InputFormat == 2),
    %strcmd = sprintf('!perl %s/ConvertFileInput.pl %s %s.stdout', preprocess.WorkingDir, input_file, input_file);
    %fprintf('!perl %s/ConvertFileInput.pl %s %s.stdout\n', preprocess.WorkingDir, input_file, input_file); 
    strcmd = sprintf('!%s/ConvertSparse.exe %s %s.stdout', preprocess.WorkingDir, input_file, input_file);
    fprintf('!%s/ConvertSparse.exe %s %s.stdout\n', preprocess.WorkingDir, 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 (isempty(D)), 
    error(' the input file not found! '); 
end;
if (preprocess.Sparse == 1),
    D = spconvert(D);
end;

⌨️ 快捷键说明

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