How do I get the distance between the point and the hyperplane using libsvm?

4 次查看(过去 30 天)
I am using libsvm. I need to know, which observations are farest away from the hyperplane. libsvm returns me the "decision_value" but how can I use it to get the distance from the hyperplane? Taking the largest positive and smallest negative values or do I have to compute it manually and if yes, how?

采纳的回答

Rishabh Gupta
Rishabh Gupta 2018-8-2
Hey Stef
You can find the distance of a point i from hyperplane as follows:
distance_i = |decision_value|_i / |w|-b
where,
w = (alpha * support_vectors)
|w| = sqrt(sum(w^2))
alphas, support_vectors and b is generated from SVM model
  5 个评论
Stef
Stef 2018-8-2
SV_indices contrains the index of the Support vectors in the original matrix. The problem is that I want to find the 5% of observations which are most likely in the -1 category. Therefore I take the x observations which are furthest away from the hyperplane in one direction and the rest (5%-x) which are closest to the hyperplane but in class 1.
Stef
Stef 2018-8-2
编辑:Stef 2018-8-2
See here an example for the fisher Iris. Is there a possibility to find the on which side of the hyperplane the observations are?
load 'fisheriris'
% create X and y
X = meas([1:100],[3:4]);
y = grp2idx(species);
% recode 2 to -1 that lables are 1 and -1
y(y==2) = -1;
%create training and testing sample
rand = randperm(100);
y_train = y(rand([1:80]),:);
X_train = X(rand([1:80]),:);
y_test = y(rand([81:100]),:);
X_test = X(rand([81:100]),:);
% SVM
options =[ '-s 0 -t 0']
[model] = svmtrain(y_train, X_train, options)
[predict_label, accuracy, decision_values] = svmpredict(y_test, X_test, model);
% find distance
w = model.sv_coef' * model.SVs;
w_abs = sqrt(sum(w.^2));
bias = model.rho;
distance = abs(decision_values) ./ (w_abs-bias);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by