library(e1071)
data("iris")
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#model
model<-svm(Species~ ., data =iris)
model
##
## Call:
## svm(formula = Species ~ ., data = iris)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
## gamma: 0.25
##
## Number of Support Vectors: 51
# split
library(caTools)
split<-sample.split(iris$Species, SplitRatio = 0.7)
training<-subset(iris,split==TRUE)
testing<-subset(iris, split==FALSE)
#visualize
library(ggplot2)
library(GGally)
ggpairs(training, ggplot2::aes(colour = Species, alpha = 0.4))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# without species
training[,1:4] = scale(training[,1:4])
testing[,1:4] = scale(testing[,1:4])
classifier1 = svm(formula = Species~., data = training, type = 'C-classification', kernel = 'radial')
classifier2 = svm(formula = Species~ Petal.Width + Petal.Length, data = training, type = 'C-classification', kernel = 'radial')
# prediction
test_pred1 = predict(classifier1, type = 'response', newdata = testing[-5])
test_pred2 = predict(classifier2, type = 'response', newdata = testing[-5])
head(test_pred1)
## 3 4 5 13 14 16
## setosa setosa setosa setosa setosa setosa
## Levels: setosa versicolor virginica
table(test_pred1,test_pred2)
## test_pred2
## test_pred1 setosa versicolor virginica
## setosa 15 0 0
## versicolor 0 15 0
## virginica 0 1 14
# again build
svm.model<-svm(Species~.,data=iris, type='C-classification',kernel='linear', scale=FALSE)
plot(iris$Sepal.Length,iris$Sepal.Width,col=as.integer(iris[,5]),pch=c("o","+")[1:150 %in% svm.model$index+1],cex=2,xlab="sepal length", ylab="sepal width")

svm.model1 <- svm(Species ~ Sepal.Length + Sepal.Width, data = iris, kernel = "linear")
plot(svm.model1, iris, Sepal.Width ~ Sepal.Length, slice=list(sepal.Width=1,sepal.Length=2))

svm.prediction<-predict(svm.model1,iris[,-5])
table(svm.prediction)
## svm.prediction
## setosa versicolor virginica
## 49 54 47
# POLYNOMIAL
svm.model2<-svm(Species ~ Sepal.Length + Sepal.Width, data = iris, kernel ='polynomial' )
plot(svm.model2,iris,Sepal.Width ~ Sepal.Length,slice = list(sepal.width=1,sepal.length=2))

# continue