How to predict responses of new data from linear SVM model?
1 view (last 30 days)
Show older comments
minhyuk jeung
on 21 May 2024
Commented: minhyuk jeung
on 27 May 2024
Hello, I am trying to calculate new predictions with trained linear SVM regression (4 input variables and 1 output).

from this equation, I tried:
x_new = [8.9 10.2 42.8 44.8]; % New input variables (4 variables)
supportVectors = Mdl.SupportVectors;
alpha = Mdl.Alpha;
bias = Mdl.Bias;
w = (alpha .* supportVectors)';
prediction = (w .* x_new) + bias;
-------------------------------------------------------------------
However, when I compare the results of the code below, there's a different value:
prediction = Mdl.predict(x_new);
If there is a code that calculates the output value when a new observation comes in through the trained SVM, please help me.
0 Comments
Accepted Answer
Angelo Yeo
on 21 May 2024
Edited: Angelo Yeo
on 27 May 2024
Here is the example I cooked up for you. I assumed that you would want to use a linear kernel. For more information on manual prediction, see the doc below.
%% Setup
% I will generate random data for two groups.
X = randn(100, 1) * 1 + 9; % It's just my random guess.
X = [X; randn(100, 1) * 1 + 43]; % It's just my random guess.
Y = [ones(100,1)*0; ones(100,1)*1];
Mdl = fitcsvm(X, Y, "KernelFunction", "linear", "Standardize", true);
%% prediction test
x_new = [8.9 10.2 42.8 44.8]; % New input variables (4 variables)
% We need to normalize the new input for manual prediction
x_new_norm = (x_new - Mdl.Mu) / Mdl.Sigma;
supportVectors = Mdl.SupportVectors;
alpha = Mdl.Alpha;
bias = Mdl.Bias;
% w = (alpha .* supportVectors)';
s = Mdl.KernelParameters.Scale;
beta = Mdl.Beta;
prediction = (x_new_norm/s)'*beta + bias;
prediction_label = prediction > 0
6 Comments
Angelo Yeo
on 27 May 2024
안녕하세요. 아래와 같이 진행해보시면 좋을 것 같습니다.
inputdata = readmatrix('inputdata.xlsx', Sheet = "Sheet1");
outputdata = readmatrix('outputdata.xlsx', Sheet = "Sheet1");
predictor = inputdata(1:100,1:4); % 4 input variable
target_variable = outputdata(1:100,1); % 1 output variable
X = predictor;
Y = target_variable;
Mdl = fitrsvm(X,Y,"Standardize",true);
pred_val = predict(Mdl, X);
%% For the new prediction
x_new = [8.9 10.2 42.8 44.8]; % New input variables (4 variables)
s = Mdl.KernelParameters.Scale;
beta = Mdl.Beta;
bias = Mdl.Bias;
x_new_norm = (x_new - Mdl.Mu) ./ Mdl.Sigma ;
x_new_norm/s * beta + bias % compare this and the predict value below
Mdl.predict(x_new)
SVR에 대한 자세한 설명은 아래 문서에서도 확인하실 수 있습니다.
More Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!