RStudio Keyboard Shortcuts

 

https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts

change the environment variable for java home in r

 Sys.setenv(JAVA_HOME="C:\\Program Files\\Java\\jre1.8.0_121\\")

 

 

  1. import numpy as np
  2.  # this is a script in R for Classification using SVM
  3. # packages that are used:
  4. # plyr: Tools for splitting, applying and combining data
  5. # e1071: for SVM
  6. # caret
  7.  
  8. # clear the objects
  9.  
  10. setwd("f:\\datamining")
  11.  
  12. #load package for SVM
  13. library(pacman)
  14. if (!p_isloaded(lattice)){library(lattice)}
  15. if (!p_isloaded(e1071)) {   library(e1071) }
  16. if (!p_isloaded(plyr)) {library(plyr)}
  17. if (!p_isloaded(scatterplot3d)){library(scatterplot3d)}
  18. if (!p_isloaded(caret)){library(caret)}
  19. library(plot3Drgl)
  20.  
  21.  
  22.  
  23. ##### function
  24.  
  25. svmError(svm.ModelLinear[[1]]$coefs, svm.ModelLinear[[1]]$SV, svm.ModelLinear[[1]]$index,50000, x,y)
  26. get_obj_func_info <- function(w, b, c_par, x, y) {
  27.   xi <- rep(0, nrow(x))
  28.  
  29.   for (i in 1:nrow(x)) {
  30.     xi[i] <- 1 - as.numeric(as.character(y[i]))*(sum(w*x[i,]) + b)
  31.     if (xi[i] < 0) xi[i] <- 0
  32.   }
  33.  
  34.   return(list(obj_func_value = 0.5*sqrt(sum(w * w)) + c_par*sum(xi),
  35.               sum_xi = sum(xi), xi = xi))
  36. }
  37.  
  38.  
  39. rm(list = ls())
  40. wine.data <- read.csv2("winequality-white.csv", stringsAsFactors = F)
  41. wine.data <- data.frame(data.matrix(wine.data))
  42. data.TwoClasses = subset(wine.data, wine.data$quality == 5 | wine.data$quality == 6)
  43. rm(wine.data) # removing the wine.data because it is not needed anymore
  44. data.TwoClasses[,"quality"] <- as.factor(data.TwoClasses[,"quality"])
  45. levels(data.TwoClasses$quality)[1] <- '-1'
  46. levels(data.TwoClasses$quality)[2] <- '1'
  47. trainIndex <- createDataPartition(data.TwoClasses$quality, p = .7,list = FALSE, times = 1)
  48. y <- as.factor( data.TwoClasses[trainIndex, "quality"])
  49. features <- data.TwoClasses[,-ncol(data.TwoClasses)]
  50. correlation <- cor(features)
  51. x <- features[trainIndex,]
  52.  
  53. ##########+##### SVM model linear #################################################
  54.  
  55.  
  56.  
  57. confMatLinear <- list()
  58. svm.ModelLinear <- list() # create empty set
  59. predLinear <- list()
  60. w <- list()
  61. normW <- list()
  62. b <- list()
  63. error <- list()
  64. tempList <- list()
  65. listResult <- list()
  66.  
  67. c1= 50000
  68. for (i in 1:4){
  69.   # then learn the model, type="C-classification" is set so that
  70.   svm.ModelLinear[[i]] <- svm(x,y,kernel = "polynomial", cost = c1, gamma = 1 , degree = i, coef0 = 0)
  71.   predLinear[[i]] <- predict(svm.ModelLinear[[i]],x)
  72.   confMatLinear[[i]]<- confusionMatrix(predLinear[[i]],y)
  73.  
  74.   v=as.matrix(svm.ModelLinear[[i]]$coefs)
  75.   u=as.matrix(svm.ModelLinear[[i]]$SV)
  76.   w[[i]] <- t(v) %*% u
  77.  
  78.   #normW[[i]] <-
  79.   b[[i]] <- svm.ModelLinear[[i]]$rho
  80.   error[[i]] <- get_obj_func_info(w[[i]], b[[i]], c1, x, y)
  81.  
  82.   #listResult <- c(svm.ModelLinear[[i]],  predLinear[[i]],  confMatLinear[[i]],w[[i]] ,
  83.   #                c(norm(w[[i]]), b[[i]]), error[[i]])
  84. }
  85.  
  86.  
  87. confMatLinear2 <- list()
  88. svm.ModelLinear2 <- list() # create empty set
  89. predLinear2 <- list()
  90. for (i in 1:4){
  91.   svm.ModelLinear2[[i]] <- svm(x,y,kernel = "polynomial" , gamma = 1 , degree = i, coef0 = 10)
  92.   predLinear2[[i]] <- predict(svm.ModelLinear2[[i]],x)
  93.   confMatLinear2[[i]]<- confusionMatrix(predLinear2[[i]],y)
  94. }
  95.  
  96.  
  97. ############# SVM Gaussian #########################################################
  98. confMatGaussian <- list()
  99. svm.ModelGaussian <- list() # create empty set
  100. predGaussian <- list()
  101. for (i in 1:4){
  102.   svm.ModelGaussian[[i]] <- svm(x,y,kernel = "radial" , gamma = 1/(i^2), coef0 = 1)
  103.   predGaussian[[i]] <- predict(svm.ModelGaussian[[i]],x)
  104.   confMatGaussian[[i]]<- confusionMatrix(predGaussian[[i]],y)
  105. }
  106.  
  107.  
  108. #### PCA #############################
  109. PCAF <- prcomp(features, tol = .1)
  110.  
  111. barplot(PCAF$sdev/PCAF$sdev[1])
  112.  
  113. # extract train dataset from rotated data in PCA
  114. x1 <- data.frame(data.matrix(PCAF$x[trainIndex,]))
  115. svm.ModelLinearAllFeatures <- svm(x1,y ,kernel = "polynomial" , degree = 1)
  116. predictionLinearAllFeatures <- predict(svm.ModelLinearAllFeatures,x1)
  117. ConfMatLinearAllFeatures <- confusionMatrix(predictionLinearAllFeatures, y)
  118.  
  119.  
  120. clrs <- c("blue" , "red")
  121. plot3d(PCAF$x[,c(1,2,3)], col = clrs[as.factor( data.TwoClasses$quality)])
  122.  
  123. ### tuning #########################################################
  124.  
  125.  
  126. obj3 <- tune(svm, quality~., data = data.TwoClasses,
  127.             ranges = list(gamma = 1, cost = 2^(2:6), degree = 1:6 , coef0 = 0:5), kernel = "polynomial",
  128.             tunecontrol = tune.control(sampling = "fix"))
  129.  
  130. obj <- tune(svm, quality~., data = data.TwoClasses,
  131.             ranges = list(gamma = 2^(-1:1), cost = 2^(2:8)),
  132.             tunecontrol = tune.control(sampling = "fix"))
  133.  
  134. obj <- tune(svm, quality~., data = data.TwoClasses,
  135.              ranges = list(gamma = 2^(-1:1), cost = 2^(2:4),  type="C-classification"),
  136.             tunecontrol = tune.control(sampling = "fix"))
  137.  
  138. # prepare data to write
  139.  
  140. #data.train <- cbind(data.train, predictionLinear)
  141.  
  142. # store the svm model (model and data scaling files)
  143. write.svm(svm.ModelGaussian[[1]], svm.file="model.svm",scale.file="model.scale")
  144.