Guest User

Untitled

a guest
May 21st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. # Grid Search
  2. # using the caret package to tune a classifier.
  3.  
  4. rm(list = ls())
  5.  
  6. # Importing the dataset
  7. dataset <- read.csv(file.path(getwd(),'Data/Social_Network_Ads.csv'))
  8. dataset = dataset[3:5]
  9.  
  10. # Encoding the target feature as factor
  11. dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))
  12.  
  13. # Splitting the dataset into the Training set and Test set
  14. # install.packages('caTools')
  15. library(caTools)
  16. set.seed(123)
  17. split = sample.split(dataset$Purchased, SplitRatio = 0.75)
  18. training_set = subset(dataset, split == TRUE)
  19. test_set = subset(dataset, split == FALSE)
  20.  
  21. # Feature Scaling
  22. training_set[-3] = scale(training_set[-3])
  23. test_set[-3] = scale(test_set[-3])
  24.  
  25. # Fitting Kernel SVM to the Training set
  26. # install.packages('e1071')
  27. library(e1071)
  28. classifier = svm(formula = Purchased ~ .,
  29. data = training_set,
  30. type = 'C-classification',
  31. kernel = 'radial')
  32.  
  33. # Predicting the Test set results
  34. y_pred = predict(classifier, newdata = test_set[-3])
  35.  
  36. # Making the Confusion Matrix
  37. cm <- as.matrix(table(Actual = test_set[, 3], Predicted = y_pred)) # create the confusion matrix
  38.  
  39. # Calculating accuracy
  40. # accuracy = sum(diag(cm)) / sum(cm)
  41. (accuracy <- mean(y_pred == test_set$Purchased))
  42.  
  43. # Model Evaluation
  44. # Applying k-Fold Cross Validation
  45. # install.packages('caret')
  46. library(caret)
  47. folds = createFolds(training_set$Purchased, k = 10)
  48. cv = lapply(folds, function(x) {
  49. training_fold = training_set[-x, ]
  50. test_fold = training_set[x, ]
  51. classifier = svm(formula = Purchased ~ .,
  52. data = training_fold,
  53. type = 'C-classification',
  54. kernel = 'radial')
  55. y_pred = predict(classifier, newdata = test_fold[-3])
  56. cm = table(test_fold[, 3], y_pred)
  57. #accuracy = (cm[1,1] + cm[2,2]) / (cm[1,1] + cm[2,2] + cm[1,2] + cm[2,1])
  58. accuracy = sum(diag(cm)) / sum(cm)
  59. return(accuracy)
  60. })
  61. accuracy_k_folds <- mean(as.numeric(cv))
  62.  
  63. # Applying Grid Search to find the best parameters
  64. # install.packages('caret')
  65. library(caret)
  66. classifier = train(form = Purchased ~ ., data = training_set, method = 'svmRadial')
  67. classifier
  68. classifier$bestTune
  69.  
  70. # Visualising the Training set results
  71. library(ElemStatLearn)
  72. set = training_set
  73. X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
  74. X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
  75. grid_set = expand.grid(X1, X2)
  76. colnames(grid_set) = c('Age', 'EstimatedSalary')
  77. y_grid = predict(classifier, newdata = grid_set)
  78. plot(set[, -3],
  79. main = 'Kernel SVM (Training set)',
  80. xlab = 'Age', ylab = 'Estimated Salary',
  81. xlim = range(X1), ylim = range(X2))
  82. contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
  83. points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
  84. points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
  85.  
  86. # Visualising the Test set results
  87. library(ElemStatLearn)
  88. set = test_set
  89. X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
  90. X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
  91. grid_set = expand.grid(X1, X2)
  92. colnames(grid_set) = c('Age', 'EstimatedSalary')
  93. y_grid = predict(classifier, newdata = grid_set)
  94. plot(set[, -3], main = 'Kernel SVM (Test set)',
  95. xlab = 'Age', ylab = 'Estimated Salary',
  96. xlim = range(X1), ylim = range(X2))
  97. contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
  98. points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
  99. points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
Add Comment
Please, Sign In to add comment