📄 svm.m
字号:
function D = support_vectors(train_features, train_targets, params, region);
% Classify using the Support Vectors algorithm
% Inputs:
% features - Train features
% targets - Train targets
% Params - A vector of many parameters
% region - Decision region vector: [-x x -y y number_of_points]
%
% Outputs
% D - Decision sufrace
if (isempty(dir('learn.*')) | isempty(dir('classify.*')) | isempty(dir('svm_learn.*')) | isempty(dir('svm_classify.*'))),
disp('SVMlight executable files not found.');
disp('These files can be downloaded from http://ais.gmd.de/~thorsten/svm_light');
disp('After downloading them, compile the files as explained in the SVMlight package');
disp('into two modules: A learning module named ''learn'' and a classification');
disp('module named ''classify''');
error('Cannot continue without the executables');
end
write_svm_data(train_features,train_targets, 'train_data.txt');
letters = ['v', 'c', 'j', 'b', 'i', 'p', 't', 'd', 'g', 's', 'r', 'q', 'u', 'm', 'e', 'h', 'f'];
defaults = [1 1000 1 1 0 0.5 0 2 50 1 1 0 10 40 0.001 100 1];
str = 'learn ';
for i = 1:length(defaults),
if (defaults(i) ~= params(i)),
str = [str ' -' letters(i) ' ' num2str(params(i))];
end
end
str = [str ' train_data.txt model'];
feval('!',str);
%Generate test features
N = region(5);
x = ones(N,1) * linspace (region(1),region(2),N);
y = linspace (region(3),region(4),N)' * ones(1,N);
test_features = [x(:)';y(:)'];
%Find decision region
write_svm_data(test_features, ones(1,size(test_features,2)), 'test_data.txt')
feval('!',['classify -v ' num2str(params(1)) ' test_data.txt model output.txt']);
load output.txt
final_targets = (output > 0);
D = reshape(final_targets,region(5),region(5));
delete('output.txt');
delete('model');
delete('test_data.txt');
delete('train_data.txt');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -