-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled.R
68 lines (56 loc) · 1.77 KB
/
Untitled.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
title: "SVM"
author: "Amit"
date: "7/18/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
data(iris)
str(iris)
library(ggplot2)
qplot(Petal.Length,Petal.Width,data=iris,color=Species)
```
```{r}
library(e1071)
classificationmymode<-svm(Species~.,data=iris)
linearmymode<-svm(Species~.,data=iris,kernal="linear")
logisticmode<-svm(Species~.,data=iris,kernal="logistic")
tanhmode<-svm(Species~.,data=iris,kernal="tanh")
summary(tanhmode)
summary(logisticmode)
summary(classificationmymode)
summary(linearmymode)
plot(tanhmode,data=iris,Petal.Width~Petal.Length,slice=list(Sepal.Width=3,Sepal.Length=4))
```
```{r}
#confusion matrix
pred<-predict(tanhmode,iris)
tab<-table(Predicted=pred,Actual=iris$Species)
tab
1-sum(diag(tab))/sum(tab)
```
```{r}
#Tuning
set.seed(123)
tmodel<-tune(svm,Species~.,data=iris,ranges = list(epsilon=seq(0,1,0.1),cost=2^(2:9)))
plot(tmodel)
summary(tmodel)
#best model
permodel<-tmodel$best.model
summary(permodel)
plot(permodel,data=iris,Petal.Width~Petal.Length,slice=list(Sepal.Width=3,Sepal.Length=4))
```
```{r}
#Misclassification error is only 2
pre<-predict(permodel,iris)
tab<-table(Predicted=pre,Actual=iris$Species)
tab
1-sum(diag(tab))/sum(tab)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.