library(e1071)
## A small example with the IRIS data set
data(iris)
## Split to train + test set
idxs <- sample(1:nrow(iris),as.integer(0.7*nrow(iris)))
trainIris <- iris[idxs,]
testIris <- iris[-idxs,]
?svm #scale = TRUE, kernel = "radial", degree = 3, gamma = if (is.vector(x)) 1 else 1 / ncol(x), cost = 1,
#Default Paramters
model <- svm(Species~., data=trainIris)
pred <- predict(model, testIris)
confus.matrix = table(real=testIris$Species, predict=pred)
print(confus.matrix)
accuracy = sum(diag(confus.matrix))/sum(confus.matrix)
cat("accuracy =",accuracy)
?tune.svm
## Tuning SVM to find the best cost and gamma by 10 fold cross-validation.
svm_tune <- tune.svm(Species~., data = trainIris,
kernel="radial", cost=10^(-1:2), gamma=c(.5,1,2))
print(svm_tune)
plot(svm_tune)
svm_model_after_tune <- svm(Species ~ ., data=trainIris, kernel="radial",
cost=svm_tune$best.parameters$cost, gamma=svm_tune$best.parameters$gamma)
pred <- predict(svm_model_after_tune,testIris)
confus.matrix = table(real=testIris$Species, predict=pred)
print(confus.matrix)
accuracy = sum(diag(confus.matrix))/sum(confus.matrix)
cat("accuracy =",accuracy)
Also use the svm() function in R package 'e1071'.
data = data.frame(x=1:20,
y=c(3,4,8,2,6,10,12,13,15,14,17,18,20,17,21,22,25,30,29,31))
plot(data$x, data$y, pch=16, xlab="X", ylab="Y")
## by linear regression
model <- lm(y ~ x , data)
lm.pred = predict(model, data)
plot(data$x, data$y, pch=16, xlab="X", ylab="Y")
points(lm.pred, pch=2, col="red")
abline(model, col="red")
## by SVR
model <- svm(y ~ x , data)
svr.pred = predict(model, data)
plot(data$x, data$y, pch=16, xlab="X", ylab="Y")
points(svr.pred, pch=4, col="blue") ## not linear
## Check the accuracy
cat("Mean Error by lm is: ",sqrt(mean((data$y - lm.pred)^2)),"\n")
cat("Mean Error by SVR is: ",sqrt(mean((data$y - svr.pred)^2)))
# tune cost and epsilon in SVR
tune.model = tune(svm,
y~x,
data=data,
range=list(cost=2^(2:9), epsilon = seq(0,1,0.1))
)
plot(tune.model)
model <- svm(y ~ x , data,cost = tune.model$best.parameters$cost,epsilon = tune.model$best.parameters$epsilon)
svr.pred = predict(model, data)
## Check the accuracy
cat("Mean Error by tuned SVR is: ",sqrt(mean((data$y - svr.pred)^2)))