Skip to content

[WIP] New assert helpers for model comparison and fit reset checks #4841

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

Closed
wants to merge 4 commits into from

Conversation

raghavrv
Copy link
Member

Split from #3907 and #4162

  • New helpers for model comparison - assert_same_model, assert_not_same_model, assert_fitted_attributes_equal and assert_safe_sparse_allclose.
  • Check if all estimators reset upon fit

TODO

  • assert_safe_sparse_allcose - To support sparse/dense matrices. (naming inspired from safe_sparse_dot)
  • assert_same_model / assert_not_same_model / assert_fitted_attributes_equal
  • Unit tests for the assert helpers.
  • Check to make sure estimator resets when fit.

Partially fixes : #406

@raghavrv raghavrv force-pushed the new_assert_helpers branch from 3b8e8f7 to e70af31 Compare June 10, 2015 13:15
@raghavrv raghavrv changed the title [WIP] New assert helpers [MRG] New assert helpers Jun 10, 2015
"'regressor', 'transformer', 'cluster' or None, got"
" %s." % repr(type_filter))
"'regressor', 'transformer', 'cluster' or None,"
"got %s." % repr(type_filter))
Copy link
Member

Choose a reason for hiding this comment

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

you forgot a space between , and got

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks :)

@raghavrv raghavrv force-pushed the new_assert_helpers branch from e70af31 to 53a7637 Compare June 10, 2015 13:35
@raghavrv
Copy link
Member Author

@TomDLT done :)

@jnothman
Copy link
Member

I had meant that you might propose this in #4162 where at least it has one motivating example. We'll see how this is received.

# Check if the method(X) returns the same for both models.
res1 = getattr(estimator1, method)(X)
res2 = getattr(estimator2, method)(X)
same_model = (res1.shape == res2.shape) and np.allclose(res1, res2)
Copy link
Member

Choose a reason for hiding this comment

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

This will not handle transforms with sparse output.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@raghavrv
Copy link
Member Author

Thanks for the quick review!

I had meant that you might propose this in #4162 where at least it has one motivating example. We'll see how this is received.

Oh :P Anyway I'll rebase #4162 upon this! So this can be reviewed separately... :)

BTW do you feel we should add a test under estimator_checks to make sure this works for all the estimators? Sorry that is more or less what #4162 does!

@jnothman
Copy link
Member

