8000 FIX sklearn.tree: fix validation of class_names argument for plot_tree by 2maz · Pull Request #26903 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

Conversation

2maz
Copy link
Contributor
@2maz 2maz commented 10BC0 Jul 26, 2023

Reference Issues/PRs

What does this implement/fix? Explain your changes.

In sklearn.tree.plot_tree: allow documented use of 'class_names' arguments, i.e. setting class_names=True

Currently this raises:

 sklearn.utils._param_validation.InvalidParameterError: The 'class_names' parameter of plot_tree must be an instance of 'list' or None. Got True instead.

Replicate with:

from sklearn.datasets import load_iris
from sklearn import tree

clf = tree.DecisionTreeClassifier(random_state=0)
iris = load_iris()

clf = clf.fit(iris.data, iris.target)

# Plotting class_names=True will raise
tree.plot_tree(clf, class_names=True)

Any other comments?

Reproducible with the above code snippet

@2maz 2maz changed the title sklearn.tree: Fix validation of class_names argument for plot_tree FIX sklearn.tree: fix validation of class_names argument for plot_tree Jul 26, 2023
@github-actions
Copy link
github-actions bot commented Jul 26, 2023

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 6feb25f. Link to the linter CI: here

Copy link
Contributor
@Micky774 Micky774 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR @2maz! Nice simple change. Looks good, with one minor change in addition to what you've already done.

"max_depth": [Interval(Integral, 0, None, closed="left"), None],
"feature_names": [list, None],
"class_names": [list, None],
"class_names": [list, None, "boolean"],
Copy link
Contributor

Choose a reason for hiding this comment

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

While we're here, we should probably also change list to "array-like" similarly to export_graphviz. Also, totally just a nit-pick, but I like keeping None as the final option by convention :)

Suggested change
"class_names": [list, None, "boolean"],
"class_names": ["array-like", "boolean", None],

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the quick feedback. Just updated accordingly.

@Micky774 Micky774 added Quick Review For PRs that are quick to review Validation related to input validation Waiting for Second Reviewer First reviewer is done, need a second one! labels Jul 26, 2023
@Micky774
Copy link
Contributor

@2maz Just for future reference, please avoid force-pushing commits in these PRs. It's totally okay for now, but in general we prefer to maintain the commit history within the branch/PR. When, eventually, we merge these into main we will squash all commits into one to keep the history of main clean, so there is no need to do so within this branch/PR.

It helps for keeping history, debugging, and following development. Let me know if you have any questions/concerns regarding this. Thanks :)

Copy link
Member
@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

Thank you for the PR @2maz !

@thomasjpfan thomasjpfan added this to the 1.3.1 milestone Jul 26, 2023
@thomasjpfan
Copy link
Member

I think this is a bug fix and should be in 1.3.1.

Please add an entry to the change log at doc/whats_new/v1.3.rst with tag |Fix|. Like the other entries there, please reference this pull request with :pr: and credit yourself (and other contributors if applicable) with :user:.

@thomasjpfan thomasjpfan merged commit b8d4f46 into scikit-learn:main Jul 27, 2023
punndcoder28 pushed a commit to punndcoder28/scikit-learn that referenced this pull request Jul 29, 2023
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Sep 18, 2023
jeremiedbb pushed a commit that referenced this pull request Sep 20, 2023
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
REDVM pushed a commit to REDVM/scikit-learn that referenced this pull request Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug module:tree Quick Review For PRs that are quick to review Validation related to input validation Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0