[matlab]机器学习及SVM工具箱学习笔记

机器学习与神经网络的关系:

机器学习是目的,神经网络是算法。神经网络是实现机器学习的一种方法,平行于SVM。

常用的两种工具:svm tool、libsvm

SVM分为SVC和SVR,svc是专门用来分类的,svr是用来作回归的

注:matlab自带的svm工具箱无回归预测功能

 函数介绍:http://blog.sina.com.cn/s/blog_6c76c0890100w1zm.html

libsvm参数介绍:http://blog.csdn.net/changyuanchn/article/details/7540014

    clear;
    N = 50;
    n=2*N;
    randn('state',6);
    x1 = randn(2,N)
    y1 = ones(1,N);
    x2 = 5+randn(2,N);
    y2 = -ones(1,N);
    figure;
    plot(x1(1,:),x1(2,:),'bx',x2(1,:),x2(2,:),'k.');
    axis([-3 8 -3 8]);
    title('C-SVC')
    hold on;
    X1 = [x1,x2];
    Y1 = [y1,y2];  
    X=X1';
    Y=Y1';
    C=Inf;
    ker='linear';
    global p1 p2
    p1=3;
    p2=1;
    
    %命令
    [nsv alpha bias] = svc(X,Y,ker,C)  %训练函数
    predictedY = svcoutput(X,Y,X,ker,alpha,bias)   %输入预测函数
    err = svcerror(trnX,trnY,tstX,tstY,ker,alpha,bias)  %分类函数,准确率
    svcplot(X,Y,ker,alpha,bias) %画图

  

libsvm使用(回归预测):

close all;
clear;
clc;
format compact;

% 生成待回归的数据
x = (-1:0.1:1)';
y = -x.^2;

% 建模回归模型
model = libsvmtrain(y,x,'-s 3 -t 2 -c 2.2 -g 2.8 -p 0.01');

% 利用建立的模型看其在训练集合上的回归效果
[py,mse,devalue] = libsvmpredict(y,x,model);
figure;
plot(x,y,'o');
hold on;
plot(x,py,'r*');
legend('原始数据','回归数据');
grid on;

% 进行预测
testx = [1.1,1.2,1.3]';
display('真实数据')
testy = -testx.^2

[ptesty,tmse,detesvalue] = libsvmpredict(testy,testx,model);
display('预测数据');
ptesty

  

原文地址:https://www.cnblogs.com/elpsycongroo/p/7219911.html