David Hoareau
David Hoareau

Reputation: 53

Issue with Scikit Learn Package for SVR Regression

I am trying to fit a SVM regression model using Scikit Learn Package but it is not working like I am expecting.

Could you please help me to find the error? The code that I would like to use is:

from sklearn.svm import SVR
import numpy as np


X = []
x = np.arange(0, 20)
y = [3, 4, 8, 4, 6, 9, 8, 12, 15, 26, 35, 40, 45, 54, 49, 59, 60, 62, 63, 68]
X.append(x)

clf = SVR(verbose=1)
clf.fit(np.transpose(X), y)

print("Expecting Result:")
print(y)
print("Predicted Result:")
print(clf.predict(np.transpose(X)))

The Output that I have is:

[LibSVM]*
optimization finished, #iter = 10
obj = -421.488272, rho = -30.500000
nSV = 20, nBSV = 20
Expecting Result:
[3, 4, 8, 4, 6, 9, 8, 12, 15, 26, 35, 40, 45, 54, 49, 59, 60, 62, 63, 68]
Predicted Result:
[ 29.1136814   28.74580196  28.72748632  28.72736291  28.7273628
  28.7273628   28.72736302  28.72760984  28.76424112  29.5         31.5
  32.23575888  32.27239016  32.27263698  32.2726372   32.2726372
  32.27263709  32.27251368  32.25419804  31.8863186 ]

We can see that the predicted results are very far from the training data. How can I improve the fitting?

Thanks

David

Upvotes: 2

Views: 1005

Answers (1)

FoxRocks
FoxRocks

Reputation: 68

This is an edge case where RBF (default for SVM on scikit-learn) kernels don't work very well.

Change the SVR line to this: clf = SVR(verbose=1, kernel='linear') and you will see much more reasonable results.

[LibSVM]Expecting Result: [3, 4, 8, 4, 6, 9, 8, 12, 15, 26, 35, 40, 45, 54, 49, 59, 60, 62, 63, 68] Predicted Result: [ -6.9 -2.9 1.1 5.1 9.1 13.1 17.1 21.1 25.1 29.1 33.1 37.1 41.1 45.1 49.1 53.1 57.1 61.1 65.1 69.1]

I understand that you are just trying to get a feel for how SVM's work. Take a look at this blog post for how RBF kernels work.

Upvotes: 2

Related Questions