Path: blob/master/Part 2 - Regression/Decision Tree Regression/decision_tree_regression.R
1009 views
# Decision Tree Regression12# Importing the dataset3dataset = read.csv('Position_Salaries.csv')4dataset = dataset[2:3]56# Splitting the dataset into the Training set and Test set7# # install.packages('caTools')8# library(caTools)9# set.seed(123)10# split = sample.split(dataset$Salary, SplitRatio = 2/3)11# training_set = subset(dataset, split == TRUE)12# test_set = subset(dataset, split == FALSE)1314# Feature Scaling15# training_set = scale(training_set)16# test_set = scale(test_set)1718# Fitting Decision Tree Regression to the dataset19# install.packages('rpart')20library(rpart)21regressor = rpart(formula = Salary ~ .,22data = dataset,23control = rpart.control(minsplit = 1))2425# Predicting a new result with Decision Tree Regression26y_pred = predict(regressor, data.frame(Level = 6.5))2728# Visualising the Decision Tree Regression results (higher resolution)29# install.packages('ggplot2')30library(ggplot2)31x_grid = seq(min(dataset$Level), max(dataset$Level), 0.01)32ggplot() +33geom_point(aes(x = dataset$Level, y = dataset$Salary),34colour = 'red') +35geom_line(aes(x = x_grid, y = predict(regressor, newdata = data.frame(Level = x_grid))),36colour = 'blue') +37ggtitle('Truth or Bluff (Decision Tree Regression)') +38xlab('Level') +39ylab('Salary')4041# Plotting the tree42plot(regressor)43text(regressor)4445