Guest User

Untitled

a guest
May 21st, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. # SVM Kernel
  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 Kernel 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 = 'radial')
  31.  
  32. # Predicting the Test set results
  33. y_pred = predict(classifier, newdata = test_set[-3])
  34.  
  35. # Calculating accuracy
  36. (accuracy <- mean(y_pred == test_set$Purchased))
  37.  
  38. # Making the Confusion Matrix
  39. y_pred <- factor(y_pred, levels = c(0,1), labels = c(0,1)) # using class lables
  40. cm <- as.matrix(table(Actual = test_set[, 3], Predicted = y_pred)) # create the confusion matrix
  41.  
  42. # Visualising the Training set results
  43. library(ElemStatLearn)
  44. set = training_set
  45. X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
  46. X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
  47. grid_set = expand.grid(X1, X2)
  48. colnames(grid_set) = c('Age', 'EstimatedSalary')
  49. y_grid = predict(classifier, newdata = grid_set)
  50. plot(set[, -3],
  51. main = 'Kernel SVM (Training set)',
  52. xlab = 'Age', ylab = 'Estimated Salary',
  53. xlim = range(X1), ylim = range(X2))
  54. contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
  55. points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
  56. points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
  57.  
  58. # Visualising the Test set results
  59. library(ElemStatLearn)
  60. set = test_set
  61. X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
  62. X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
  63. grid_set = expand.grid(X1, X2)
  64. colnames(grid_set) = c('Age', 'EstimatedSalary')
  65. y_grid = predict(classifier, newdata = grid_set)
  66. plot(set[, -3], main = 'Kernel SVM (Test set)',
  67. xlab = 'Age', ylab = 'Estimated Salary',
  68. xlim = range(X1), ylim = range(X2))
  69. contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
  70. points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
  71. points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
Add Comment
Please, Sign In to add comment