Support Vector Regression Tutorial for Machine Learning

Alakh Last Updated : 13 Nov, 2024
9 min read

Support Vector Machines (SVM) are widely used in machine learning for classification problems, but they can also be applied to regression problems through Support Vector Regression (SVR). SVR uses the same principles as SVM but focuses on predicting continuous outputs rather than classifying data points. This tutorial will explore SVR’s work, emphasizing key concepts such as quadratic, radial basis function, and sigmoid kernels. By leveraging these kernels, SVR can effectively handle complex, non-linear relationships in data. We will also demonstrate how to implement SVR in Python using training samples, showcasing its practical applications in artificial intelligence.

In this article you will get understanding about the Support Vector Regression Mdoel. So, Support vector regression (SVR) is a robust machine learning method utilized for forecasting continuous results. The SVR model, unlike typical regression models, employs support vector machines (SVMs) principles to transform input features into high-dimensional spaces to locate the ideal hyperplane that accurately represents the data. This method enables support vector regression (SVR) to effectively manage both linear and non-linear relationships, rendering it a versatile tool across different fields, such as financial forecasting and scientific research. Utilizing the distinctive features of support vector machine regression allows SVR models to attain high accuracy and robustness, even when dealing with intricate datasets.

Support Vector Regression Tutorial for Machine Learning | SVR | SVM

Learning Outcomes

  • Grasp the fundamental concepts of Support Vector Machine Regression, including hyperplanes, margins, and how SVM separates data into different classes.
  • Recognize the key differences between Support Vector Machines for classification and Support Vector Regression for regression problems.
  • Learn about important SVR hyperparameters, such as kernel types (quadratic, radial basis function, and sigmoid), and how they influence the model’s performance.
  • Gain practical experience in implementing Support Vector Regression using Python, including data preprocessing, feature scaling, and model training.
  • Use SVR to predict continuous outputs in various contexts, demonstrating its application in fields like finance, engineering, and healthcare.
  • Develop skills to visualize the results of SVM for Regression, understand how to interpret the best-fit line, and understand the impact of different kernels on the model’s predictions.
  • Learn how to assess the performance of SVR models using appropriate metrics and techniques, ensuring accurate and reliable predictions.

What is a Support Vector Machine (SVM)?

A Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. SVM works by finding a hyperplane in a high-dimensional space that best separates data into different classes. It aims to maximize the margin (the distance between the hyperplane and the nearest data points of each class) while minimizing classification errors. SVM can handle both linear and non-linear classification problems by using various kernel functions. It’s widely used in tasks such as image classification, text categorization, and more.

So what exactly is Support Vector Machine (SVM)? We’ll start by understanding SVM in simple terms. Let’s say we have a plot of two label classes as shown in the figure below:

Support Vector Machine

Can you decide what the separating line will be? You might have come up with this:

Support Vector Machine

The line fairly separates the classes. This is what SVM essentially does – simple class separation. Now, what is the data was like this:

Support Vector Machine

Here, we don’t have a simple line separating these two classes. So we’ll extend our dimension and introduce a new dimension along the z-axis. We can now separate these two classes:

Support Vector Machine

When we transform this line back to the original plane, it maps to the circular boundary as I’ve shown here:

Support Vector Machine

This is exactly what Support Vector Machine Regression does! It tries to find a line/hyperplane (in multidimensional space) that separates these two classes. Then it classifies the new point depending on whether it lies on the positive or negative side of the hyperplane depending on the classes to predict.

Also, Read about this article “A-Z guide to Support Vector Machine

Hyperparameters of the Support Vector Machine (SVM) Algorithm

There are a few important parameters of SVM that you should be aware of before proceeding further:

  • Kernel: A kernel helps us find a hyperplane in the higher dimensional space without increasing the computational cost. Usually, the computational cost will increase if the dimension of the data increases. This increase in dimension is required when we are unable to find a separating hyperplane in a given dimension and are required to move in a higher dimension:
Support Vector Machine parameters
  • Hyperplane: This is basically a separating line between two data classes in SVM. But in Support Vector Regression, this is the line that will be used to predict the continuous output
  • Decision Boundary: A decision boundary can be thought of as a demarcation line (for simplification) on one side of which lie positive examples and on the other side lie the negative examples. On this very line, the examples may be classified as either positive or negative. This same concept of SVM will be applied in Support Vector Regression as well

