Open In App

Support Vector Regression (SVR) using Linear and Non-Linear Kernels in Scikit Learn

Support vector regression (SVR) is a type of support vector machine (SVM) that is used for regression tasks. It tries to find a function that best predicts the continuous output value for a given input value.

SVR can use both linear and non-linear kernels. A linear kernel is a simple dot product between two input vectors, while a non-linear kernel is a more complex function that can capture more intricate patterns in the data. The choice of kernel depends on the data’s characteristics and the task’s complexity.



In scikit-learn package for Python, you can use the SVR’ class to perform SVR with a linear or non-linear kernel’. To specify the kernel, you can set the kernel parameter to ‘linear’ or ‘RBF’ (radial basis function).

Concepts related to the Support vector regression (SVR):

There are several concepts related to support vector regression (SVR) that you may want to understand in order to use it effectively. Here are a few of the most important ones:



Fitting an SVR Model on the Sine Curve data using Linear Kernel

First, we will try to achieve some baseline results using the linear kernel on a non-linear dataset and we will try to observe up to what extent it can be fitted by the model.




import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR
  
# generate synthetic data
X = np.sort(5 * np.random.rand(40, 1),
            axis=0)
y = np.sin(X).ravel()
  
# add some noise to the data
y[::5] += 3 * (0.5 - np.random.rand(8))
  
# create an SVR model with a linear kernel
svr = SVR(kernel='linear')
  
# train the model on the data
svr.fit(X, y)
  
# make predictions on the data
y_pred = svr.predict(X)
  
# plot the predicted values against the true values
plt.scatter(X, y, color='darkorange',
            label='data')
plt.plot(X, y_pred, color='cornflowerblue',
         label='prediction')
plt.legend()
plt.show()

Output:

Model fitted using Linear kernel

Fitting an SVR Model on the Sine Curve data using Polynomial Kernel

Now we will fit a Support vector Regression model using a polynomial kernel. This will be hopefully a little better than the SVR model with a linear kernel.




import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR
  
# generate synthetic data
X = np.sort(5 * np.random.rand(40, 1), axis=0)
y = np.sin(X).ravel()
  
# add some noise to the data
y[::5] += 3 * (0.5 - np.random.rand(8))
  
# create an SVR model with a linear kernel
svr = SVR(kernel='poly')
  
# train the model on the data
svr.fit(X, y)
  
# make predictions on the data
y_pred = svr.predict(X)
  
# plot the predicted values against the true values
plt.scatter(X, y, color='darkorange',
            label='data')
plt.plot(X, y_pred, color='cornflowerblue',
         label='prediction')
plt.legend()
plt.show()

Output:

Model fitted using a polynomial kernel

Fitting an SVR Model on the Sine Curve data using RBF Kernel

Now we will fit a Support vector Regression model using an RBF(Radial Basis Function) kernel. This will help us to achieve probably the best results as the RBF kernel is one of the best kernels which helps us to introduce non-linearity in our model.




import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR
  
# generate synthetic data
X = np.sort(5 * np.random.rand(40, 1),
            axis=0)
y = np.sin(X).ravel()
  
# add some noise to the data
y[::5] += 3 * (0.5 - np.random.rand(8))
  
# create an SVR model with a linear kernel
svr = SVR(kernel='rbf')
  
# train the model on the data
svr.fit(X, y)
  
# make predictions on the data
y_pred = svr.predict(X)
  
# plot the predicted values against the true values
plt.scatter(X, y, color='darkorange',
            label='data')
plt.plot(X, y_pred, color='cornflowerblue',
         label='prediction')
plt.legend()
plt.show()

Output:

Model fitted using RBF kernel


Article Tags :