2013-05-06 108 views
0

我想在MATLAB中得到一個預測列矩陣,但我不知道如何去編碼它。我當前的代碼是 -如何使用MATLAB在SVM中獲得預測值?

load DataWorkspace.mat 
groups = ismember(Num,'Yes'); 
k=10; 

%# number of cross-validation folds: 
%# If you have 50 samples, divide them into 10 groups of 5 samples each, 
%# then train with 9 groups (45 samples) and test with 1 group (5 samples). 
%# This is repeated ten times, with each group used exactly once as a test set. 
%# Finally the 10 results from the folds are averaged to produce a single 
%# performance estimation. 

cvFolds = crossvalind('Kfold', groups, k); 
cp = classperf(groups); 
for i = 1:k        
    testIdx = (cvFolds == i);    
    trainIdx = ~testIdx;     
    svmModel = svmtrain(Data(trainIdx,:), groups(trainIdx), ... 
       'Autoscale',true, 'Showplot',false, 'Method','SMO', ... 
       'Kernel_Function','rbf'); 

    pred = svmclassify(svmModel, Data(testIdx,:), 'Showplot',false); 

    %# evaluate and update performance object 
    cp = classperf(cp, pred, testIdx); 
end 
cp.CorrectRate 
cp.CountingMatrix 

的問題是,它的實際計算精度共11次 - 10次,每次倍和最後一個時間的平均值。但是,如果我對每個迴路進行單獨的預測併爲每個迴路打印pred,則可以理解的精度大大降低。

但是,我需要每行數據的預測值的列矩陣。關於如何修改代碼的任何想法?

回答

1

交叉驗證的整個思想是獲得分類器性能的無偏估計。

一旦完成,您通常只是在整個數據上訓練一個模型。這個模型將被用來預測未來的情況。

所以只是做:

svmModel = svmtrain(Data, groups, ...); 
pred = svmclassify(svmModel, otherData, ...);