# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Decision Tree Regression Notebook

# #### *Author: <NAME>*
# #### *University of Chicago, CAPP'20*

# +
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn.tree import DecisionTreeRegressor

# %matplotlib notebook
# -

# ### Load Data

salary = pd.read_csv("Position_Salaries.csv")
salary.head()

# ### Data Cleaning

salary.isnull().sum()

# No value missing.

# ### Feature Selection

X = salary.iloc[:, 1:2].values
X.shape

y = salary.Salary.values
y.shape

# ### Model Training

# As we only have ten observations, we are using the whole data set to train our model.

dtr = DecisionTreeRegressor(random_state=123)
dtr.fit(X, y)

# ### Model Evaluation

# +
plt.scatter(X, y, color="red")
plt.plot(X, dtr.predict(X), color="blue")

plt.title("Salary against Position Level (with Decision Tree Regressor)")
plt.xlabel("Position Level")
plt.ylabel("Salary ($)")
plt.show()
# -

# **This does not look like a predictive output of a decision tree regression, as decision tree regression is not continuous.**

# It happens because expect for each position level, we are not predicting and plotting y values in between. A decision tree prediction should look like below:

X_grid = np.arange(min(X), max(X), 0.00001)
X_grid = X_grid.reshape((len(X_grid), 1))

# +
plt.scatter(X, y, color="red")
plt.plot(X_grid, dtr.predict(X_grid), color="blue")

plt.title("Actual: Salary against Position (with Decision Tree Regressor)")
plt.xlabel("Position Level")
plt.ylabel("Salary ($)")
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered