Skip to content

Conversation

tttthomasssss
Copy link
Contributor

@tttthomasssss tttthomasssss commented Aug 17, 2017

Reference Issue

Fixes #9199

What does this implement/fix? Explain your changes.

The fix simply extends the current sparse input incompatibility check with a callable check.

Any other comments?

A test is included in test_neighbors.py

@jnothman
Copy link
Member

Unfortunately the earliest supported version of scipy does not have count_nonzero. Is np.diff(X.indptr) sufficient?

Also, you should use PEP8 to pass our tests.

@tttthomasssss
Copy link
Contributor Author

I've just modified my PR - apologies for the pep8 mess...

@jnothman
Copy link
Member

Tests are failing. Can you please make the title of the PR more descriptive as well?

@tttthomasssss tttthomasssss changed the title Fixes #9199 Fixes #9199 - Fitting a NearestNeighbors model fails with sparse input and a callable as metric Aug 24, 2017
@tttthomasssss
Copy link
Contributor Author

@jnothman - do you know which ones are affected by my change? I've removed my fix and the same tests (+ the one I added) are failing in comparison to with my fix (- the one I added).

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.

LGTM

@@ -1255,6 +1255,35 @@ def test_dtype_convert():
assert_array_equal(result, y)


def test_sparse_metric_callable():
def sparse_metric(x, y): # Metric accepting sparse matrix input (only)

Copy link
Member

Choose a reason for hiding this comment

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

remove blank line, or put comment here.

@jnothman jnothman changed the title Fixes #9199 - Fitting a NearestNeighbors model fails with sparse input and a callable as metric [MRG+1] Fixes #9199 - Fitting a NearestNeighbors model fails with sparse input and a callable as metric Sep 4, 2017
def test_sparse_metric_callable():
def sparse_metric(x, y): # Metric accepting sparse matrix input (only)

return min(x.nnz, y.nnz) / max(x.nnz, y.nnz)
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I have to take back the LGTM. :\ This is apparently producing different results on old scipy, and tests are failing. Perhaps just assert isspmatrix(x) and return some more ordinary function of the inputs.

@jnothman jnothman changed the title [MRG+1] Fixes #9199 - Fitting a NearestNeighbors model fails with sparse input and a callable as metric Fixes #9199 - Fitting a NearestNeighbors model fails with sparse input and a callable as metric Sep 4, 2017
@tttthomasssss
Copy link
Contributor Author

OK, finally managed to get back to this, the metric now returns the dot product of the inputs

@jnothman jnothman changed the title Fixes #9199 - Fitting a NearestNeighbors model fails with sparse input and a callable as metric [MRG+1] Fixes #9199 - Fitting a NearestNeighbors model fails with sparse input and a callable as metric Sep 26, 2017
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.

Thanks. LGTM. Properly this time :)

@TomDLT
Copy link
Member

TomDLT commented Dec 6, 2017

LGTM, thanks for the fix @tttthomasssss

@TomDLT TomDLT merged commit ece341c into scikit-learn:master Dec 6, 2017
@jnothman
Copy link
Member

jnothman commented Dec 6, 2017

There's no what's new entry. My bad :(

@tttthomasssss, would you be able to open a new PR which adds an entry to the change log at doc/whats_new/v0.20.rst. Like the other entries there, please reference this pull request with :issue: and credit yourself (and other contributors if applicable) with :user:

tttthomasssss added a commit to tttthomasssss/scikit-learn that referenced this pull request Dec 14, 2017
jnothman pushed a commit that referenced this pull request Dec 14, 2017
jwjohnson314 pushed a commit to jwjohnson314/scikit-learn that referenced this pull request Dec 18, 2017
jwjohnson314 pushed a commit to jwjohnson314/scikit-learn that referenced this pull request Dec 18, 2017
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.

Fitting a NearestNeighbors model fails with sparse input and a callable as metric
3 participants