Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.47 KB | None | 0 0
  1. rm(list=ls())
  2.  
  3. source('dataPartitionUtil.R')
  4. source('GAUtil.R')
  5.  
  6. load("top_univariate_features.RData")
  7.  
  8. library(GA)
  9. library(reshape2)
  10. library(ggplot2)
  11. library(e1071)
  12. library(foreach)
  13. library(doMC)
  14. library(parallel)
  15. library(cvTools)
  16.  
  17. registerDoMC(cores = detectCores())
  18.  
  19. predict.SVM <- function(){
  20.    
  21.     fitness <- function(string){
  22.        
  23.         print(paste("Executando função de ajuste...", i))
  24.         i <<- i + 1
  25.        
  26.         predict.with.SVM <- function(fs.train, fs.test){
  27.            
  28.             inc <- which(string == 1)
  29.            
  30.             features.subset <- top.features.vector[inc]
  31.            
  32.             train.fs.data <- fs.data[fs.train, features.subset]
  33.             train.fs.data <- transform(train.fs.data,
  34.                                        Label = fs.data$Label[fs.train])
  35.            
  36.             test.fs.data <- fs.data[fs.test, features.subset]
  37.             test.fs.data <- transform(test.fs.data ,
  38.                                       Label = fs.data$Label[fs.test])
  39.            
  40.             model <- svm(Label ~.,
  41.                          data = train.fs.data)
  42.            
  43.             result <- predict(model, newdata = test.fs.data)
  44.            
  45.             mean(result == test.fs.data$Label)
  46.         }
  47.        
  48.         folds <- cvFolds(nrow(fs.data), K = 10)
  49.        
  50.         prediction <- foreach(i = 1:10,
  51.                                .combine = 'cbind',
  52.                                .packages = 'e1071',
  53.                                .export = c('top.features.vector',
  54.                                            'fs.data', 'down.sample')) %dopar% {
  55.            
  56.             predict.with.SVM(folds$subsets[folds$which != i],
  57.                              folds$subsets[folds$which == i])
  58.         }
  59.        
  60.         mean(prediction)
  61.     }
  62.    
  63.     train <- down.sample(breast.cancer.data,
  64.                          breast.cancer.data$Label,
  65.                          30)
  66.    
  67.     fs.data <- breast.cancer.data[train,]
  68.    
  69.     remaining.data <- breast.cancer.data[-train,]
  70.    
  71.     test <- down.sample(remaining.data,
  72.                         remaining.data$Label,
  73.                         10)
  74.    
  75.     test.data <- remaining.data[test,]
  76.    
  77.     i <- 0
  78.    
  79.     GA <- ga("binary",
  80.              fitness = fitness,
  81.              nBits = length(top.features.vector),
  82.              names = top.features.vector,
  83.              monitor = plot,
  84.              maxiter = 300,
  85.              population = best.first,
  86.              popSize = 100
  87.     )
  88.    
  89.     plot(GA)
  90.    
  91.     solution.SVM <- top.features.vector[GA@solution[1, ] == 1]#[1:64]
  92.    
  93.     predict.breast.cancer <- function(){
  94.          
  95.         train.data <- fs.data[, solution.SVM]
  96.         train.data <- transform(train.data,
  97.                                 Label = fs.data$Label)
  98.        
  99.         vld.data <- test.data[, solution.SVM]
  100.         vld.data <- transform(test.data ,
  101.                               Label = test.data$Label)
  102.            
  103.         model <- svm(Label ~.,
  104.                      data = train.data)
  105.        
  106.         result <- predict(model, newdata = vld.data)
  107.        
  108.         c1.test.indexes <- vld.data$Label == 1
  109.         c2.test.indexes <- vld.data$Label == 0
  110.        
  111.         c2.acc <- mean(result[c2.test.indexes] == vld.data$Label[c2.test.indexes])
  112.         c1.acc <- mean(result[c1.test.indexes] == vld.data$Label[c1.test.indexes])
  113.         total.acc <- mean(result == vld.data$Label)
  114.        
  115.         c(c1.acc, c2.acc, total.acc)
  116.     }
  117.    
  118.     predictions <- predict.breast.cancer()
  119.                        
  120.     names(predictions) <- c("Classe 1 (y = 1)", "Classe 2 (y = 0)", "Total")
  121.    
  122.     list(top.features = solution.SVM,
  123.          predictions = predictions)
  124.    
  125. }
  126.  
  127. final.result <- lapply(1:10,
  128.                        function(i){
  129.                            predict.SVM()
  130.                        })
  131.  
  132. acc <- matrix(nrow = 0, ncol = 3)
  133. for(i in 1:length(final.result)){
  134.   acc <- as.matrix(rbind(acc, final.result[[i]]$predictions))
  135. }
  136.  
  137. melted <- melt(acc)
  138. boxplot <- ggplot(melted, aes(x = Var2, y = value)) +
  139.   facet_wrap(~Var2, scale="free_x") +
  140.   geom_boxplot() +
  141.   ggtitle("Performance using SVM applied to best subset of features according to GA with SVM") +
  142.   xlab("Classes") +
  143.   ylab("Accuracy") +
  144.   theme(title =  element_text(size = 20))
  145.  
  146. print(boxplot)
  147.  
  148. ggsave(plot = boxplot,
  149.        filename = "GA_SVM_rene_no_pls.pdf",
  150.        height = 10,
  151.        width = 20)
  152.  
  153. save.image("SVM_rene.RData")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement