Alternative way to use fitcsvm for code generation
3 次查看(过去 30 天)
显示 更早的评论
how to use alternate syntax if fitcsvm is not supported for code generation
2 个评论
Manikanta Aditya
2024-3-5
Hey,
The 'fitcsvm' function in MATLAB is used for training a support vector machine (SVM) for binary classification. However, if 'fitcsvm' is not supported for code generation, you can use the lower-level functions that fitcsvm uses internally.
Here’s an example of how you can do this:
% Assume X is your N-by-P matrix of predictors, and Y is your N-by-1 vector of responses
X = 10; % your predictor matrix
Y = 20; % your response vector
% Convert the responses to +1 and -1 for the SVM
Y(Y == 0) = -1;
% Set up the SVM problem
H = 0.5 * (X' * X);
f = -Y;
A = -diag(Y) * X;
b = -ones(size(Y));
lb = zeros(size(X, 2), 1);
% Solve the SVM problem using quadprog
alpha = quadprog(H, f, A, b, [], [], lb);
% Compute the bias term
bias = mean(Y - X * (X' * alpha));
% Now you can classify new data using sign(Xnew * (X' * alpha) + bias)
This code does essentially the same thing as fitcsvm, but uses 'quadprog' to solve the SVM problem directly.
Thanks!
回答(1 个)
Suman
2024-9-29
编辑:Suman
2024-9-29
Hi Keshav,
You can try to use the "coder.extrinsic" function to use functions that are not supported for code generation. Please refer to this documentation to learn how to use it and the limitations associated with it: https://www.mathworks.com/help/coder/ref/coder.extrinsic.html
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!