Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Support Vector Regression (SVR)
- rm(list = ls())
- # Importing the dataset
- dataset <- read.csv(file.path(getwd(),'Data/Position_Salaries.csv'))
- dataset <- dataset[2:3]
- # Splitting the dataset into the Training set and Test set
- # # install.packages('caTools')
- # library(caTools)
- # set.seed(123)
- # split = sample.split(dataset$Salary, SplitRatio = 2/3)
- # training_set = subset(dataset, split == TRUE)
- # test_set = subset(dataset, split == FALSE)
- # Feature Scaling
- # training_set = scale(training_set)
- # test_set = scale(test_set)
- # Fitting SVR to the dataset
- # install.packages('e1071')
- if (!require("e1071"))
- install.packages("e1071") # to install the package if you don't have it
- library(e1071)
- regressor <- svm(formula = Salary ~ .,
- data = dataset,
- type = 'eps-regression',
- kernel = 'radial')
- # Predicting a new result
- y_pred <- predict(regressor, data.frame(Level = 6.5))
- # Visualising the SVR results
- # install.packages('ggplot2')
- library(ggplot2)
- ggplot() +
- geom_point(aes(x = dataset$Level, y = dataset$Salary),
- colour = 'red') +
- geom_line(aes(x = dataset$Level, y = predict(regressor, newdata = dataset)),
- colour = 'blue') +
- ggtitle('Truth or Bluff (SVR)') +
- xlab('Level') +
- ylab('Salary') +
- theme_bw() +
- theme(plot.title = element_text(hjust = 0.5))
- # Visualising the SVR results (for higher resolution and smoother curve)
- # install.packages('ggplot2')
- library(ggplot2)
- x_grid <- seq(min(dataset$Level), max(dataset$Level), 0.1)
- ggplot() +
- geom_point(aes(x = dataset$Level, y = dataset$Salary),
- colour = 'red') +
- geom_line(aes(x = x_grid, y = predict(regressor, newdata = data.frame(Level = x_grid))),
- colour = 'blue') +
- ggtitle('Truth or Bluff (SVR)') +
- xlab('Level') +
- ylab('Salary') +
- theme_bw() +
- theme(plot.title = element_text(hjust = 0.5))
Add Comment
Please, Sign In to add comment