Created
June 16, 2016 10:56
-
-
Save arthurwuhoo/accbd7255c0df932c58666322d6eb417 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sample.data <- read.csv("svm_sample.csv") | |
sample.data <- sample.data[,-1] #getting rid of id variables | |
library(caret) | |
train_index <- createDataPartition(sample.data$color, 0.8)[[1]] | |
sample.data.train <- sample.data[train_index,] | |
sample.data.test <- sample.data[-train_index,] | |
########################################################################## | |
## MODEL 1: | |
##doing a linear svm | |
########################################################################## | |
sample.svm <- svm(color ~ ., data = sample.data.train, kernel = "linear") | |
sample.svm #creates 75 support vectors!!! | |
svm.predictions_linear <- predict(sample.svm, sample.data.test) | |
table(svm.predictions_linear,sample.data.test$color) | |
########################################################################## | |
## MODEL 2: | |
##doing a radial svm | |
########################################################################## | |
##doing a radial svm | |
library(e1071) | |
sample.svm <- svm(color ~ ., data = sample.data.train, kernel = "radial") | |
sample.svm #creates 75 support vectors!!! | |
svm.predictions_radial <- predict(sample.svm, sample.data.test) | |
table(svm.predictions_radial,sample.data.test$color) | |
###TUNING THIS MORE | |
?tune | |
svm_tune <- tune.svm(color ~., data = sample.data.train, kernel="radial", cost=10^(-1:2), gamma=c(.5,1,2)) | |
print(svm_tune) | |
tuned_svm <- svm(color ~., data = sample.data.train, kernel = "radial", cost = 0.1, gamma = 2) | |
svm.predictions_tuned_radial <- predict(tuned_svm, sample.data.test) | |
table(svm.predictions_tuned_radial, sample.data.test$color) | |
########################################################################## | |
## MODEL 3: | |
##doing a logistic regression | |
########################################################################## | |
#need to convert values to a 0 or 1 for a logistic regression | |
sample.data.train.log <- sample.data.train | |
sample.data.test.log <- sample.data.test | |
sample.data.train.log$color <- as.numeric(sample.data.test$color) - 1 | |
sample.data.test.log$color <- as.numeric(sample.data.test$color) - 1 | |
## this means purple is 0, green is 1 | |
sample.logistic <- glm(color ~ ., data = sample.data.train.log, family = "binomial") | |
log.predictions <- predict(sample.logistic, sample.data.test.log, link = "response") | |
log.predictions <- ifelse(log.predictions<0.5, 0, 1) | |
log.predictions[log.predictions==0] <- "purple" | |
log.predictions[log.predictions==1] <- "green" | |
table(log.predictions, sample.data.test.log$color) #utter disaster | |
########################################################################## | |
## MODEL 4: | |
##doing a logistic regression | |
########################################################################## | |
##doing a decision tree | |
sample.rpart <- rpart(color ~ ., data = sample.data.train) | |
library(rattle) | |
fancyRpartPlot(sample.rpart) | |
sample.rpart #creates 75 support vectors!!! | |
rpart.predictions <- predict(sample.rpart, sample.data.test, type = "class") | |
## assessing accuracy for all models | |
accuracy_rpart <- sum(rpart.predictions==sample.data.test$color)/length(sample.data.test$color) | |
accuracy_svmlinear <- sum(svm.predictions_linear==sample.data.test$color)/length(sample.data.test$color) | |
accueracy_svmradial_tuned <- sum(svm.predictions_tuned_radial==sample.data.test$color)/length(sample.data.test$color) | |
accuracy_svmradial <- sum(svm.predictions_radial==sample.data.test$color)/length(sample.data.test$color) | |
accuracy_log <- sum(log.predictions==sample.data.test.log$color)/length(sample.data.test$color) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment