【发布时间】: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