1

I'm trying to replicate scikit-learn's svm.svr.predict(X) and don't know how to do it correctly.

I want to do is, because after training the SVM with an RBF kernel I would like to implement the prediction on another programming language (Java) and I would need to be able to export the model's parameters to be able to perform predictions of unknown cases.

On scikit's documentation page, I see that there are 'support_ and 'support_vectors_ attributes, but don't understand how to replicate the .predict(X) method.

A solution of the form y_pred = f(X,svm.svr.support_, svm.svr.support_vectors_,etc,...) is what I am looking for.

Thank you in advance!

Edit: Its SVM for REGRESSION, not CLASSIFICATION!

Edit: This is the code I am trying now, from Calculating decision function of SVM manually with no success...

from sklearn import svm
import math
import numpy as np

X = [[0, 0], [1, 1], [1,2], [1,2]]
y = [0, 1, 1, 1]
clf = svm.SVR(gamma=1e-3)
clf.fit(X, y)
Xtest = [0,0]
print 'clf.decision_function:'
print clf.decision_function(Xtest)

sup_vecs = clf.support_vectors_
dual_coefs = clf.dual_coef_
gamma = clf.gamma
intercept = clf.intercept_

diff = sup_vecs - Xtest

# Vectorized method
norm2 = np.array([np.linalg.norm(diff[n, :]) for n in range(np.shape(sup_vecs)[0])])
dec_func_vec = -1 * (dual_coefs.dot(np.exp(-gamma*(norm2**2))) - intercept)
print 'decision_function replication:'
print dec_func_vec

The results I'm getting are different for both methods, WHY??

clf.decision_function:
[[ 0.89500898]]
decision_function replication:
[ 0.89900498]
Community
  • 1
  • 1
learn2day
  • 1,566
  • 1
  • 14
  • 17
  • Have a look at this answer: http://stackoverflow.com/questions/28503932/calculating-decision-function-of-svm-manually?noredirect=1#comment45487831_28503932 – Andreas Mueller Feb 19 '15 at 03:57
  • Thank you @AndreasMueller, but that post is about a Classifier, and I'm talking about Regression here, is it the same calculation? – learn2day Feb 19 '15 at 14:49
  • yes, it is. Only the loss is different. – Andreas Mueller Feb 20 '15 at 18:38
  • Hi, thank you @AndreasMueller again for your help. I copied the vectorized code in the post you told me and was able to replicate an example of Classification, but failed for Regression. (the same code in both but `clf = svm.SVR()` instead of `svm.SVC()`) I'm using the vectorized method from @user1182556 of the post you linked. I would really appreciate help with this, thank you!! – learn2day Mar 05 '15 at 20:29
  • What is the error / problem? – Andreas Mueller Mar 05 '15 at 23:03
  • @AndreasMueller I'm getting different results comparing `clf.decision_function(Xtest)` with `dec_func_vec` – learn2day Mar 06 '15 at 15:44
  • Feel free to open an issue. There should be a test reproducing the predict function. – Andreas Mueller Mar 08 '15 at 21:33
  • How do you know if sup_vecs and Xtest have the same dimensions? – Riley Aug 17 '18 at 06:31

1 Answers1

1

Thanks to the contribution of B@rmaley.exe, I found the way to replicate SVM manually. I had to replace

    dec_func_vec = -1 * (dual_coefs.dot(np.exp(-gamma*(norm2**2))) - intercept)

with

    dec_func_vec = (dual_coefs.dot(np.exp(-gamma*(norm2**2))) + intercept)

So, the full vectorized method is:

    # Vectorized method
    norm2 = np.array([np.linalg.norm(diff[n, :]) for n in range(np.shape(sup_vecs)[0])])
    dec_func_vec = -1 * (dual_coefs.dot(np.exp(-gamma*(norm2**2))) - intercept)
learn2day
  • 1,566
  • 1
  • 14
  • 17