Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 56 additions & 15 deletions python_scripts/trees_hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
Comment on lines +128 to +133
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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}"
)
r2 = 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: {r2:.2f}"
)


# %% [markdown]
# Now, let's increase the `max_depth` parameter value to check the difference
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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}"
)
r2 = 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: {r2:.2f}"
)


# %% [markdown]
# For both classification and regression setting, we observe that
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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`.