matlab中使用SVM

1 下载libsvm的源码
2 在matlab中编译
先输入:

mex -setup

选择编译器
再直接输入make编译
3 使用例子:

%% I. 清空环境变量
clear all
clc
%% II. 导入数据
load iris_data.mat
%%
% 1. 随机产生训练集和测试集
n = randperm(size(features,1));
%%
% 2. 训练集——120个样本
train_features = features(n(1:120),:);
train_classes = classes(n(1:120),:);
%%
% 3. 测试集——30个样本
test_features = features(n(121:end),:);
test_classes = classes(n(121:end),:);
%% III. 数据归一化
[Train_features,PS] = mapminmax(train_features');
Train_features = Train_features';
Test_features = mapminmax('apply',test_features',PS);
Test_features = Test_features';
%%
% 2. 创建/训练SVM模型
model = svmtrain(train_classes,Train_features);
%% V. SVM仿真测试
[predict_label_1,accuracy_1] = svmpredict(train_classes,Train_features,model);
[predict_label_2,accuracy_2] = svmpredict(test_classes,Test_features,model);

最终结果:
Accuracy = 97.5% (117/120) (classification)
Accuracy = 96.6667% (29/30) (classification)

你可能感兴趣的:(机器学习)