import pandas as pd
from sklearn.tree import DecisionTreeRegressor
file = '../input/train.csv'
data = pd.read_csv(file)
y = data.SalePrice
predictors = ['YearBuilt','LotArea']
x = data[predictors]
model = DecisionTreeRegressor()
model.fit(x,y)
columns_name = ['YearBuilt','LotArea','SalePrice']
columns_data = data[columns_name]
print(columns_data.head())
print("Making predictions for the following 5 houses:"
print(x.head())
print("The predictions are")
print(model.predict(x.head()))
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, 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))
from sklearn.ensemble import RandomForestRegressor
forest_model = RandomForestRegressor()
forest_model.fit(train_X,train_Y)
melb_preds = forest_model.predict(val_X)
print(mean_absolute_error(val_y,melb_preds))