How to predict responses of new data from linear SVM model?

3 ビュー (過去 30 日間)
minhyuk jeung
minhyuk jeung 2024 年 5 月 21 日
コメント済み: minhyuk jeung 2024 年 5 月 27 日
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.

採用された回答

Angelo Yeo
Angelo Yeo 2024 年 5 月 21 日
編集済み: Angelo Yeo 2024 年 5 月 27 日
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
prediction_label = 4x1 logical array
0 0 1 1
  6 件のコメント
Angelo Yeo
Angelo Yeo 2024 年 5 月 27 日
안녕하세요. 아래와 같이 진행해보시면 좋을 것 같습니다.
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
ans = 0.0714
Mdl.predict(x_new)
ans = 0.0714
SVR에 대한 자세한 설명은 아래 문서에서도 확인하실 수 있습니다.
minhyuk jeung
minhyuk jeung 2024 年 5 月 27 日
위 코드로 실행해보니 해결되었습니다.
감사합니다!! ㅠ

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSupport Vector Machine Regression についてさらに検索

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by