【问题标题】:How to fix the fisheriris cross classification如何修复fisheriris交叉分类
【发布时间】:2014-10-03 01:32:59
【问题描述】:

我尝试运行在网上找到的这段代码,但它不起作用。错误是

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

【问题讨论】:

  • 如果svmModel 不是struct,那么它是什么?你能在命令窗口中输入whos svmModel并将输出粘贴到这里吗?
  • >> whos svmModel Name Size Bytes Class Attributes svmModel 0x0 0 double 这是结果
  • 这意味着它是一个空矩阵,并且命令 svmtrain 根本不起作用。你可以试试svmModel = svmtrain(meas(trainIdx,:), groups(trainIdx));
  • 另外,请确保您调用了 MATLAB 的 svmtrain 命令。如果你有libsvm,它将调用libsvm 的svmtrain,但在这种情况下你会得到一个错误。所以我猜你只是在调用 MATLAB 的命令。你能调试代码并“进入”命令svmtrain
  • svmModel = svmtrain(meas(trainIdx,:), groups(trainIdx));错误:标签向量和实例矩阵必须是双精度的

标签: matlab


【解决方案1】:

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

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

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

>> which -all svmtrain

您应该会发现内置函数被 LIBSVM 函数所遮蔽。您可以使用工具条中的“设置路径”工具从 MATLAB 搜索路径中删除 LIBSVM,或者从不包含 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 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 2016-04-19
    • 2020-01-03
    • 2021-07-17
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多