In [1]:
#importing the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
#importing the dataset
dataset=pd.read_csv('Position_Salaries.csv')
X=dataset.iloc[:,1:2].values
y=dataset.iloc[:,2].values

In [ ]:


In [5]:
#fitting the decision tree regression model to the dataset
from sklearn.tree import DecisionTreeRegressor
regressor=DecisionTreeRegressor(random_state=0)
regressor.fit(X,y)


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

In [6]:
y_pred = regressor.predict(array([[6.5]]))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-31ce106e9c67> in <module>
----> 1 y_pred = regressor.predict(array([[6.5]]))

NameError: name 'array' is not defined

In [8]:
y_pred


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-3aaf935e6aec> in <module>
----> 1 y_pred

NameError: name 'y_pred' is not defined

In [55]:
#visualising the decision tree regression result(for higher resolution and smoother curves)
X_grid=np.arange(min(X),max(X),0.01)
X_grid.shape


Out[55]:
(900,)

In [56]:
#reshaping X_grid from 1-D array to 2-D array
X_grid=X_grid.reshape(len(X_grid),1)

In [58]:
plt.scatter(X,y,color='red')
plt.plot(X_grid,regressor.predict(X_grid),color='blue')
plt.title('Truth vs Bluff(Decision tree regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()



In [ ]: