Get startedGet started for free

Generating a 2d radially separable dataset

In this exercise you will create a 2d radially separable dataset containing 400 uniformly distributed data points.

This exercise is part of the course

Support Vector Machines in R

View Course

Exercise instructions

  • Generate a data frame df with:
    • 400 points with variables x1 and x2.
    • x1 and x2 uniformly distributed in (-1, 1).
  • Introduce a circular boundary of radius 0.8, centred at the origin.
  • Create df$y, which takes value -1 or 1 depending on whether a point lies within or outside the circle.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

#set number of variables and seed
n <- ___
set.seed(1)

#Generate data frame with two uniformly distributed predictors, x1 and x2
df <- data.frame(x1 = runif(n, min = ___, max = ___), 
                 x2 = runif(n, min = ___, max = ___))

#We want a circular boundary. Set boundary radius 
radius <- ___
radius_squared <- radius^2

#create dependent categorical variable, y, with value -1 or 1 depending on whether point lies
#within or outside the circle.
df$y <- factor(ifelse(df$___ + df$___ < radius_squared, -1, 1), levels = c(-1, 1))
Edit and Run Code