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

조회 수: 5 (최근 30일)
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.

채택된 답변

Angelo Yeo
Angelo Yeo 2024년 6월 6일
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

추가 답변 (1개)

Angelo Yeo
Angelo Yeo 2024년 6월 28일
편집: Angelo Yeo 2024년 6월 28일
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

카테고리

Help CenterFile Exchange에서 Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by