# ---
# 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
# ---

# This is a summary on working with Decision trees

import pandas as pd
HousingData = pd.read_csv("Melbourne_housing_FULL.csv")

#Drop rows with missing values
HousingData = HousingData.dropna(axis =0)
#Display column names
print(HousingData.columns)

#Extract target variable and Features
y = HousingData.Price
features = ['Landsize', 'Rooms', 'Bathroom', 'BuildingArea'] #Just select few non categorical features
X = HousingData[features]
X.head(5) #View first few feature

# +
#Split to training and test set
from sklearn.model_selection import train_test_split

# split data into training and validation data, for both features and target. Set random_state for reproducibality
train_X, val_X, train_y, val_y = train_test_split(X, y,random_state = 0)
# -

# We will use Decision Tree Regressor. To find the ideal leaf nodes, use a for loop

from sklearn.metrics import mean_absolute_error
from sklearn.tree import DecisionTreeRegressor
def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y):
    model = DecisionTreeRegressor(max_leaf_nodes= max_leaf_nodes,random_state=0)
    model.fit(train_X,train_y)
    pred = model.predict(val_X)
    mae = mean_absolute_error(pred,val_y)
    return mae


for max_leaf_nodes in [5,25,50,100,250,500,750,1000]:
    mae = get_mae(max_leaf_nodes, train_X,val_X,train_y,val_y)
    print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %f" %(max_leaf_nodes, mae))

# We notice that A Leaf node of 25 seems to produce the lowest error. So Build a final model around that

DecisionTree = DecisionTreeRegressor(max_leaf_nodes= 25,random_state=0)
DecisionTree.fit(train_X,train_y)

Predictions = DecisionTree.predict(val_X)
print("The Mean Absoulte Error: %f" %mean_absolute_error(Predictions,val_y))

StarcoderdataJupyterScriptsDedupFiltered