Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG+1] Add check_is_fitted to non standard functions #12279

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
merged 15 commits into from
Oct 19, 2018
Merged

[MRG+1] Add check_is_fitted to non standard functions #12279

merged 15 commits into from
Oct 19, 2018

Conversation

whiletruelearn
Copy link
Contributor

Reference Issues/PRs

Fixes #12276

What does this implement/fix? Explain your changes.

Adding check_is_fitted method to other non standard functions

Any other comments?

@NicolasHug
Copy link
Member

Thanks @whiletruelearn , can you add a test to make sure that the correct exception is raised, like the code snippets in #12276?

@whiletruelearn whiletruelearn changed the title Add check_is_fitted to non standard functions [WIP] Add check_is_fitted to non standard functions Oct 4, 2018
@NicolasHug
Copy link
Member

You can make sure the tests pass locally before pushing commits:

pytest sklearn/neighbors/tests/test_approximate.py

@NicolasHug
Copy link
Member

test_graphs should not be failing as it calls lshf.fit, maybe this has to do with the use of ignore_warnings ?

@whiletruelearn
Copy link
Contributor Author

@NicolasHug thanks . Had some trouble in setting up a proper dev environment. I think i have found the issue and fixed it with the last commit.

@@ -664,7 +665,7 @@ class from an array representing our data set and ask who's
For efficiency, `radius_neighbors` returns arrays of objects, where
each object is a 1D array of indices or distances.
"""
check_is_fitted(self, "_fit_method")
check_is_fitted(self, "_fit_X")
Copy link
Member

Choose a reason for hiding this comment

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

Does

check_is_fitted(self, "_fit_method", "_fit_X")

still work? I think it would be best as it still ensure the previous test is covered

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It works. Initially used _fit_method which was failing.

My understanding is that _fit_X is the attribute that needs to be checked for this functions.

Copy link
Member

Choose a reason for hiding this comment

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

I would leave check_is_fitted(self, "_fit_method", "_fit_X") then, it's safer and might cover some bugs that aren't caught in the tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay @NicolasHug . Will update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NicolasHug I Tried adding this and tested locally. The build fails when i do so.

The current implementation looks correct to me .

Because in check_is_fitted

if not all_or_any([hasattr(estimator, attr) for attr in attributes]):

, exception is raised even if one of the attribute is not present. Please correct me if i am wrong.

Copy link
Member

Choose a reason for hiding this comment

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

You can use all_or_any=any

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also made a slight change check_is_fitted(self, ["_fit_method", "_fit_X"], all_or_any=any)

@whiletruelearn whiletruelearn changed the title [WIP] Add check_is_fitted to non standard functions [MRG] Add check_is_fitted to non standard functions Oct 5, 2018
@whiletruelearn
Copy link
Contributor Author

@jnothman @NicolasHug Please review and let me know if i need to change something else.

@whiletruelearn
Copy link
Contributor Author

Slight ping @jnothman @NicolasHug

@jnothman
Copy link
Member

jnothman commented Oct 8, 2018

Please add a small |API| entry to the change log at doc/whats_new/v0.21.rst. Like the other entries there, please reference this pull request with :issue: and credit yourself (and other contributors if applicable) with :user:

@whiletruelearn
Copy link
Contributor Author

Thanks. I have updated @jnothman

@whiletruelearn
Copy link
Contributor Author

@jnothman @NicolasHug Kindly let me know if there are anymore changes to make. Thanks

:func: `NearestNeighbors.kneighbors`,
:func: `NearestNeighbors.radius_neighbors`,
:func: `NearestNeighbors.kneighbors_graph`,
:func: `NearestNeighbors.radius_neighbors_graph`
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest building the docs locally and checking the formatting is correct. Here you need to remove spaces after :func:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NicolasHug Sorry , I have corrected this. I was hoping the build would capture issues if any. I did try make html, but it seems to be taking lots of time. What would be the right way to test a small change like this ?

:func:`NearestNeighbors.kneighbors_graph`,
:func:`NearestNeighbors.radius_neighbors_graph`
without first fitting should raise `NotFittedError`.
:issue:`12279` by :user:`Krishna Sangeeth <whiletruelearn>`.
Copy link
Member

Choose a reason for hiding this comment

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

The formatting is still a bit messy. You can copy / paste this, it will do:

- |Fix| Fixed a bug in :class:`neighbors.NearestNeighbors` calling functions
  :func:`kneighbors <neighbors.NearestNeighbors.kneighbors>`,
  :func:`radius_neighbors <neighbors.NearestNeighbors.radius_neighbors>`,
  :func:`kneighbors_graph <neighbors.NearestNeighbors.kneighbors_graph>`, and
  :func:`radius_neighbors_graph
  <neighbors.NearestNeighbors.radius_neighbors_graph>` without first fitting
  should raise ``NotFittedError``. :issue:`12279` by :user:`Krishna Sangeeth
  <whiletruelearn>`.

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 @NicolasHug . I have updated.

@NicolasHug
Copy link
Member

Thanks @whiletruelearn , LGTM!

......................

- |Fix| Fixed a bug in :class:`neighbors.NearestNeighbors` calling functions
:func:`kneighbors <neighbors.NearestNeighbors.kneighbors>`,
Copy link
Member

Choose a reason for hiding this comment

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

:func:`kneighbors <neighbors.NearestNeighbors.kneighbors>`

is equivalent to

:func:`~neighbors.NearestNeighbors.kneighbors`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated.

:mod:`sklearn.neighbors`
......................

- |Fix| Fixed a bug in :class:`neighbors.NearestNeighbors` calling functions
Copy link
Member

Choose a reason for hiding this comment

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

It's not a big bug. Let's phrase it as "Methods x x x now raise NotFittedError, rather than AttributeError, when called before fit". Can label it |API| too.

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 correcting. I have updated @jnothman . Apologies for the delay.

@whiletruelearn
Copy link
Contributor Author

Appveyor build seems to be failing for some reason. Should i create another issue for this.

[00:10:19] ================================== FAILURES ===================================
[00:10:19] _____________________________ test_count_nonzero ______________________________
[00:10:19] 
[00:10:19]     def test_count_nonzero():
[00:10:19]         X = np.array([[0, 3, 0],
[00:10:19]                       [2, -1, 0],
[00:10:19]                       [0, 0, 0],
[00:10:19]                       [9, 8, 7],
[00:10:19]                       [4, 0, 5]], dtype=np.float64)
[00:10:19]         X_csr = sp.csr_matrix(X)
[00:10:19]         X_csc = sp.csc_matrix(X)
[00:10:19]         X_nonzero = X != 0
[00:10:19]         sample_weight = [.5, .2, .3, .1, .1]
[00:10:19]         X_nonzero_weighted = X_nonzero * np.array(sample_weight)[:, None]
[00:10:19]     
[00:10:19]         for axis in [0, 1, -1, -2, None]:
[00:10:19]             assert_array_almost_equal(count_nonzero(X_csr, axis=axis),
[00:10:19]                                       X_nonzero.sum(axis=axis))
[00:10:19]             assert_array_almost_equal(count_nonzero(X_csr, axis=axis,
[00:10:19]                                                     sample_weight=sample_weight),
[00:10:19]                                       X_nonzero_weighted.sum(axis=axis))
[00:10:19]     
[00:10:19]         assert_raises(TypeError, count_nonzero, X_csc)
[00:10:19]         assert_raises(ValueError, count_nonzero, X_csr, axis=2)
[00:10:19]     
[00:10:19]         assert (count_nonzero(X_csr, axis=0).dtype ==
[00:10:19]                 count_nonzero(X_csr, axis=1).dtype)
[00:10:19]         assert (count_nonzero(X_csr, axis=0, sample_weight=sample_weight).dtype ==
[00:10:19]                 count_nonzero(X_csr, axis=1, sample_weight=sample_weight).dtype)
[00:10:19]     
[00:10:19]         # Check dtypes with large sparse matrices too
[00:10:19]         X_csr.indices = X_csr.indices.astype(np.int64)
[00:10:19]         X_csr.indptr = X_csr.indptr.astype(np.int64)
[00:10:19] >       assert (count_nonzero(X_csr, axis=0).dtype ==
[00:10:19]                 count_nonzero(X_csr, axis=1).dtype)
[00:10:19] 
[00:10:19] X          = array([[ 0.,  3.,  0.],
[00:10:19]        [ 2., -1.,  0.],
[00:10:19]        [ 0.,  0.,  0.],
[00:10:19]        [ 9.,  8.,  7.],
[00:10:19]        [ 4.,  0.,  5.]])
[00:10:19] X_csc      = <5x3 sparse matrix of type '<type 'numpy.float64'>'
[00:10:19] 	with 8 stored elements in Compressed Sparse Column format>
[00:10:19] X_csr      = <5x3 sparse matrix of type '<type 'numpy.float64'>'
[00:10:19] 	with 8 stored elements in Compressed Sparse Row format>
[00:10:19] X_nonzero  = array([[False,  True, False],
[00:10:19]        [ True,  True, False],
[00:10:19]        [False, False, False],
[00:10:19]        [ True,  True,  True],
[00:10:19]        [ True, False,  True]])
[00:10:19] X_nonzero_weighted = array([[0. , 0.5, 0. ],
[00:10:19]        [0.2, 0.2, 0. ],
[00:10:19]        [0. , 0. , 0. ],
[00:10:19]        [0.1, 0.1, 0.1],
[00:10:19]        [0.1, 0. , 0.1]])
[00:10:19] axis       = None
[00:10:19] sample_weight = [0.5, 0.2, 0.3, 0.1, 0.1]
[00:10:19] 

@jnothman
Copy link
Member

Sorry for the failing test. See #12382

@whiletruelearn
Copy link
Contributor Author

@jnothman Slight ping. Anything else to look into :-)

Copy link
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

Just wait for another review, please

@whiletruelearn whiletruelearn changed the title [MRG] Add check_is_fitted to non standard functions [MRG+1] Add check_is_fitted to non standard functions Oct 17, 2018
Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM as well, merging. Thank you very much @whiletruelearn!

@ogrisel ogrisel merged commit 5bcd84b into scikit-learn:master Oct 19, 2018
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling NearestNeighbors.{kneighbors,radius_neighbors}_graph without first fitting should raise NotFittedError
4 participants