问题描述
我必须在数字数据集上使用SVM分类器.数据集由数字28x28的图像和总共2000张图像组成.我尝试使用svmtrain,但是matlab给出了svmtrain已被删除的错误.所以现在我正在使用fitcsvm.
I have to use SVM classifier on digits dataset. The dataset consists of images of digits 28x28 and a toal of 2000 images.I tried to use svmtrain but the matlab gave an error that svmtrain has been removed. so now i am using fitcsvm.
我的代码如下:
labelData = zeros(2000,1);
for i=1:1000
labelData(i,1)=1;
end
for j=1001:2000
labelData(j,1)=1;
end
SVMStruct =fitcsvm(trainingData,labelData)
%where training data is the set of images of digits.
我需要知道如何使用svm预测测试数据的输出?另外我的代码正确吗?
I need to know how i can predict the outputs of test data using svm? Further is my code correct?
推荐答案
您要查找的功能是 predict
.它以SVM对象作为输入,后跟一个数据矩阵,并返回预测的标签.确保不对所有数据而是对合理的子集(通常为70%)训练模型.您可以使用交叉验证的准备工作:
The function that you are looking for is predict
. It takes the SVM-object as input followed by a data-matrix and returns the predicted labels.Make sure that you do not train your model on all data but on a reasonable subset (usually 70%). You can use the cross-validation preparation:
% create cross-validation object
cvp = cvpartition(Lbl,'HoldOut',0.3);
% extract logical vectors for training and testing data
lgTrn = cvp.training;
lgTst = cvp.test;
% train SVM
mdl = fitcsvm(Dat(lgTrn,:),Lbl(lgTrn));
% test / predict SVM
Lbl_prd = predict(mdl,Dat(lgTst,:));
请注意,您的标签会产生一个由1构成的向量.
Note that your labeling produces a single vector of ones.
The Mathworks将svmtrain
更改为fitcsvm
的原因很简洁.现在很明显是分类"(fit c svm)还是回归"(fit r svm).
The reason why The Mathworks changed svmtrain
to fitcsvm
is conciseness. It is now clear whether it is "classification" (fitcsvm) or "regression" (fitrsvm).
这篇关于Matlab中的数字图像集上的SVM分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!