Reduction to linear regression
SVM can be used to fit linear regression. In this section, we will explore how to do this with TensorFlow.
Getting ready
The same maximum margin concept can be applied toward fitting linear regression. Instead of maximizing the margin that separates the classes, we can think about maximizing the margin that contains the most (x, y) points. To illustrate this, we will use the same iris dataset, and show that we can use this concept to fit a line between sepal length and petal width.
The corresponding loss function will be similar to

. Here,

is half of the width of the margin, which makes the loss equal to 0 if a point lies in this region.
How to do it...
We proceed with the recipe as follows:
- First, we load the necessary libraries, start a graph, and load the iris dataset. After that, we will split the dataset into train and test sets to visualize the loss on both. Use the following code:
import matplotlib.pyplot as plt import numpy as np import tensorflow as tf ...