Question: Question 6 . R code understanding [10 points] rm(list=ls()) set.seed(1) x = matrix(rnorm(20*2), ncol =2) # How many obs? y = c(rep(-1,10), rep(1,10) ) #

Question 6. R code understanding [10 points]

rm(list=ls())

set.seed(1)

x = matrix(rnorm(20*2), ncol =2) # How many obs?

y = c(rep(-1,10), rep(1,10) ) # How many obs?

x[y==1,] = x[y==1,] + 1 # Create 2 groups with different means

plot(x, col=(3-y)) # Use numbers to define color

dat=data.frame(x=x, y=as.factor(y)) # Change data format

library (e1071) ##########################################

## e1071 contains svm()

## y is the target

## ~ separate target and features

## . means all features are included

## kernel can be linear, polynomial, radial basis, sigmoid

## cost is the cost parameter

## When cost is small, margin will be wide (opposite to the argument in the note)

## and more support vectors.

## scale=FALSE tells the svm() function NOT to scale feature to N(0,1) ##########################################

svm1 = svm(y~., data=dat, kernel="linear", cost=10, scale=FALSE )

plot(svm1, dat)

svm1$index

  1. Copy the code in your R. You may need to install package e1071. How many support vectors are there?
  2. Change the cost parameter to cost=1. How many support vectors now? Explain.

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!