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

📄 mysvm.m

📁 一个matlab的工具包,里面包括一些分类器 例如 KNN KMEAN SVM NETLAB 等等有很多.
💻 M
字号:
function  [Y_compute, Y_prob] = mySVM(para, X_train, Y_train, X_test, Y_test, num_class)
   
global temp_train_file temp_test_file temp_output_file mySVM_dir; 
class_set = GetClassSet(Y_train);
config_file = char(ParseParameter(para, {'-Config'}, {'N/A'}));

if (num_class > 2)
    error('SVM_light: The class number is larger than 2!');
end;

% set up the commands
train_cmd = sprintf('!%s/mysvm %s/%s %s', ...
            mySVM_dir, mySVM_dir, config_file, temp_train_file);    
test_cmd =  sprintf('!%s/mySVMpredict %s/%s %s.svm %s', ...
            mySVM_dir, mySVM_dir, config_file, temp_train_file, temp_test_file);    
temp_output_file = sprintf('%s.pred', temp_test_file);
[num_data, num_feature] = size(X_train);

% make the format string
format_str = '';
format_str = strcat(format_str, '%d');
for i = 1:num_feature
    format_str = strcat(format_str, ' %f');
end
format_str = strcat(format_str, '\n');

if (~isempty(X_train)),
    % save the train data
    fid = fopen(temp_train_file, 'w');
    fprintf(fid, '@example\n');
    fprintf(fid, format_str, [Y_train, X_train]');
    fclose(fid);
    
    eval(train_cmd);
end;

% save the test data
fid = fopen(temp_test_file, 'w');
fprintf(fid, '@example\n');
fprintf(fid, format_str, [Y_test, X_test]');
fclose(fid);
   
% train the svm
eval(test_cmd);

fid = fopen(temp_output_file, 'r');
fgets(fid);
Ypred = fscanf(fid, '%f');
fclose(fid);
   
Y_compute = class_set(1) * (Ypred >= 0) + class_set(2) * (Ypred < 0);
Y_prob = Ypred;  

⌨️ 快捷键说明

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