Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # reading data and loading libraries
- setwd("~/R/wd/R_digits")
- data_all=read.csv("train.csv")
- library(parallel)
- library(e1071)
- library(caret)
- library(ROCR)
- # pre-processing
- # binary : 8 --> TRUE, others --> FALSE
- labels <- data_all[,1] == 8
- ## multiclass
- # labels <- as.factor(data_all[,1])
- # data.frame
- data_8 <- cbind(labels,data_all[,-1])
- # quantity (%) of observations to use
- q_data <- 0.1
- nb_pics <- nrow(data_8)
- idx <- sample(1:nb_pics, floor(nb_pics*q_data), replace = FALSE)
- data_8 <- data_8[idx,]
- #test/train full index
- nb_pics <- nrow(data_8)
- idx <- sample(1:nb_pics, floor(nb_pics*0.8), replace = FALSE)
- # test/train full sets
- training <- data_8[idx,]
- testing <- data_8[-idx,]
- # deleting empty cols
- del_zeros <- function(matrice) {which(apply(matrice,2,max) !=0)}
- nozeros <- del_zeros(training)
- training <- training[,nozeros]
- # svm
- # starting time
- t <- Sys.time()
- model <- svm(labels ~ ., data = training, probability = TRUE, type = "C-classification",cost = 2, gamma = 1.4 * (1/(dim(training)[2]-1)))
- # total time
- t <- Sys.time() - t
- t
- # analysis
- # predict
- pred <- predict(model, testing[,-1], probability = TRUE)
- # confusion matrix
- CM <- confusionMatrix(pred, testing[,1])
- acc <- CM$overall[1]
- conf <- CM$table
- conf
- acc
- # ROC curve using ROCR
- plot(col = "red", performance(prediction(attr(pred, "probabilities")[,2], testing[,1]), 'tpr', 'fpr'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement