필터 지우기
필터 지우기

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

조회 수: 15 (최근 30일)
minhyuk jeung
minhyuk jeung 2024년 5월 21일
댓글: minhyuk jeung 2024년 5월 27일 1:13
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일 0:14
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일 0:18
안녕하세요. 아래와 같이 진행해보시면 좋을 것 같습니다.
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일 1:13
위 코드로 실행해보니 해결되었습니다.
감사합니다!! ㅠ

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by