To understand SVM from scratch, I recommend this tutorial: How to Use Support Vector Machines (SVM) for Data Science

Introduction to Support Vector Regression (SVR)

Support Vector Regression (SVR) is a machine learning algorithm used for regression analysis. SVR Model in Machine Learning aims to find a function that approximates the relationship between the input variables and a continuous target variable while minimizing the prediction error.

Unlike Support Vector Machines (SVMs) used for classification tasks, SVR Model seeks a hyperplane that best fits the data points in a continuous space. This is achieved by mapping the input variables to a high-dimensional feature space and finding the hyperplane that maximizes the margin (distance) between the hyperplane and the closest data points, while also minimizing the prediction error.

SVR Model can handle non-linear relationships between the input and target variables by using a kernel function to map the data to a higher-dimensional space. This makes it a powerful tool for regression tasks where complex relationships may exist.

Support Vector Regression (SVR) uses the same principle as SVM but for regression problems. Let’s spend a few minutes understanding the idea behind SVR in Machine Learning.

Here top 6 Free University Courses to Learn Machine Learning

The Idea Behind Support Vector Regression

The problem of regression is to find a function that approximates mapping from an input domain to real numbers based on a training sample. So, let’s dive deep and understand how SVR actually works.

Support Vector Regression, svm regression python

Consider these two red lines as the decision boundary and the green line as the hyperplane. When we move on with SVR in Machine Learning, our objective is to consider the points within the decision boundary line. Our best fit line is the hyperplane with the maximum number of points.

The first thing that we’ll understand is what is the decision boundary (the danger red line above!). Consider these lines as being at any distance, say ‘a’, from the hyperplane. So, these are the lines that we draw at distance ‘+a’ and ‘-a’ from the hyperplane. This ‘a’ in the text is basically referred to as epsilon.

Assuming that the equation of the hyperplane is as follows:

Y = wx+b (equation of hyperplane)

Then the equations of decision boundary become:

wx+b= +a

wx+b= -a

Thus, any hyperplane that satisfies our SVM for Regression Model should satisfy:

-a < Y- wx+b < +a 

Our main aim here is to decide a decision boundary at ‘a’ distance from the original hyperplane such that data points closest to the hyperplane or the support vectors are within that boundary line.

Hence, we will take only those points within the decision boundary that have the least error rate or are within the Margin of Tolerance. This will give us a better-fitting model.

Implementing Support Vector Regression (SVR) in Python

Time to put on our coding hats! In this section, we’ll understand the use of Support Vector Regression with the help of a dataset. Here, we have to predict the salary of an employee, given a few independent variables. A classic HR analytics project!

Implementing Support Vector Regression in Python

If you looking for Guide here is Full Guide for Support Vector Machine (SVM) Algorithm

Step 1: Importing the libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Step 2: Reading the dataset

dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

Step 3: Feature Scaling

A real-world dataset contains features that vary in magnitudes, units, and range. I would suggest performing normalization when the scale of a feature is irrelevant or misleading.

Feature Scaling basically helps to normalize the data within a particular range. Normally several common class types contain the feature scaling function so that they make feature scaling automatically. However, the SVR Model in machine learning class is not a commonly used class type so we should perform feature scaling using Python.

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)

Step 4: Fitting SVR to the dataset

from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)

Kernel is the most important feature. There are many types of kernels – linear, Gaussian, etc. Each is used depending on the dataset. To learn more about this, read this: Support Vector Machine (SVM) in Python and R

Step 5. Predicting a New Result

y_pred = regressor.predict(6.5)
y_pred = sc_y.inverse_transform(y_pred) 

So, the prediction for y_pred(6, 5) will be 170,370.

Step 6. Visualizing the SVR results (for higher resolution and smoother curve)

X_grid = np.arange(min(X), max(X), 0.01) #this step required because data is feature scaled.
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
Implementing SVR in Python, svm regression python

This is what we get as output- the best fit line that has a maximum number of points. Quite accurate!

What is the difference between SVM and SVR?

