0% found this document useful (0 votes)
34 views7 pages

Support Vector Classifier Analysis

The document details the implementation of a Support Vector Classifier (SVC) using the R programming language, including data generation, model fitting, and parameter tuning. It demonstrates the use of the 'e1071' library for SVM classification with various cost parameters and evaluates model performance through cross-validation. The results include summaries of the fitted models, support vectors, and prediction accuracy on test data.

Uploaded by

hubertkuo418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Support Vector Classifier Analysis

The document details the implementation of a Support Vector Classifier (SVC) using the R programming language, including data generation, model fitting, and parameter tuning. It demonstrates the use of the 'e1071' library for SVM classification with various cost parameters and evaluates model performance through cross-validation. The results include summaries of the fitted models, support vectors, and prediction accuracy on test data.

Uploaded by

hubertkuo418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Support Vector ClassifierSupport Vector Classifier

411210002 郭玉皓

2024-12-14
[Link](1)
x = matrix(rnorm(20*2), ncol = 2)
y =c(rep(-1, 10), rep(1, 10))
x[y==1,] = x[y==1,] +1
plot(x, col = (3 - y))

dat = [Link](x = x, y = [Link](y))


library(e1071)

svmfit = svm(y~., data = dat, kernel = "linear", cost = 10, scale = FAL
SE)
plot(svmfit, dat)
svmfit$index

## [1] 1 2 5 7 14 16 17

summary(svmfit)

##
## Call:
## svm(formula = y ~ ., data = dat, kernel = "linear", cost = 10, scale
= FALSE)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 10
##
## Number of Support Vectors: 7
##
## ( 4 3 )
##
##
## Number of Classes: 2
##
## Levels:
## -1 1
svmfit = svm(y~., data = dat, kernel = "linear", cost = 0.1, scale = FA
LSE)
svmfit = svm(y~., data = dat, kernel = "linear", cost = 0.1, scale = FA
LSE)
plot(svmfit, dat)

svmfit$index

## [1] 1 2 3 4 5 7 9 10 12 13 14 15 16 17 18 20

[Link](1)
[Link] = tune(svm, y~., data = dat, kernel = "linear", ranges = list
(cost=c(0.001, 0.01, 0.1, 1, 5, 10, 100)))
summary([Link])

##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.1
##
## - best performance: 0.05
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-03 0.55 0.4377975
## 2 1e-02 0.55 0.4377975
## 3 1e-01 0.05 0.1581139
## 4 1e+00 0.15 0.2415229
## 5 5e+00 0.15 0.2415229
## 6 1e+01 0.15 0.2415229
## 7 1e+02 0.15 0.2415229

bestmod = [Link]$[Link]
summary(bestmod)

##
## Call:
## [Link](METHOD = svm, train.x = y ~ ., data = dat, ranges = list(c
ost = c(0.001,
## 0.01, 0.1, 1, 5, 10, 100)), kernel = "linear")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 0.1
##
## Number of Support Vectors: 16
##
## ( 8 8 )
##
##
## Number of Classes: 2
##
## Levels:
## -1 1

xtest = matrix(rnorm(20*2), ncol = 2)


ytest = sample(c(-1, 1), 20, rep = TRUE)
xtest[ytest==1, ] = xtest[ytest==1, ] + 1
testdat = [Link](x = xtest, y = [Link](ytest))
ypred = predict(bestmod, testdat)
table(predict = ypred, truth = testdat$y)

## truth
## predict -1 1
## -1 9 1
## 1 2 8

svmfit = svm(y~., data = dat, kernel = "linear", cost = 0.1, scale =


FALSE)
ypred = predict(svmfit, testdat)
table(predict = ypred, truth = testdat$y)
## truth
## predict -1 1
## -1 9 1
## 1 2 8

x[y==1, ] = x[y==1, ] + 0.5


plot(x, col = (y+5)/2, pch = 19)

dat = [Link](x = x, y = [Link](y))


svmfit = svm(y~., data = dat, kernel = "linear", cost = 1e+05)
summary(svmfit)

##
## Call:
## svm(formula = y ~ ., data = dat, kernel = "linear", cost = 1e+05)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 1e+05
##
## Number of Support Vectors: 3
##
## ( 1 2 )
##
##
## Number of Classes: 2
##
## Levels:
## -1 1

plot(svmfit, dat)

svmfit = svm(y~., data = dat, kernel = "linear", cost = 1)


summary(svmfit)

##
## Call:
## svm(formula = y ~ ., data = dat, kernel = "linear", cost = 1)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 1
##
## Number of Support Vectors: 7
##
## ( 4 3 )
##
##
## Number of Classes: 2
##
## Levels:
## -1 1

plot(svmfit, dat)

You might also like