-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
FIX Raise an error when min_samples_split=1 in trees #25744
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
Merged
thomasjpfan
merged 4 commits into
scikit-learn:main
from
jeremiedbb:invalid-type-interval-cst
Mar 6, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -364,9 +364,12 @@ class Interval(_Constraint): | |
|
||
Parameters | ||
---------- | ||
type : {numbers.Integral, numbers.Real} | ||
type : {numbers.Integral, numbers.Real, "real_not_int"} | ||
The set of numbers in which to set the interval. | ||
|
||
If "real_not_int", only reals that don't have the integer type | ||
are allowed. For example 1.0 is allowed but 1 is not. | ||
|
||
left : float or int or None | ||
The left bound of the interval. None means left bound is -∞. | ||
|
||
|
@@ -392,14 +395,6 @@ class Interval(_Constraint): | |
`[0, +∞) U {+∞}`. | ||
""" | ||
|
||
@validate_params( | ||
{ | ||
"type": [type], | ||
thomasjpfan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"left": [Integral, Real, None], | ||
"right": [Integral, Real, None], | ||
"closed": [StrOptions({"left", "right", "both", "neither"})], | ||
} | ||
) | ||
def __init__(self, type, left, right, *, closed): | ||
super().__init__() | ||
self.type = type | ||
|
@@ -410,6 +405,18 @@ def __init__(self, type, left, right, *, closed): | |
self._check_params() | ||
|
||
def _check_params(self): | ||
if self.type not in (Integral, Real, "real_not_int"): | ||
raise ValueError( | ||
"type must be either numbers.Integral, numbers.Real or 'real_not_int'." | ||
f" Got {self.type} instead." | ||
) | ||
|
||
if self.closed not in ("left", "right", "both", "neither"): | ||
raise ValueError( | ||
"closed must be either 'left', 'right', 'both' or 'neither'. " | ||
f"Got {self.closed} instead." | ||
) | ||
|
||
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. With the removal of 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. Indeed, I added the missing validation |
||
if self.type is Integral: | ||
suffix = "for an interval over the integers." | ||
if self.left is not None and not isinstance(self.left, Integral): | ||
|
@@ -424,6 +431,11 @@ def _check_params(self): | |
raise ValueError( | ||
f"right can't be None when closed == {self.closed} {suffix}" | ||
) | ||
else: | ||
if self.left is not None and not isinstance(self.left, Real): | ||
raise TypeError("Expecting left to be a real number.") | ||
if self.right is not None and not isinstance(self.right, Real): | ||
raise TypeError("Expecting right to be a real number.") | ||
|
||
if self.right is not None and self.left is not None and self.right <= self.left: | ||
raise ValueError( | ||
|
@@ -447,8 +459,13 @@ def __contains__(self, val): | |
return False | ||
return True | ||
|
||
def _has_valid_type(self, val): | ||
if self.type == "real_not_int": | ||
return isinstance(val, Real) and not isinstance(val, Integral) | ||
return isinstance(val, self.type) | ||
|
||
def is_satisfied_by(self, val): | ||
if not isinstance(val, self.type): | ||
if not self._has_valid_type(val): | ||
return False | ||
|
||
return val in self | ||
|
This file contains hidden or 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
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.
Would it be good to have a description of these internally in the docstring? Just for community-devs that aren't familiar w/ what each of these are intended to mean?
8000 span>
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.
I added a description of the new option.
A short description of all the constraints and what they represent can be found here https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/utils/_param_validation.py#L28
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.
Looks good!
I was just mentioning mainly the
real_not_int
option, not the others.