Support Vector Machines (SVM) and Support Vector Regression (SVR) are supervised learning techniques employed in machine learning with unique functions and features.

Key Differences:

Support Vector Machine (SVM) is mostly utilized for tasks involving classification. The goal is to locate the best hyperplane that divides distinct classes within the feature space. The objective is to increase the distance between the nearest points of distinct classes, which are referred to as support vectors.

SVR is utilized for tasks involving regression. It forecasts values that are continual instead of distinct category labels. SVR aims to maximize the number of data points fitting within a given margin of tolerance (epsilon) while reducing errors outside this range.

Conclusion

Support Vector Regression (SVR) extends the principles of Support Vector Machines (SVM) to regression problems, offering a powerful tool for predicting continuous outputs. By leveraging various kernels such as quadratic, radial basis function, and sigmoid, SVR Model can handle complex and non-linear relationships in the data. Through this tutorial, we’ve explored the essential hyperparameters, implemented SVR in Python, and applied it to real-world datasets, demonstrating its versatility in artificial intelligence applications. Whether dealing with training samples in finance, engineering, or healthcare, SVR Model provides a robust approach to model continuous data effectively, enhancing the accuracy and reliability of predictive analytics.

Hope you like the article! Support vector regression (SVR) uses support vector machines to forecast continuous results, effectively managing linear and non-linear correlations. The SVR model shows robustness, versatility, and accuracy across different applications. If you found this information helpful, feel free to Share it.

Here is 10 YouTube Channels to Master Python

Key Takeaways

  • SVR extends Support Vector Machines (SVM) into regression problems, allowing for the prediction of continuous outcomes rather than classifying data into discrete categories as with a classifier.
  • SVR utilizes various kernel functions, such as quadratic, radial basis function, and sigmoid, to handle non-linear relationships in data, akin to how neural networks manage complex patterns.
  • Effective hyperparameter tuning, including choosing the right kernel and setting the epsilon parameter, is vital for maximizing SVR performance, similar to the role of gradient optimization in neural networks.
  • The SVR Model offers greater flexibility and robustness compared to traditional linear regression. It finds a hyperplane that best fits the data within a specified margin, making it suitable for more complex datasets.
  • Unlike logistic regression, primarily used for binary classification problems, Support Vector Regression (SVR) focuses on predicting continuous outcomes. SVR in Machine Learning leverages kernel functions to handle non-linear relationships in data, offering a more versatile approach for regression tasks.

Frequently Asked Questions

Q1. What are the applications of SVM regression?

A. Support Vector Regression (SVM) is a versatile algorithm used in finance, engineering, bioinformatics, natural language processing, image processing, and healthcare for accurate predictions. It commonly predicts stock prices, machine performance, protein structures, text classifications, sentiment analysis, object recognition, and medical outcomes.

Q2. How does the regularization parameter in SVM affect the regression model?

A. Regularization is a technique that avoids overfitting by penalizing large coefficients in the model. In SVM for Regression, the regularization parameter determines the trade-off between achieving a low error on the training data and minimizing the complexity of the regression model. A higher value of the regularization parameter increases the penalty for large coefficients, which helps to prevent the model from fitting the noise in the training data.

Q3. What are the benefits of using a polynomial kernel in SVM for regression?

A. A polynomial kernel helps in fitting a regression model that can capture more complex relationships in the input data. It transforms the original features into polynomial features of a given degree, thus allowing the model to learn non-linear relationships. This is especially beneficial in scenarios where the relationship between the dependent and independent variables is not linear, providing a more flexible and powerful model.

Responses From Readers

Clear

Rahul Dev
Rahul Dev

Thanks for the article,it gave an intuitive understanding about SVR It would be really helpful if you could also include the dataset,used for the demonstration.

Venkat
Venkat

The code is completely irrelevant to the dataset shown in the picture. Also this code is from Udemy course by Kiril Ermenko. Atleast give them the credit when you have plagiarized the code and content of the tutorial from elsewhere.

Junior Mukenze
Junior Mukenze

Thank you for this article, is very clear and helpful. However, I have one question on the example you gave. And My question concern characteristics variables (X) and target variables (Y). How to use SVR if we have more then one (1) characteristic variables. Like if we want to consider Salary against position level and age?

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details