-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP Use score in tree hyperparameter notebook #503
Open
glemaitre
wants to merge
1
commit into
INRIA:main
Choose a base branch
from
glemaitre:score_tree_hyperparameter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -114,13 +114,23 @@ def plot_regression(model, X, y, ax=None): | |||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
plot_classification(tree_clf, data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
accuracy = tree_clf.score(data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') | ||||||||||||||||||||||||||
_ = plt.title(f"Shallow classification tree with max-depth of {max_depth}") | ||||||||||||||||||||||||||
_ = plt.title( | ||||||||||||||||||||||||||
f"Shallow classification tree with max-depth of {max_depth}" | ||||||||||||||||||||||||||
f"\n Accuracy of the fit: {accuracy:.2f}" | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
plot_regression(tree_reg, data_reg[data_reg_columns], | ||||||||||||||||||||||||||
data_reg[target_reg_column]) | ||||||||||||||||||||||||||
_ = plt.title(f"Shallow regression tree with max-depth of {max_depth}") | ||||||||||||||||||||||||||
plot_regression(tree_reg, data_reg[data_reg_columns], data_reg[target_reg_column]) | ||||||||||||||||||||||||||
accuracy = tree_reg.score(data_reg[data_reg_columns], data_reg[target_reg_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
_ = plt.title( | ||||||||||||||||||||||||||
f"Shallow regression tree with max-depth of {max_depth}" | ||||||||||||||||||||||||||
f"\n R$^2$ of the fit: {accuracy:.2f}" | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% [markdown] | ||||||||||||||||||||||||||
# Now, let's increase the `max_depth` parameter value to check the difference | ||||||||||||||||||||||||||
|
@@ -134,13 +144,24 @@ def plot_regression(model, X, y, ax=None): | |||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
plot_classification(tree_clf, data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
accuracy = tree_clf.score(data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') | ||||||||||||||||||||||||||
_ = plt.title(f"Deep classification tree with max-depth of {max_depth}") | ||||||||||||||||||||||||||
_ = plt.title( | ||||||||||||||||||||||||||
f"Shallow classification tree with max-depth of {max_depth}" | ||||||||||||||||||||||||||
f"\n Accuracy of the fit: {accuracy:.2f}" | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
plot_regression(tree_reg, data_reg[data_reg_columns], | ||||||||||||||||||||||||||
data_reg[target_reg_column]) | ||||||||||||||||||||||||||
_ = plt.title(f"Deep regression tree with max-depth of {max_depth}") | ||||||||||||||||||||||||||
accuracy = tree_reg.score(data_reg[data_reg_columns], data_reg[target_reg_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
_ = plt.title( | ||||||||||||||||||||||||||
f"Shallow regression tree with max-depth of {max_depth}" | ||||||||||||||||||||||||||
f"\n R$^2$ of the fit: {accuracy:.2f}" | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
Comment on lines
+159
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% [markdown] | ||||||||||||||||||||||||||
# For both classification and regression setting, we observe that | ||||||||||||||||||||||||||
|
@@ -160,15 +181,23 @@ def plot_regression(model, X, y, ax=None): | |||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
plot_classification(tree_clf, data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
accuracy = tree_clf.score(data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') | ||||||||||||||||||||||||||
_ = plt.title(f"Optimal depth found via CV: " | ||||||||||||||||||||||||||
f"{tree_clf.best_params_['max_depth']}") | ||||||||||||||||||||||||||
f"{tree_clf.best_params_['max_depth']}" | ||||||||||||||||||||||||||
f"\n Accuracy of the fit: {accuracy:.2f}") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
plot_regression(tree_reg, data_reg[data_reg_columns], | ||||||||||||||||||||||||||
data_reg[target_reg_column]) | ||||||||||||||||||||||||||
accuracy = tree_reg.score(data_reg[data_reg_columns], | ||||||||||||||||||||||||||
data_reg[target_reg_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
_ = plt.title(f"Optimal depth found via CV: " | ||||||||||||||||||||||||||
f"{tree_reg.best_params_['max_depth']}") | ||||||||||||||||||||||||||
f"{tree_reg.best_params_['max_depth']}" | ||||||||||||||||||||||||||
f"\n R$^2$ of the fit: {accuracy:.2f}") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% [markdown] | ||||||||||||||||||||||||||
# With this example, we see that there is not a single value that is optimal | ||||||||||||||||||||||||||
|
@@ -225,7 +254,11 @@ def plot_regression(model, X, y, ax=None): | |||||||||||||||||||||||||
tree_clf = DecisionTreeClassifier(max_depth=max_depth) | ||||||||||||||||||||||||||
plot_classification(tree_clf, data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
_ = plt.title(f"Decision tree with max-depth of {max_depth}") | ||||||||||||||||||||||||||
accuracy = tree_clf.score(data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
_ = plt.title(f"Decision tree with max-depth of {max_depth}" | ||||||||||||||||||||||||||
f"\n Accuracy of the fit: {accuracy:.2f}") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% [markdown] | ||||||||||||||||||||||||||
# As expected, we see that the blue blob on the right and the red blob on the | ||||||||||||||||||||||||||
|
@@ -252,7 +285,11 @@ def plot_regression(model, X, y, ax=None): | |||||||||||||||||||||||||
tree_clf = DecisionTreeClassifier(max_depth=max_depth) | ||||||||||||||||||||||||||
plot_classification(tree_clf, data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
_ = plt.title(f"Decision tree with max-depth of {max_depth}") | ||||||||||||||||||||||||||
accuracy = tree_clf.score(data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
_ = plt.title(f"Decision tree with max-depth of {max_depth}" | ||||||||||||||||||||||||||
f"\n Accuracy of the fit: {accuracy:.2f}") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
_, ax = plt.subplots(figsize=(11, 7)) | ||||||||||||||||||||||||||
|
@@ -274,15 +311,19 @@ def plot_regression(model, X, y, ax=None): | |||||||||||||||||||||||||
tree_clf = DecisionTreeClassifier(min_samples_leaf=min_samples_leaf) | ||||||||||||||||||||||||||
plot_classification(tree_clf, data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
accuracy = tree_clf.score(data_clf[data_clf_columns], | ||||||||||||||||||||||||||
data_clf[target_clf_column]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
_ = plt.title( | ||||||||||||||||||||||||||
f"Decision tree with leaf having at least {min_samples_leaf} samples") | ||||||||||||||||||||||||||
f"Decision tree with leaf having at least {min_samples_leaf} samples" | ||||||||||||||||||||||||||
f"\n Accuracy of the fit: {accuracy:.2f}") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% | ||||||||||||||||||||||||||
_, ax = plt.subplots(figsize=(10, 7)) | ||||||||||||||||||||||||||
_ = plot_tree(tree_clf, ax=ax, feature_names=data_clf_columns) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# %% [markdown] | ||||||||||||||||||||||||||
# This hyperparameter allows to have leaves with a minimum number of samples | ||||||||||||||||||||||||||
# and no further splits will be search otherwise. Therefore, these | ||||||||||||||||||||||||||
# hyperparameters could be an alternative to fix the `max_depth` | ||||||||||||||||||||||||||
# hyperparameter. | ||||||||||||||||||||||||||
# This hyperparameter allows to have leaves with a minimum number of samples and | ||||||||||||||||||||||||||
# no further splits will be search otherwise. Similarly, the rest of the above | ||||||||||||||||||||||||||
# mentioned hyperparameters can be tuned as an alternative to fixing the | ||||||||||||||||||||||||||
# `max_depth`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.