问题
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.
My code is as below:
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.
I need to know how i can predict the outputs of test data using svm? Further is my code correct?
回答1:
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,:));
Note that your labeling produces a single vector of ones.
The reason why The Mathworks changed svmtrain
to fitcsvm
is conciseness. It is now clear whether it is "classification" (fitcsvm) or "regression" (fitrsvm).
来源:https://stackoverflow.com/questions/59334837/svm-classifications-on-set-of-images-of-digits-in-matlab