2012-03-30 40 views
3

我想爲c-SVM分類選擇參數c和gamma,使用 RBF(徑向基函數)內核與libsvm \ tools \ grid.py,但我不知道它有多可能?我安裝了libsvm和gnuplot和python,並運行python中的grid.py,但它有錯誤,並沒有顯示結果。我如何使用grid.py進行參數選擇?

回答

12
%grid of parameters 
folds = 5; 
[C,gamma] = meshgrid(-5:2:15, -15:2:3); 
%# grid search, and cross-validation 
cv_acc = zeros(numel(C),1); 
d= 2; 
for i=1:numel(C) 
    cv_acc(i) = svmtrain(TrainLabel,TrainVec, ...   
     sprintf('-c %f -g %f -v %d -t %d', 2^C(i), 2^gamma(i), folds,d)); 
end 
%# pair (C,gamma) with best accuracy 
[~,idx] = max(cv_acc); 
%# contour plot of paramter selection 
contour(C, gamma, reshape(cv_acc,size(C))), colorbar 
hold on; 
text(C(idx), gamma(idx), sprintf('Acc = %.2f %%',cv_acc(idx)), ... 
    'HorizontalAlign','left', 'VerticalAlign','top') 
hold off 
xlabel('log_2(C)'), ylabel('log_2(\gamma)'), title('Cross-Validation Accuracy') 
%# now you can train you model using best_C and best_gamma 
best_C = 2^C(idx); best_gamma = 2^gamma(idx); %# ... 

此執行網格搜索,以及...但使用MATLAB ...不使用grid.py ...也許這有助於...

+0

對不起,我不明白功能svmtrain去的返回值。它應該是一個返回的模型。爲什麼你把它當作一個準確性和準確性呢?謝謝! – 2015-04-22 08:54:15

+0

@lakesh:在這裏,對於這個網格搜索和選擇參數,它需要將所有數據(訓練和測試)作爲輸入提供給'svmtrain',還是有另一種方法來處理數據? – Amin 2017-08-09 13:43:53

6

你可以使用所提供的,而不是電網的MATLAB腳本.py常問問題

問:我怎樣才能使用MATLAB接口進行參數選擇? http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f803

bestcv = 0; 
for log2c = -1:3, 
    for log2g = -4:1, 
    cmd = ['-v 5 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g)]; 
    cv = svmtrain(heart_scale_label, heart_scale_inst, cmd); 
    if (cv >= bestcv), 
     bestcv = cv; bestc = 2^log2c; bestg = 2^log2g; 
    end 
    fprintf('%g %g %g (best c=%g, g=%g, rate=%g)\n', log2c, log2g, cv, bestc, bestg, bestcv); 
    end 
end