[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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM