Skip to content

[MRG + 1] FIX raise an error message when n_groups > number of groups (#7681)#7683

Merged
amueller merged 3 commits intoscikit-learn:masterfrom
polmauri:raise-error
Oct 25, 2016
Merged

[MRG + 1] FIX raise an error message when n_groups > number of groups (#7681)#7683
amueller merged 3 commits intoscikit-learn:masterfrom
polmauri:raise-error

Conversation

@polmauri
Copy link
Copy Markdown
Contributor

@polmauri polmauri commented Oct 17, 2016

This change fixes issue #7681:

  • Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
  • Add unit test.

unique_groups = np.unique(groups)
if self.n_groups > len(unique_groups):
raise ValueError("Cannot have n_groups=%d greater than the number "
"of unique groups: %d."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i find it odd that you choose to use = to indicate the variable value in for n_groups and : for the number of unique groups; maybe keep it consistent?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I copied the format from this other error in the same file, should I change it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If all the file use =, you can keep =.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's okay as n_groups is a parameter and unique groups is the information we are trying to include in the error message...

@raghavrv
Copy link
Copy Markdown
Member

Thanks for the PR @polmauri

unique_groups = np.unique(groups)
if self.n_groups > len(unique_groups):
raise ValueError("Cannot have n_groups=%d greater than the number "
"of unique groups: %d."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's okay as n_groups is a parameter and unique groups is the information we are trying to include in the error message...

list(LeavePGroupsOut(n_groups=5).split(X, y, groups))

assert_raises(ValueError, leave_one_group_out_zero_groups)
assert_raises(ValueError, leave_p_groups_out_greater_than_unique_groups)
Copy link
Copy Markdown
Member

@raghavrv raghavrv Oct 17, 2016

Choose a reason for hiding this comment

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

Instead of defining functions you could pass the parameters to assert_raise_message / assert_raises and test these like

assert_raise_message(ValueError, "<snippet of the error message">,
                     LeaveOneGroupOut().split, X, y)

assert_equal(3, LeaveOneGroupOut().get_n_splits(X, y, groups))


def test_leave_group_out_split_errors():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

--> test_leave_one_p_group_out_error_on_fewer_number_of_groups

@tguillemot
Copy link
Copy Markdown
Contributor

Thanks @polmauri.
+1 once corrected.

X = y = groups = np.ones(10)
list(LeavePGroupsOut(n_groups=5).split(X, y, groups))

assert_raises(ValueError, leave_one_group_out_zero_groups)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add the ValueError message raised by leave_one_group_out_zero_groups to be sure it is the right message.

list(LeavePGroupsOut(n_groups=5).split(X, y, groups))

assert_raises(ValueError, leave_one_group_out_zero_groups)
assert_raises(ValueError, leave_p_groups_out_greater_than_unique_groups)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add the ValueError message raised by leave_one_group_out_zero_groups to be sure it is the right message.

@polmauri
Copy link
Copy Markdown
Contributor Author

Changed:

  • Check error message with assert_raise_message
  • Pass parameters to assert_raise_message instead of defining functions

…cikit-learn#7681)

This change addresses issue scikit-learn#7681:
- Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
- Add unit test.
- Check error message with `assert_raise_message`
- Pass parameters to `assert_raise_message` instead of defining functions
# We make a copy of groups to avoid side-effects during iteration
groups = np.array(groups, copy=True)
unique_groups = np.unique(groups)
if len(unique_groups) == 0:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

len(unique_groups) <= 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

needs a test.

groups = np.array(groups, copy=True)
unique_groups = np.unique(groups)
if len(unique_groups) == 0:
raise ValueError("Cannot have zero groups")
Copy link
Copy Markdown
Member

@raghavrv raghavrv Oct 19, 2016

Choose a reason for hiding this comment

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

"The groups parameter contains fewer than 2 unique groups (%s). LeaveOneGroupOut expects at least 2."
% (unique_groups)

raise ValueError("The groups parameter should not be None")
groups = np.array(groups, copy=True)
unique_groups = np.unique(groups)
if self.n_groups > len(unique_groups):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

>=

groups = np.array(groups, copy=True)
unique_groups = np.unique(groups)
if self.n_groups > len(unique_groups):
raise ValueError("Cannot have n_groups=%d greater than the number "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"The groups parameter contains fewer than (or equal to) n_groups (%d) numbers of unique groups"
" (%s). LeavePGroupsOut expects that atleast n_groups + 1 (%d) unique groups be present"
% (n_groups, unique_groups, n_groups + 1)

@amueller
Copy link
Copy Markdown
Member

@polmauri are you still working on this?

@polmauri
Copy link
Copy Markdown
Contributor Author

Changes requested done.

@raghavrv
Copy link
Copy Markdown
Member

Thanks for addressing all the comments. Looks good to me.

@raghavrv raghavrv changed the title FIX raise an error message when n_groups > number of groups (#7681) [MRG + 1] FIX raise an error message when n_groups > number of groups (#7681) Oct 25, 2016
@raghavrv raghavrv added this to the 0.18.1 milestone Oct 25, 2016
@amueller amueller merged commit 73d3f03 into scikit-learn:master Oct 25, 2016
@amueller
Copy link
Copy Markdown
Member

thanks :)

amueller pushed a commit to amueller/scikit-learn that referenced this pull request Oct 25, 2016
…scikit-learn#7681) (scikit-learn#7683)

* FIX raise an error message when n_groups > actual number of groups (scikit-learn#7681)

This change addresses issue scikit-learn#7681:
- Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
- Add unit test.

* Make requested changes

- Check error message with `assert_raise_message`
- Pass parameters to `assert_raise_message` instead of defining functions

* Update condition and exception message
amueller pushed a commit to amueller/scikit-learn that referenced this pull request Oct 27, 2016
…scikit-learn#7681) (scikit-learn#7683)

* FIX raise an error message when n_groups > actual number of groups (scikit-learn#7681)

This change addresses issue scikit-learn#7681:
- Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
- Add unit test.

* Make requested changes

- Check error message with `assert_raise_message`
- Pass parameters to `assert_raise_message` instead of defining functions

* Update condition and exception message
sergeyf pushed a commit to sergeyf/scikit-learn that referenced this pull request Feb 28, 2017
…scikit-learn#7681) (scikit-learn#7683)

* FIX raise an error message when n_groups > actual number of groups (scikit-learn#7681)

This change addresses issue scikit-learn#7681:
- Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
- Add unit test.

* Make requested changes

- Check error message with `assert_raise_message`
- Pass parameters to `assert_raise_message` instead of defining functions

* Update condition and exception message
Sundrique pushed a commit to Sundrique/scikit-learn that referenced this pull request Jun 14, 2017
…scikit-learn#7681) (scikit-learn#7683)

* FIX raise an error message when n_groups > actual number of groups (scikit-learn#7681)

This change addresses issue scikit-learn#7681:
- Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
- Add unit test.

* Make requested changes

- Check error message with `assert_raise_message`
- Pass parameters to `assert_raise_message` instead of defining functions

* Update condition and exception message
paulha pushed a commit to paulha/scikit-learn that referenced this pull request Aug 19, 2017
…scikit-learn#7681) (scikit-learn#7683)

* FIX raise an error message when n_groups > actual number of groups (scikit-learn#7681)

This change addresses issue scikit-learn#7681:
- Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
- Add unit test.

* Make requested changes

- Check error message with `assert_raise_message`
- Pass parameters to `assert_raise_message` instead of defining functions

* Update condition and exception message
maskani-moh pushed a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017
…scikit-learn#7681) (scikit-learn#7683)

* FIX raise an error message when n_groups > actual number of groups (scikit-learn#7681)

This change addresses issue scikit-learn#7681:
- Raise ValueError when n_groups > actual number of unique groups in LeaveOneGroupOut and LeavePGroupsOut.
- Add unit test.

* Make requested changes

- Check error message with `assert_raise_message`
- Pass parameters to `assert_raise_message` instead of defining functions

* Update condition and exception message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants