如何修复fisheriris交叉分类

时间:2014-08-09 20:19:56

标签: matlab

我试图在网上运行此代码,但它不起作用。错误是

Error using svmclassify (line 53)
The first input should be a `struct` generated by `SVMTRAIN`.

Error in fisheriris_classification (line 27)
pred = svmclassify(svmModel, meas(testIdx,:), 'Showplot',false);

任何人都可以帮我解决这个问题吗?非常感谢你!

clear all;
close all;
load fisheriris                              %# load iris dataset
groups = ismember(species,'setosa');         %# create a two-class problem

%# 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.
k=10;

cvFolds = crossvalind('Kfold', groups, k);   %# get indices of 10-fold CV
cp = classperf(groups);                      %# init performance tracker

for i = 1:k                                  %# for each fold
    testIdx = (cvFolds == i);                %# get indices of test instances
    trainIdx = ~testIdx;                     %# get indices training instances

    %# train an SVM model over training instances
    svmModel = svmtrain(meas(trainIdx,:), groups(trainIdx), ...
                 'Autoscale',true, 'Showplot',false, 'Method','QP', ...
                 'BoxConstraint',2e-1, 'Kernel_Function','rbf', 'RBF_Sigma',1);

    %# test using test instances
    pred = svmclassify(svmModel, meas(testIdx,:), 'Showplot',false);

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

%# get accuracy
cp.CorrectRate

%# get confusion matrix
%# columns:actual, rows:predicted, last-row: unclassified instances
cp.CountingMatrix
%with the output:

%ans =
%      0.99333
%ans =
%   100     1
%     0    49
%     0     0

1 个答案:

答案 0 :(得分:0)

问题的原因在于我认为MATLAB在搜索路径上找到函数的方式。我相当肯定它仍在尝试使用LIBSVM函数而不是内置的MATLAB函数。以下是有关搜索路径的更多信息:

http://www.mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html

要验证是否存在此问题,请在命令窗口中尝试以下命令:

>> which -all svmtrain

你会发现内置函数被LIBSVM函数遮蔽了。您可以使用" Set Path"从MATLAB搜索路径中删除LIBSVM。 Toolstrip中的工具,或者从不包含LIBSVM文件的其他目录运行代码。我会推荐第一个选项。要阅读有关内置MATLAB函数的更多信息,请查看以下链接:

http://www.mathworks.com/help/stats/svmtrain.html

http://www.mathworks.com/help/stats/svmclassify.html

如果您想继续使用LIBSVM,我建议您查看以下网站。

https://www.csie.ntu.edu.tw/~cjlin/index.html 希望这会有所帮助。