Matlab 如何确定Fisherris交叉分类

Matlab 如何确定Fisherris交叉分类,matlab,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); 有人能帮我解决这个问题吗?非常感谢你

我试图运行在线找到的代码,但它不起作用。错误是

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

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

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

>> which -all svmtrain
您应该发现内置函数正被LIBSVM函数遮挡。可以使用Toolstrip中的Set path工具从MATLAB搜索路径中删除LIBSVM,也可以从不包含LIBSVM文件的其他目录中运行代码。我建议第一种选择。要阅读有关内置MATLAB函数的更多信息,请查看以下链接:

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


希望这有帮助。

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