How to manually calculate the score values for Gaussian SVM?
2 次查看(过去 30 天)
显示 更早的评论
I have trained an SVM using the following hyperparameters:
where data_pt are NP by 2 training data points and data_val contains a column vector of 1 or 0. I am trying to construct a score function to calculate the prediction score for a new observation. As it mentioned in the documentation, "you must first apply any transformations to the predictor data that were applied during training. Specifically, if you specify 'Standardize',true when using fitcsvm, then you must standardize the predictor data manually by using the mean SVMModel.Mu and standard deviation SVMModel.Sigma, and then divide the result by the kernel scale in SVMModel.KernelParameters.Scale", I belive I did that. However, the output value I get are just the basis for any input prediction X. Does anyone have any idea what I did wrong? Thank you so much!
Here is my code:
function [s,dsdx,dsdy] = myscore_gaussian(x,Mdl)
% output:
% s: the manual calculation of prediction score for the predictor data x
% (scalar)
% inputs:
% 1. x: (1 by 2 vector) the x and y coordinates of the predictor data,
% must within the trained x and y limits
% 2. Mdl: the trained SVM model
% s(x) = sum(from i = 1 to NS) alpha_i y_i K(x_i,x) + b
% Get parameters for the trained Mdl
kscale = Mdl.KernelParameters.Scale;
alpha = Mdl.Alpha;
bias = Mdl.Bias;
sv = Mdl.SupportVectors;
[NS,~] = size(sv);
mu = Mdl.Mu;
sigma = Mdl.Sigma;
svclass = Mdl.SupportVectorLabels;
% standarize the input predictor x
xd = (x - mu)./sigma/kscale;
s = 0;
dsdx = 0;
dsdy = 0;
for i = 1:NS
svi = sv(i,:);
alphai = alpha(i);
yi = svclass(i);
temp = -((svi(1)-xd(1))^2 + (svi(2)-xd(2))^2);
ki = exp(temp);
tempdx = 2*(svi(1)-xd(1));
tempdy = 2*(svi(2)-xd(2));
s = s + alphai*yi*ki;
dsdx = dsdx + alphai*yi*ki*tempdx;
dsdy = dsdy + alphai*yi*ki*tempdy;
end
s = s + bias;
0 个评论
回答(1 个)
Vanshika Vaishnav
2023-3-7
If SVMModel.KernelParameters.Function is 'linear', then the classification score for the observation x is
f(x)=(x/s)′β+b.
SVMModel stores β, b, and s in the properties Beta, Bias, and KernelParameters.Scale, respectively.
For more information, refer the below documentation:-
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!