Question: Support Vector Machines This assignment will build off of the previous ungraded assignment. However, here you will use a radial basis function for your kernel

Support Vector Machines
This assignment will build off of the previous ungraded assignment. However, here you will use a radial basis function for your kernel rather than a linear specification.
To begin, a synthetic data set has been provided below. It is normally distributed with an added offset to create two separate classes.
library(tidymodels)
library(ISLR2)
set.seed(1)
sim_data2<- tibble(
x1= rnorm(200)+ rep(c(2,-2,0), c(100,50,50)),
x2= rnorm(200)+ rep(c(2,-2,0), c(100,50,50)),
y = factor(rep(c(1,2), c(150,50)))
)
sim_data2%>%
ggplot(aes(x1, x2, color = y))+
geom_point()
Now, you will try an SVM using a radial basis function (RBF). RBF should allow you to capture the non-linearity in the data. To create the specification, you should use svm_rbf(). Be sure to pass in classification as the mode and kernlab as the engine. Save your output to svm_rbf_spec.
Student's answer(Top)
library(tidymodels)
# Set seed for reproducibility
set.seed(1)
# Create synthetic data
sim_data2<- tibble(
x1= rnorm(200)+ rep(c(2,-2,0), c(100,50,50)),
x2= rnorm(200)+ rep(c(2,-2,0), c(100,50,50)),
y = factor(rep(c(1,2), c(150,50)))
)
# Visualize the synthetic data
sim_data2%>%
ggplot(aes(x1, x2, color = y))+
geom_point()
# Create SVM specification with RBF kernel using parsnip
svm_rbf_spec <- svm_rbf()%>%
set_mode("classification")%>%
set_engine("kernlab")
# Print the specification
svm_rbf_spec
Grade cell: cell-66b70e8f71c4b8acScore: 50.0/50.0(Top)
Hidden Tests Redacted
Congratulations! All test cases in this cell passed.
Now fit your model using fit().
Student's answer(Top)
# Load required libraries
library(tidymodels)
# Set seed for reproducibility
set.seed(1)
# Create synthetic data
sim_data2<- tibble(
x1= rnorm(200)+ rep(c(2,-2,0), c(100,50,50)),
x2= rnorm(200)+ rep(c(2,-2,0), c(100,50,50)),
y = factor(rep(c(1,2), c(150,50)))
)
# Visualize the synthetic data
sim_data2%>%
ggplot(aes(x1, x2, color = y))+
geom_point()
# Create SVM specification with RBF kernel using parsnip
svm_rbf_spec <- svm_rbf()%>%
set_mode("classification")%>%
set_engine("kernlab")
# Print the specification
svm_rbf_spec
# Fit the SVM model
svm_rbf_fit <- fit(svm_rbf_spec, data = sim_data2, formula = y ~ x1+ x2)
# Print the fitted model
svm_rbf_fit
Grade cell: cell-34d5ed07e423b2cdScore: 50.0/50.0(Top)
Hidden Tests Redacted
Congratulations! All test cases in this cell passed.
Plot your model. What do you notice?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!