Within limits, check_fit_reset will check this for all estimators. The
limits are: estimators with 2d float array input to fit, and only their
behaviour in that case (i.e. not testing sparse input effects)`.

On 11 June 2015 at 00:08, Raghav R V notifications@github.com wrote:

Thanks for the quick review!

I had meant that you might propose this in #4162
#4162 where at least
it has one motivating example. We'll see how this is received.

Oh :P Anyway I'll rebase #4162
#4162 upon this! So
this can be reviewed separately... :)

BTW do you feel we should add a test under estimator_checks to make sure
this works for all the estimators?


Reply to this email directly or view it on GitHub
#4841 (comment)
.

@raghavrv
Copy link
Member Author

i.e. not testing sparse input effects

Ok! so a separate test for sparse alone will suffice apart from having #4162 and #3907 right?

@jnothman
Copy link
Member

Might I hedge my bets and say "for now"?

On 11 June 2015 at 00:15, Raghav R V notifications@github.com wrote:

i.e. not testing sparse input effects

Ok! so a separate test for sparse alone will suffice apart from having
#4162 #4162 and #3907
#3907 right?


Reply to this email directly or view it on GitHub
#4841 (comment)
.

@raghavrv
Copy link
Member Author

haha okay :)

@raghavrv raghavrv force-pushed the new_assert_helpers branch from 53a7637 to 3341afb Compare June 13, 2015 21:08
@raghavrv raghavrv changed the title [MRG] New assert helpers [WIP] New assert helpers Jun 13, 2015
verbose=True):
"""Check if two sparse arrays are equal up to the desired tolerance.

It compares the difference between `actual` and `desired` to
Copy link
Member

Choose a reason for hiding this comment

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

The absolute difference, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean absolute difference as in the absolute difference between -1 and 1 is 0?
(I am pretty sure I misunderstood what u said... could you please expand a bit?)

Copy link
Member

Choose a reason for hiding this comment

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

The test compares the absolute value of the difference to the thing you mention. Not the difference. For more information take a look at the documentation of np.allclose.

Copy link
Member Author

Choose a reason for hiding this comment

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

That helps! Thanks

@vene
Copy link
Member

vene commented Jun 14, 2015

I agree with @jnothman that it's hard to review this PR as it is, because it's not clear how reusable these helpers are across the codebase. Are there any places in the existing tests where these helpers could be used?

Out of context as they are now, it's hard to say whether the API and implementation choices in this PR are the right ones.



def test_qda_same_model():
# NRT to make sure the rotations_ attribute is correctly compared
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean non-regression test? In general, could you document tests with docstrings with clear first lines?

For this test in particular, I don't see what it would test that the one above wouldn't, as LinearSVC and KMeans also have fitted attributes of their own. Am I missing anything?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for the lack of clarity in the comment... @amueller had pointed out in one comment that QDA was not correctly compared by the previous implementation of the assert_same_model. That was because rotations was a list of numpy arrays and so I thought it would be worthwhile to add a NRT for QDA alone!

Will update the comment to make it more clear!

Copy link
Member

Choose a reason for hiding this comment

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

Good, I think the docstring should explain this.

I would also suggest using stub estimators to more explicitly test these helpers. You could test this with something like

class Dummy(object):
    pass

def test_compare_attributes():
    a = Dummy()
    also_a = Dummy()
    not_a = Dummy()

    a.foo_ = [3, 10]
    also_a.foo_ = [3, 10]
    not_a.foo_ = [42]
    # assert a and also_a are the same model and not_a is not

This would make both the test more self-descriptive and any failures easier to pinpoint. You should probably use the classes from tests/test_base.py

@vene
Copy link
Member

vene commented Jun 14, 2015

Sorry, I was pretty chaotic in reviewing this, since I jumped from minor nitpicks to high-level concerns and back. I think it'd be best to discuss the high-level things first, just in case we decide to implement things completely differently. For example as Joel suggested we might want to just densify sparse attributes and leverage assert_allclose.

assert False, msg
assert_allclose(val1, val2, rtol=rtol, atol=atol, err_msg=msg)
else:
assert False, msg
Copy link
Member Author

Choose a reason for hiding this comment

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

@vene @jnothman Could you look at this implementation once? (This is still WIP as 30% of the tests (fit reset tests) don't pass... but I'd like to know if I am going in the right direction)

@raghavrv raghavrv force-pushed the new_assert_helpers branch from 7f8b5a5 to 35fdeaa Compare August 3, 2016 12:41
@raghavrv
Copy link
Member Author

raghavrv commented Aug 3, 2016

@jnothman Any suggestions on how to salvage this? Should we proceed on this?

@betatim
Copy link
Member

betatim commented Aug 29, 2016

I'd be interested in the assert_same_model for #7270. Do you want to keep working on this or should I pick that method (and its tests) out of this PR?

@raghavrv
Copy link
Member Author

Please go ahead and pick it up if you wish to :) Thanks for working on that!

@jnothman
Copy link
Member

I think you'll find getting this to merge is a bit of an uphill battle. In your case, Tim (and perhaps a variant applies in general), I also wonder whether pickle equality is what you want...

@jnothman
Copy link
Member

jnothman commented Jun 4, 2017

I'd like some discussion of this, perhaps at the sprint. Being able to easily asset that models are equal could simplify many tests or make them more rigorous

@betatim
Copy link
Member

betatim commented Jun 5, 2017

(I've not done any work on this, if someone else wants to go ahead please do!)

@raghavrv
Copy link
Member Author

raghavrv commented Jun 5, 2017

I can revive this if someone is interested in reviewing it... @jnothman is interested. @vene would you be available for the 2nd review?

@vene
Copy link
Member

vene commented Jun 5, 2017 via email

@jnothman
Copy link
Member

jnothman commented Jun 5, 2017 via email

@amueller
Copy link
Member

amueller commented Jun 5, 2017

Last time I checked, I felt it was not worth the effort, and I haven't thought about it since. Can someone maybe give use-cases?
And defining what equality of estimators is is just really hard.... Do we have a definition? Do all the private attributes need to be the same?

@haiatn
Copy link
Contributor

haiatn commented Jul 29, 2023

I like the thinking that went behind this but this seems complicated, I don't know we're at a point where this is needed yet

@adrinjalali
Copy link
Member

At this point if we're going to define estimator equality we probably needs a SLEP, and I haven't seen much of a usecase for it. Happy to have a fresh discussion on a SLEP of folks are interested though.

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.

factorize common tests
9 participants