2016-04-15 184 views
0

有人可以幫我用「sequentialfs」嗎?在Matlab中使用支持向量機與支持向量機

我無法使用它與以下SVM功能。 'X'包含每個觀測的特徵,'y'包含每個觀測的類別。

SVMModel = fitcsvm(X,Y); 
predict(SVMModel, X); 

當執行sequentialfs,會出現以下錯誤:

函數 'featureSelection' 生成以下錯誤: 太多輸入參數。

在這裏,我的代碼:

fs = sequentialfs(@featureSelection,X,y) 

function err=featureSelection(X,y) 
    SVMModel = fitcsvm(X,y,'KernelFunction','gaussian', 'KernelScale','auto'); 
    err = 0; 
    for i=1:size(X,1) 
     err = err + (y(i) ~= predict(SVMModel,X(i,:))); 
    end 
end 

謝謝!

回答

1

我有同樣的問題。根據MATLAB的文檔:

sequentialfs performs 10-fold cross-validation by repeatedly calling fun with different training subsets of X and y, XTRAIN and ytrain, and test subsets of X and y, XTEST and ytest, as follows:

criterion = fun(XTRAIN,ytrain,XTEST,ytest)

這意味着,你的標準功能,應遵循以下形式:

function err=featureSelection(XTRAIN,ytrain,XTEST,yTest) 

只要sequentialfs會默認你的X數據分開來XTRAIN和XTEST子集。

下面是一個例子:

c = cvpartition(Labels,'Holdout',0.3); 
opts = statset('display','iter'); 
classf = @(train_data,train_labels,test_data,test_labels) ... 
     sum(predict(fitcsvm(train_data,train_labels,'KernelFunction','rbf'), test_data) ~= test_labels); 
[fs,history] = sequentialfs(classf,Data,Labels,'cv',c,'options',opts)