MATLAB Answers

How to correct the error - ClassificationSVM

82 views (last 30 days)
My Matlab code -
clear
load fisheriris
% Only use the third and fourth features
x=meas(:,3:4);
gscatter(x(:,1),x(:,2),species);
% Only use the last two categories
x=meas(51:end,3:4);
group=species(51:end,1);
gscatter(x(:,1),x(:,2),group);
% Linear SVM
svmStruct = fitcsvm(x,group,'showplot',true);
% Kernel SVM
svmStruct = fitcsvm(xdata,group,'showplot',true,'kernel_function','rbf');
% Select different sigma
svmStruct = fitcsvm(xdata,group,'showplot',true,'kernel_function','rbf','rbf_sigma',0.5);
But here I get the error message such as below -
Error in fitcsvm (line 316)
obj = ClassificationSVM.fit(X,Y,RemainingArgs{:});
Error in Untitled (line 11)
svmStruct = fitcsvm(x,group,'showplot',true);

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jun 2018
Edited: Walter Roberson on 16 Jun 2018
svmStruct = fitcsvm(x,group,'HyperparameterOptimizationOptions', struct('showplot',true))
svmStruct = fitcsvm(x,group,'HyperparameterOptimizationOptions', struct('showplot',true), 'KernelFunction','rbf','KernelScale',0.5)

  1 Comment

Walter Roberson
Walter Roberson on 17 Jun 2018
ntry = 10;
kftypes = {'gaussian', 'rbf', 'polynomial'};
nkf = length(kftypes);
svmStructs = cell(ntry,1);
for idx = 1 : ntry
kfidx = randi(nkf);
kftype = kftypes{kfidx};
if ismember(kfidx, [1, 2])
ks = exp(randn());
opts = {'KernelScale', ks};
else
q = randi(20);
opts = {'PolynomialOrder', q}
end
svmStructs{idx} = fitcsvm(x, group, 'HyperparameterOptimizationOptions', struct('showplot',true), 'KernelFunction', kftype, opts{:});
disp(kftype)
celldisp(opts);
pause(2);
end

Sign in to comment.

More Answers (4)

vokoyo
vokoyo on 17 Jun 2018
Edited: vokoyo on 17 Jun 2018
Many thanks for your correct solution however can you please provide further suggestion such as how to modify the output diagram based on adjusting the parameters?
(Herewith refer to the attached file)
Thank you again

  1 Comment

Walter Roberson
Walter Roberson on 17 Jun 2018
Try different settings for the KernelFunction https://www.mathworks.com/help/stats/fitcsvm.html#bt9w6j6_sep_shared-KernelFunction and for the KernelScale and see what the effects are.

Sign in to comment.


vokoyo
vokoyo on 17 Jun 2018
Edited: vokoyo on 17 Jun 2018
Kindly please help and provide your sample codes as a reference (because this is very important for studies)
After all I am not sure how to perform Matlab programming for Supervised Classification and compare all the results
Here can contact with more detail information - tcynotebook@yahoo.com (my mail)

  1 Comment

Walter Roberson
Walter Roberson on 17 Jun 2018
Students experimenting is very important for studies.

Sign in to comment.


vokoyo
vokoyo on 18 Jun 2018
Edited: vokoyo on 18 Jun 2018
This is the Matlab code -
clear
load fisheriris
% Only use the third and fourth features
x=meas(:,3:4);
gscatter(x(:,1),x(:,2),species);
% Only use the last two categories
x=meas(51:end,3:4);
group=species(51:end,1);
gscatter(x(:,1),x(:,2),group);
% Linear SVM
svmStruct = fitcsvm(x,group,'HyperparameterOptimizationOptions', struct('showplot',true))
% Kernel SVM
svmStruct = fitcsvm(x,group,'HyperparameterOptimizationOptions', struct('showplot',true), 'KernelFunction','rbf')
% Select different sigma
svmStruct = fitcsvm(x,group,'HyperparameterOptimizationOptions', struct('showplot',true), 'KernelFunction','rbf','KernelScale',0.1)
ntry = 10;
kftypes = {'gaussian', 'rbf', 'polynomial'};
nkf = length(kftypes);
svmStructs = cell(ntry,1);
for idx = 1 : ntry
kfidx = randi(nkf);
kftype = kftypes{kfidx};
if ismember(kfidx, [1, 2])
ks = exp(randn());
opts = {'KernelScale', ks};
else
q = randi(20);
opts = {'PolynomialOrder', q}
end
svmStructs{idx} = fitcsvm(x, group, 'HyperparameterOptimizationOptions', struct('showplot',true), 'KernelFunction', kftype, opts{:});
disp(kftype)
celldisp(opts);
pause(2);
end
Why the output diagram is the same and not any special result?
(Herewith kindly refer to the attached picture)

  6 Comments

Show 3 older comments
Walter Roberson
Walter Roberson on 18 Jun 2018
"I have no time to come here every day and write one or two statements"
But you expect other people to have plenty of time to write your homework for you.
"I think you are not understand my question and problem"
I understood your question. The answer is as I posted: fitcsvm() is not plotting anything. All you are seeing is what you plot with you gscatter() calls.
If the plotting from fitcsvm() was working, then what it would be showing is progress towards finding the best fit.
vokoyo
vokoyo on 18 Jun 2018
The more you write the more problems I get
svm_3d_matlab_vis
Not enough input arguments.
Error in svm_3d_matlab_vis (line 2)
sv = svmStruct.SupportVectors;
I think I need to stop here
Anyhow thank for the first answer
Walter Roberson
Walter Roberson on 18 Jun 2018
It sounds as if you are calling svm_3d_matlab_vis without passing in any parameters.

Sign in to comment.


Don Mathis
Don Mathis on 18 Jun 2018
FITCSVM does not have an argument named 'showplot'. When I run your original code in R2018a I get this:
Error using classreg.learning.FitTemplate/fillIfNeeded (line 612)
showplot is not a valid parameter name.
Error in classreg.learning.FitTemplate.make (line 124)
temp = fillIfNeeded(temp,type);
Error in ClassificationSVM.template (line 235)
temp = classreg.learning.FitTemplate.make('SVM','type','classification',varargin{:});
Error in ClassificationSVM.fit (line 239)
temp = ClassificationSVM.template(varargin{:});
Error in fitcsvm (line 316)
obj = ClassificationSVM.fit(X,Y,RemainingArgs{:});
Error in Untitled3 (line 11)
svmStruct = fitcsvm(x,group,'showplot',true);

  3 Comments

Walter Roberson
Walter Roberson on 18 Jun 2018
svmtrain() had 'showplot' and 'rbf_sigma' -- but svmtrain() has been replaced by fitcsvm. Which unfortunately has a more confusing user interface.
Hui Theng Leong
Hui Theng Leong on 2 May 2019
I have the same error as the above, could you please provide the workaround for this?
Thank You
Walter Roberson
Walter Roberson on 2 May 2019
As I posted above, you now need to use 'Hyperparameteroptimizationoptions', struct('Showplots', true)

Sign in to comment.

Sign in to answer this question.