-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverfitting.py
More file actions
40 lines (30 loc) · 1.24 KB
/
overfitting.py
File metadata and controls
40 lines (30 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
# Carregando dados
path_in = 'dados/melb_data.csv'
data = pd.read_csv(path_in)
# Seleção de variáveis e processamento
target = ['Price']
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude']
data = data[melbourne_features + target]
data.dropna(axis=0, inplace=True)
y = data[target[0]]
X = data[melbourne_features]
X.describe()
X.head()
# Separando base de dados em treino e validação
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
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)
preds_val = model.predict(val_X)
mae = mean_absolute_error(val_y, preds_val)
return mae
for max_leaf_nodes in [5, 50, 100, 250, 500, 1000, 1750, 2500, 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))
# Modelo final
model_final = DecisionTreeRegressor(max_leaf_nodes=1000)
model_final.fit(X, y)