1
votes

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]
1
Thank you @AndreasMueller, but that post is about a Classifier, and I'm talking about Regression here, is it the same calculation?learn2day
yes, it is. Only the loss is different.Andreas Mueller
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
What is the error / problem?Andreas Mueller

1 Answers

1
votes

Thanks to the contribution of [email protected], 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)