MATLAB: How to add the C parameter as a single value in fitcsvm

fitcsvmsvm

According to the documentation, fitcsvm require the Cost parameter to be a 2×2 matrix (or a structure), specifying the cost for each classification. I've never seen it written this way before, svmtrain and Scikit-learn's SVM classifier have the input be a single value.
For example using the libsvm package, I could enter the parameters as
model = svmtrain(y, X, sprintf('-m 3000 -t 0 -c %f -q', C))
where C = 0.1.
As a result, I'm unsure how to modify my code to work this way.
My thinking right now is if I want to use C = 0.1, I could specify the matrix as [0, C; C 0], such as
model = fitcsvm(y, X, 'KernelFunction', 'linear', 'CacheSize', 3000, 'Cost', [0, C; C, 0])
Would that be the same as adding C=0.1 in svmtrain?

Best Answer

The 'Cost' parameter in fitcsvm is usually used to add weights to imbalanced classes, not modifying the constraint on the hyperplane, which is what you're doing in svmtrain.
In the fitcsvm function, the constraint on the hyperplane is called the 'BoxConstraint' and can easily be added the same way as
model = fitcsvm(X, y, 'KernelFunction', 'linear', 'CacheSize', 3000, 'BoxConstraint', C)
However, if you're using the libsvm package, you can still use svmtrain and svmpredict in the newer releases of MATLAB by simply re-making the package in MATLAB. Either method works and I ended up doing the latter.
Related Question