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

4 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Angelo Yeo
Angelo Yeo am 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

Weitere Antworten (1)

Angelo Yeo
Angelo Yeo am 28 Jun. 2024
Bearbeitet: Angelo Yeo am 28 Jun. 2024
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

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by