In [25]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

In [26]:
np.set_printoptions(precision=3, suppress=True)

Data Preparation

Importing a dataset


In [27]:
dataset = pd.read_csv('Position_Salaries.csv')

In [28]:
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

Decision Tree Regression

Fitting the model to the dataset


In [29]:
from sklearn.tree import DecisionTreeRegressor

In [30]:
regressor = DecisionTreeRegressor(random_state=0)
regressor.fit(X, y)


Out[30]:
DecisionTreeRegressor(criterion='mse', max_depth=None, max_features=None,
           max_leaf_nodes=None, min_impurity_split=1e-07,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, presort=False, random_state=0,
           splitter='best')

Predicting a new result


In [31]:
regressor.predict(6.5)


Out[31]:
array([ 150000.])

Visualising the Regression results


In [32]:
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()