-
Notifications
You must be signed in to change notification settings - Fork 15
/
solution_03_DT.py
35 lines (24 loc) · 1.36 KB
/
solution_03_DT.py
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
grid_values = {'criterion': ['entropy','gini'],
'max_depth':[2,6,10,14],
'min_samples_split':np.arange(2,len(X_penguin_train)//4,5),
'min_samples_leaf':np.arange(1,len(X_penguin_train)//4,5)}
grid_tree_p_roc_auc = GridSearchCV(DecisionTreeClassifier(),
param_grid = grid_values,
scoring='roc_auc_ovr_weighted',
n_jobs=-1)
grid_tree_p_roc_auc.fit(X_penguin_train, y_penguin_train)
y_decision_fn_scores_p_roc_auc=grid_tree_p_roc_auc.score(X_penguin_test,y_penguin_test)
print(f'Grid best score (roc_auc_ovr_weighted): {grid_tree_p_roc_auc.best_score_:.3f}')
print('Grid best parameter (max. roc_auc_ovr_weighted): ')
for k,v in grid_tree_p_roc_auc.best_params_.items():
print('\t',k,'->',v)
from sklearn.metrics import accuracy_score, confusion_matrix
y_test_score=grid_tree_p_roc_auc.score(X_penguin_test,y_penguin_test)
print('Grid best parameter (max. accuracy) model on test: ', y_test_score)
y_penguin_pred_test = grid_tree_p_roc_auc.predict(X_penguin_test)
confusion_m_cancer = confusion_matrix(y_penguin_test, y_penguin_pred_test)
plt.figure(figsize=(5.5,4))
sns.heatmap(confusion_m_cancer, annot=True)
plt.title('test {} : {:.3f}'.format( grid_tree_p_roc_auc.scoring , y_test_score ))
plt.ylabel('True label')
plt.xlabel('Predicted label')