decision tree regression
A tutorial on How to use decision tree regression.
- 0. Data Preprocessing
- 1. Training the decision tree regression model on the whole dataset
- 2. Predicting a new result with Linear Regression
- 3. Visualising the Decision Tree Regression results (higher resolution)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Position_Salaries.csv')
dataset
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor()
regressor.fit(X, y)
regressor.predict([[6.5]])
X_grid = np.arange(min(X), max(X), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
plt.title('Truth or Bluff (Decision Tree Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()