Skip to content

Commit

Permalink
Add scoring model on test set (#440)
Browse files Browse the repository at this point in the history

Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
  • Loading branch information
ArturoAmorQ and ogrisel committed Aug 27, 2021
1 parent b46dc84 commit 879ebe1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
12 changes: 10 additions & 2 deletions python_scripts/parameter_tuning_ex_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
# loops, make a search of the best combinations of the `learning_rate` and
# `max_leaf_nodes` parameters. In this regard, you will need to train and test
# the model by setting the parameters. The evaluation of the model should be
# performed using `cross_val_score`. We will use the following parameters
# search:
# performed using `cross_val_score` on the training set. We will use the
# following parameters search:
# - `learning_rate` for the values 0.01, 0.1, 1 and 10. This parameter controls
# the ability of a new tree to correct the error of the previous sequence of
# trees
Expand All @@ -76,3 +76,11 @@

# %%
# Write your code here.

# %% [markdown]
#
# Now use the test set to score the model using the best parameters
# that we found using cross-validation in the training set.

# %%
# Write your code here.
21 changes: 19 additions & 2 deletions python_scripts/parameter_tuning_sol_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
# loops, make a search of the best combinations of the `learning_rate` and
# `max_leaf_nodes` parameters. In this regard, you will need to train and test
# the model by setting the parameters. The evaluation of the model should be
# performed using `cross_val_score`. We will use the following parameters
# search:
# performed using `cross_val_score` on the training set. We will use the
# following parameters search:
# - `learning_rate` for the values 0.01, 0.1, 1 and 10. This parameter controls
# the ability of a new tree to correct the error of the previous sequence of
# trees
Expand Down Expand Up @@ -100,3 +100,20 @@

print(f"The best accuracy obtained is {best_score:.3f}")
print(f"The best parameters found are:\n {best_params}")

# %% [markdown]
#
# Now use the test set to score the model using the best parameters
# that we found using cross-validation in the training set.

# %%
# solution
best_lr = best_params['learning-rate']
best_mln = best_params['max leaf nodes']

model.set_params(classifier__learning_rate=best_lr,
classifier__max_leaf_nodes=best_mln)
model.fit(data_train, target_train)
test_score = model.score(data_test, target_test)

print(f"Test score after the parameter tuning: {test_score:.3f}")

0 comments on commit 879ebe1

Please sign in to comment.