Clear Filters
Clear Filters

Equations for predicting outputs under SVM regression (RBF or polynomial)

28 views (last 30 days)
Hello everyone,
I am trying to calculate output (Y) for new input data (X) using a pretrained SVM (trained with kernel function RBF or polynomial).
I know the equations for linear SVM regression:
ex) Y = inputdata * Beta + Bias
However, I am trying to find the equations to calculate the Y response under 'RBF' or 'Polynomial' SVM regression.
Please help
Thank you.

Accepted Answer

Angelo Yeo
Angelo Yeo on 6 Jun 2024
Here is one for RBF. This will be good reference for polynomial kernel.
X = readmatrix('inputdata.xlsx', Sheet = "Sheet1");
Y = readmatrix('outputdata.xlsx', Sheet = "Sheet1");
Mdl = fitrsvm(X,Y,"Standardize", true, "KernelFunction", "rbf", "KernelScale", "auto");
% pred_val = predict(Mdl, X);
%% For the new prediction
x_new = [8.9 10.2 42.8 44.8]; % New input variables (4 variables)
alpha = Mdl.Alpha;
bias = Mdl.Bias;
kernelScale = Mdl.KernelParameters.Scale;
supportVectors = Mdl.SupportVectors;
% standardize input
x_new_norm = (x_new - Mdl.Mu) ./ Mdl.Sigma ;
% scaled Gram matrix
d = (x_new_norm - supportVectors)/kernelScale;
euc_dist_squared = sum(d.^2,2); % Squared Euclidean distance
G = exp(-euc_dist_squared);
% responses
res_direct = sum(alpha .* G) + bias;
res_predict = Mdl.predict(x_new);
% Display the result
disp(['Predicted output using direct calculation: ', num2str(res_direct)]);
Predicted output using direct calculation: 0.11385
disp(['Predicted output using predict function: ', num2str(res_predict)]);
Predicted output using predict function: 0.11385

More Answers (1)

Angelo Yeo
Angelo Yeo on 28 Jun 2024 at 0:19
Edited: Angelo Yeo on 28 Jun 2024 at 0:53
Here is one for polynomial kernel.
X = readmatrix('inputdata.xlsx', Sheet="Sheet1");
Y = readmatrix('outputdata.xlsx', Sheet="Sheet1");
% Polynomial Kernel's order
PolynomialOrder = 2;
% Check if polynomial order is positive integer
if PolynomialOrder<=0 || fix(PolynomialOrder)~=PolynomialOrder
error("PolynomialOrder must be a positive integer");
end
Mdl = fitrsvm(X, Y, "Standardize", true, "KernelFunction", "polynomial", "PolynomialOrder", PolynomialOrder);
% For the new prediction
x_new = [8.9 10.2 42.8 44.8];
alpha = Mdl.Alpha;
bias = Mdl.Bias;
supportVectors = Mdl.SupportVectors;
% Standardize input
x_new_norm = (x_new - Mdl.Mu) ./ Mdl.Sigma;
% Gram matrix
G = (supportVectors * x_new_norm' + 1).^PolynomialOrder;
% responses
res_direct = sum(alpha .* G) + bias;
res_predict = Mdl.predict(x_new);
disp(['Predicted output using direct calculation: ', num2str(res_direct)]);
Predicted output using direct calculation: 0.18437
disp(['Predicted output using predict function: ', num2str(res_predict)]);
Predicted output using predict function: 0.18437

Community Treasure Hunt

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

Start Hunting!