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.
0 件のコメント
採用された回答
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)]);
disp(['Predicted output using predict function: ', num2str(res_predict)]);
その他の回答 (1 件)
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)]);
disp(['Predicted output using predict function: ', num2str(res_predict)]);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Regression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!