Guest User

Untitled

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