Clear Filters
Clear Filters

How to solve error when using fitcsvm to replace svmtrain ?

3 views (last 30 days)
There is error, when I try to replace svmtrain using fitcsvm as follow, anyone know how to solve it?
model = svmtrain(labelVecBinTrain,featureMatTrain,['-t 0 -b 1 -w1 1 ' '-w-1 ' num2str(unbalanced_weight) ]);
model = fitcsvm(labelVecBinTrain,featureMatTrain,['-t 0 -b 1 -w1 1 ' '-w-1 ' num2str(unbalanced_weight) ]);
Error using internal.stats.parseArgs
(line 42)
Wrong number of arguments.
Error in
classreg.learning.paramoptim.parseOptimizationArgs
(line 10)
[OptimizeHyperparameters,HyperparameterOptimizationOptions,~,RemainingArgs]
= internal.stats.parseArgs(...
Error in fitcsvm (line 339)
[IsOptimizing, RemainingArgs] =
classreg.learning.paramoptim.parseOptimizationArgs(varargin);
Error in mainAuthentication (line
224)
model =
fitcsvm(labelVecBinTrain,featureMatTrain,['-t
0 -b 1 -w1 1 ' '-w-1 '
num2str(unbalanced_weight) ]);

Answers (1)

Vatsal
Vatsal on 12 Jun 2024
Hi,
The error you are encountering is because 'fitcsvm' in MATLAB uses a different syntax for specifying options compared to the older 'svmtrain' function. The 'svmtrain' function accepts a string of options (like '-t 0 -b 1 -w1 1 -w-1 X'), but 'fitcsvm' uses name-value pair arguments for its options.
Here is how code can be adjusted to use 'fitcsvm':
% Define the weights
weights = ones(size(labelVecBinTrain));
weights(labelVecBinTrain == -1) = unbalanced_weight;
% Define the SVM model
model = fitcsvm(featureMatTrain, labelVecBinTrain, 'KernelFunction', 'linear', 'BoxConstraint', 1, 'Weights', weights);
In this code:
  • 'KernelFunction', 'linear' is equivalent to -t 0 in 'svmtrain'.
  • 'BoxConstraint', 1 is equivalent to -c 1 in 'svmtrain'.
  • 'Weights', weights is used to handle the unbalanced data, which is equivalent to -w1 1 -w-1 unbalanced_weight in 'svmtrain'.
Please note that 'fitcsvm' does not directly support the -b 1 option from 'svmtrain' which is for probability estimates. If you need probability estimates, predict function can be used with 'Probability',true option after training the model.
For more information on “fitcsvm”, the following resources may be helpful:
I hope this helps!

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!