Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 2 - Regression/Support Vector Regression (SVR)/svr.R
1009 views
1
# SVR
2
3
# Importing the dataset
4
dataset = read.csv('Position_Salaries.csv')
5
dataset = dataset[2:3]
6
7
# Splitting the dataset into the Training set and Test set
8
# # install.packages('caTools')
9
# library(caTools)
10
# set.seed(123)
11
# split = sample.split(dataset$Salary, SplitRatio = 2/3)
12
# training_set = subset(dataset, split == TRUE)
13
# test_set = subset(dataset, split == FALSE)
14
15
# Feature Scaling
16
# training_set = scale(training_set)
17
# test_set = scale(test_set)
18
19
# Fitting SVR to the dataset
20
# install.packages('e1071')
21
library(e1071)
22
regressor = svm(formula = Salary ~ .,
23
data = dataset,
24
type = 'eps-regression',
25
kernel = 'radial')
26
27
# Predicting a new result
28
y_pred = predict(regressor, data.frame(Level = 6.5))
29
30
# Visualising the SVR results
31
# install.packages('ggplot2')
32
library(ggplot2)
33
ggplot() +
34
geom_point(aes(x = dataset$Level, y = dataset$Salary),
35
colour = 'red') +
36
geom_line(aes(x = dataset$Level, y = predict(regressor, newdata = dataset)),
37
colour = 'blue') +
38
ggtitle('Truth or Bluff (SVR)') +
39
xlab('Level') +
40
ylab('Salary')
41
42
# Visualising the SVR results (for higher resolution and smoother curve)
43
# install.packages('ggplot2')
44
library(ggplot2)
45
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.1)
46
ggplot() +
47
geom_point(aes(x = dataset$Level, y = dataset$Salary),
48
colour = 'red') +
49
geom_line(aes(x = x_grid, y = predict(regressor, newdata = data.frame(Level = x_grid))),
50
colour = 'blue') +
51
ggtitle('Truth or Bluff (SVR)') +
52
xlab('Level') +
53
ylab('Salary')
54