Simple SVM classification code

Hi,
I have Matlab2013a, and use the following code for classifying my data (it is mostly taken from http://stackoverflow.com/questions/3070789/example-of-10-fold-svm-classification-in-matlab). My feature matrix is 84x19900 (84 subjects, each subjects having a row vector with 19 900 features), and class is a 84x1 logical array (1-42 are 1s, 42-84 are 0s). Yet my code throws an error (see end):
load feature load class
k = 7; %number of folds cvFolds = crossvalind('Kfold', class, k); cp = classperf(class);
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(feature(trainIdx,:), class(trainIdx), ...
'Autoscale',true, 'Showplot',false, 'Method','QP', ...
'BoxConstraint',2e-1, 'Kernel_Function','rbf', 'RBF_Sigma',1);
%# test using test instances
pred = svmclassify(svmModel, feature(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
Error:
Error using svmclassify (line 53)
The first input should be a struct generated by SVMTRAIN.
Error in classifyAutismTD (line 18)
pred = svmclassify(svmModel, feature(testIdx,:), 'Showplot',false);
What is wrong?

4 件のコメント

Hamoon
Hamoon 2015 年 9 月 15 日
Check this code first:
feature = rand(84,19900);
class = rand(1,84)>.5;
k = 7; %number of folds
cvFolds = crossvalind('Kfold', class, k);
cp = classperf(class);
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(feature(trainIdx,:), class(trainIdx), ...
'Autoscale',true, 'Showplot',false, 'Method','QP', ...
'BoxConstraint',2e-1, 'Kernel_Function','rbf', 'RBF_Sigma',1);
%# test using test instances
pred = svmclassify(svmModel, feature(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
Are you getting the same error here? if the answer is yes, look to the variable svmModel in your workspace, what's its value?
MiauMiau
MiauMiau 2015 年 9 月 15 日
Many thanks! So, yes, I still got the error. And the problem was the following: I have libsvm in the same folder, and it looks like some functions are named the same, hence the error. Now I removed the folder from the path, and everything is fine..many thanks nevertheless!
the cyclist
the cyclist 2015 年 9 月 15 日
You might consider copying this last comment of yours into an answer, and then accepting it. This might help other people in the future with a similar problem.
Vania krm
Vania krm 2019 年 5 月 21 日
Dear @MiauMiau
I have also this error and I can not solve it. Can you explain more how I can check the libsvm folder?

サインインしてコメントする。

回答 (0 件)

カテゴリ

タグ

質問済み:

2015 年 9 月 15 日

コメント済み:

2019 年 5 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by