I'm working on a data, and use regression , as you see bellow:

from sklearn.svm import SVR
regressor = SVR(kernel = 'linear')

above answer is:

SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='scale',
    kernel='linear', max_iter=-1, shrinking=True, tol=0.001, verbose=False)

from sklearn.metrics import r2_score
pred = regressor.predict(testX)
SVM_R2 = print('r2= ' +str(r2_score(testY,pred)))
import matplotlib.pyplot as plt
plt.plot(testY, 'r')
plt.plot(pred,'g' ) 
plt.ylabel("pred and testY")

enter image description here

I want implement 2 changes:

  1. $R^2$ be positive

  2. $R^2$ be nearer to 1.

How could I do this?


Apart from the considerations about the quality of the data or whether or not the model is suitable for the problem, one good apporach is to try different combnations of the algorithm parameteres (using cross-validation) to come up with the best possible model.

I mean, you can do a grid search or a randomizded search to find out which combination of the regression algorihtm's parameters works better (for a SVR you have the $kernel$, $gamma$, $C$...). Fortunatelly, scikit learn has it already implemented:

There are more available methods:


The key is:

  • Telling the searching algorithm which are the target parameters
  • Teling the searching algorithm which score is used to asses which combination of parameters is the best one (it can be accuracy, mae, mse...)

A $R^2$ that is that low tells you that your model is not good. Therefore, you can both make it positive and nearer to 1 by :

a) getting better/more data, or

b) picking a better model for your data.

Also, it'd be more helpful to plot the true/pred values against the underlying $X$ values and not just as a sequence.


According to the sklearn.svm.SVR documentation, the negative $R^2$ value indicates that your model is arbitrarily worse than the trend line on trainY.

By default you should check the following:

  1. Does your model have a bias/intercept? If not you may observe negative $R^2$.
  2. Is testY derived from your training data?
  3. Am I using a linear function to fit the data? You are as you have chosen: SVR(kernel = 'linear')

If you've answered 'Yes' to each of those, you will realize that the average of testY provides a better prediction of the test data than the model fit by sklearn.svm.SVR.

Therefore, if you believe such a model exists (one that outperforms the average of testY), you'll likely find it with more/different predictor variables.


Not the answer you're looking for? Browse other questions tagged or ask your own question.