Guest User

Untitled

a guest
May 21st, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. # Support Vector Regression (SVR)
  2.  
  3. rm(list = ls())
  4.  
  5. # Importing the dataset
  6. dataset <- read.csv(file.path(getwd(),'Data/Position_Salaries.csv'))
  7. dataset <- dataset[2:3]
  8.  
  9. # Splitting the dataset into the Training set and Test set
  10. # # install.packages('caTools')
  11. # library(caTools)
  12. # set.seed(123)
  13. # split = sample.split(dataset$Salary, SplitRatio = 2/3)
  14. # training_set = subset(dataset, split == TRUE)
  15. # test_set = subset(dataset, split == FALSE)
  16.  
  17. # Feature Scaling
  18. # training_set = scale(training_set)
  19. # test_set = scale(test_set)
  20.  
  21. # Fitting SVR to the dataset
  22. # install.packages('e1071')
  23.  
  24. if (!require("e1071"))
  25. install.packages("e1071") # to install the package if you don't have it
  26. library(e1071)
  27. regressor <- svm(formula = Salary ~ .,
  28. data = dataset,
  29. type = 'eps-regression',
  30. kernel = 'radial')
  31.  
  32. # Predicting a new result
  33. y_pred <- predict(regressor, data.frame(Level = 6.5))
  34.  
  35. # Visualising the SVR results
  36. # install.packages('ggplot2')
  37. library(ggplot2)
  38. ggplot() +
  39. geom_point(aes(x = dataset$Level, y = dataset$Salary),
  40. colour = 'red') +
  41. geom_line(aes(x = dataset$Level, y = predict(regressor, newdata = dataset)),
  42. colour = 'blue') +
  43. ggtitle('Truth or Bluff (SVR)') +
  44. xlab('Level') +
  45. ylab('Salary') +
  46. theme_bw() +
  47. theme(plot.title = element_text(hjust = 0.5))
  48.  
  49. # Visualising the SVR results (for higher resolution and smoother curve)
  50. # install.packages('ggplot2')
  51. library(ggplot2)
  52. x_grid <- seq(min(dataset$Level), max(dataset$Level), 0.1)
  53. ggplot() +
  54. geom_point(aes(x = dataset$Level, y = dataset$Salary),
  55. colour = 'red') +
  56. geom_line(aes(x = x_grid, y = predict(regressor, newdata = data.frame(Level = x_grid))),
  57. colour = 'blue') +
  58. ggtitle('Truth or Bluff (SVR)') +
  59. xlab('Level') +
  60. ylab('Salary') +
  61. theme_bw() +
  62. theme(plot.title = element_text(hjust = 0.5))
Add Comment
Please, Sign In to add comment