Equations for predicting outputs under SVM regression (RBF or polynomial)
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
minhyuk jeung
il 5 Giu 2024
Modificato: Angelo Yeo
il 28 Giu 2024
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.
0 Commenti
Risposta accettata
Angelo Yeo
il 6 Giu 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)]);
disp(['Predicted output using predict function: ', num2str(res_predict)]);
2 Commenti
Più risposte (1)
Angelo Yeo
il 28 Giu 2024
Modificato: Angelo Yeo
il 28 Giu 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)]);
disp(['Predicted output using predict function: ', num2str(res_predict)]);
0 Commenti
Vedere anche
Categorie
Scopri di più su Regression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!