text_plain_output_1.png
from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_absolute_error

from sklearn.tree import DecisionTreeRegressor

from sklearn.tree import DecisionTreeRegressor

import pandas as pd

import pandas as pd

import pandas as pd
main_file_path = '../input/train.csv'
data = pd.read_csv(main_file_path)
sale_price_column = data.SalePrice
two_columns = ['Alley', 'LotShape']
two_columns_data = data[two_columns]

from sklearn.tree import DecisionTreeRegressor
import pandas as pd
main_file_path = '../input/train.csv'
data = pd.read_csv(main_file_path)
y = data.SalePrice
predictors = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
x = data[predictors]
model = DecisionTreeRegressor()
model.fit(x, y)
model.predict(x.head())

from sklearn.metrics import mean_absolute_error
predicted_home_prices = model.predict(x)
mean_absolute_error(y, predicted_home_prices)

from sklearn.metrics import mean_absolute_error
from sklearn.tree import DecisionTreeRegressor
def get_mae(max_leaf_nodes, predictors_train, predictors_val, targ_train, targ_val):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
    model.fit(predictors_train, targ_train)
    preds_val = model.predict(predictors_val)
    mae = mean_absolute_error(targ_val, preds_val)
    return mae
for max_leaf_nodes in [5, 50, 75, 80, 85, 100, 500, 5000]:
    my_mae = get_mae(max_leaf_nodes, train_x, val_x, train_y, val_y)
    print('Max leaf nodes: %d  \t\t Mean Absolute Error:  %d' % (max_leaf_nodes, my_mae))