R Tutorial: Generating a linearly separable dataset

Learn to build a two-predictor dataset that demonstrates SVM margin maximization. The instructor generates 200 random points, labels them by which side of the line X1 = X2 they fall on, then creates a margin by filtering out points within 0.05 of the boundary, showing how the original decision line sits exactly halfway between the margin edges.
Chapters
In the previous lesson, we used a simple one dimensional example to illustrate the notion of an optimal decision boundary, that is one that maximizes the margin. In this lesson, we'll create a two predictor data set that we will subsequently use to illustrate some of the key principles of support vector machines, including margin maximization. The data set we will generate is essentially a generalization of the previous example in that it has two variables instead of one and the decision boundary is a line rather than a point. We generate a Data set with 200 points consisting of two predictor variables, X1 and X2 that are uniformly distributed between 0 and 1. To do this, we first set the number of data points n to 200 and the seed integer for random number generation. We then create two sets of random numbers lying between 0 and 1 using the RunIF function, which generates random uniform random numbers. The Resulting values for x1 and x2 are stored in the data frame df. Next, we create two classes separated by a straight line x1 x2. This line passes through the origin and makes an angle of 45 degrees with the horizontal axis. We label points below the line as having class equals minus one and those above as having class one. Here's the code. Now let's see what the two class dataset looks like. Let's visualize the dataset and the decision boundary using ggplot. We'll create a two dimensional scatter plot with x1 on the x axis and x2 on the y axis, distinguishing the two classes by color. Points below the decision boundary will be colored red and those above blue. The decision boundary itself is a straight line x1 x2 which passes through the origin and has a slope of 1, I.e. it makes an angle of 45 degrees with the x1 axis. Here's the code. And here's the resulting plot. Notice that although the decision boundary separates the two classes cleanly, it has no margin. So let's introduce a small margin in the dataset. To create a margin, we need to remove points that lie close to the decision boundary. One way to do this is to filter out points that have x1 and x2 values that differ by less than a specified value. Let's set this value to 0.05 and do the filtering. The dataset should now have a margin. Let's replot it using exactly the same ggplot code as before. Here's the resulting plot. Notice the empty space on either side of the decision boundary. This is the margin. We can make the margin clearer by delineating its boundaries. The margin boundaries are parallel to the decision boundary and lie 0.05 units on either side of it. We'll draw the margin boundaries as dashed lines to distinguish them from the decision boundary. Here's the plot. Notice that our decision boundary is the maximal margin separator because it lies halfway between the margin boundaries. That's it for this chapter. In the exercises, we'll create a data set similar to the one discussed in this lesson. We will then use that data set extensively in the exercises. In the next chapter.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







