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

Decision Trees for Regression

# +
#Use within an Jupyter notebook
# %matplotlib inline   

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

from sklearn.datasets import load_diabetes

diabetes = load_diabetes()

X = diabetes.data
y = diabetes.target

X_feature_names = ['age', 'gender', 'body mass index', 'average blood pressure','bl_0','bl_1','bl_2','bl_3','bl_4','bl_5']

pd.Series(y).hist(bins=50)

# +
bins = 50*np.arange(8)
binned_y = np.digitize(y, bins)

pd.Series(binned_y).hist(bins=50)

# +
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,stratify=binned_y)

# +
from sklearn.tree import DecisionTreeRegressor

dtr = DecisionTreeRegressor()
dtr.fit(X_train, y_train)
y_pred = dtr.predict(X_test)

from sklearn.metrics import mean_absolute_error
mean_absolute_error(y_test, y_pred)
# -

(np.abs(y_test - y_pred)/(y_test)).mean()

pd.Series((y_test - y_pred)).hist(bins=50)

pd.Series((y_test - y_pred)/(y_test)).hist(bins=50)

StarcoderdataJupyterScriptsDedupFiltered