Repository navigation
Conversation
|
Hmmm... I think I incorrectly triaged #9868. I briefly looked at the locally linear embedding code and saw that it could accept a NearestNeighbors object instead of the input data, which implied to me that the embedding is based on nearest neighborhoods and not on the original feature space. In my haste, I did not see that it extracts |
|
On the contrary, I think we should patch the code to enforce that the NearestNeighbors metric in use is 'euclidean'! |
|
That seems more consistent with the algorithm. |
|
Sorry to discard all your work here. Thanks for the contribution.
…On 9 October 2017 at 20:17, Patrick Fernandes ***@***.***> wrote:
That seems more consistent with the algorithm.
So basically check that, if the input is in the form of a
NearestNeighbors, check if the metric is euclidean?
I will work on it
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#9888 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz66oSOT1Qgf18OiHMg12dvJFWuMuLks5sqeSZgaJpZM4Pxw4w>
.
|
|
@jnothman |
| nbrs.fit(X) | ||
|
|
||
| if isinstance(X, (BallTree, KDTree, NearestNeighbors)) and \ | ||
| not (X.metric == 'euclidean' |
There was a problem hiding this comment.
I think you can use X.effective_metric_ == 'euclidean' here. which is pretty much the same as metric == 'euclidean' or 'minkowki' with p=2.
There was a problem hiding this comment.
I did it because according to the specification of the Nearest Neighbors, the effective metric is only set after you call the fit method, which might not be the case since I now changed the verification to before the fit method (to save computation).
| def test_invalid_metric(): | ||
| rand = np.random.RandomState(0) | ||
| X = rand.randint(0, 100, size=(20, 3)) | ||
| NN = neighbors.NearestNeighbors(2, metric='manhattan').fit(X) |
There was a problem hiding this comment.
Don't use capitals for variable names (see PEP8). X and Y are a notable exception in scikit-learn for historical reasons.
There was a problem hiding this comment.
Fixed (although the previous test has it).
| if isinstance(X, (BallTree, KDTree, NearestNeighbors)) and \ | ||
| not (X.metric == 'euclidean' | ||
| or (X.metric == 'minkowski' and X.p == 2)): | ||
| raise ValueError("metric must be euclidean or equivalent") |
There was a problem hiding this comment.
Best practice: put the offending value in the exception. I tend to like error messages like this.
"'parameter' should be have this value for this and this reason. Got 'parameter={}' instead.".format(value)| raise ValueError("unrecognized method '%s'" % method) | ||
|
|
||
| nbrs = NearestNeighbors(n_neighbors=n_neighbors + 1, n_jobs=n_jobs) | ||
|
|
There was a problem hiding this comment.
No need to add a new line here. In general only change the minimal/required amount of things to make the review process easier and more focussed.
|
|
||
| msg = "metric must be euclidean or equivalent" | ||
| assert_raise_message(ValueError, msg, manifold.locally_linear_embedding, | ||
| NN, 2, 1, method='standard', eigen_solver='arpack') |
There was a problem hiding this comment.
I don't think method and eigen_solver are needed, right?
I would use keyword parameters, i.e. n_neighbors=2, n_components=1 because it's more readable (I don't think people know the signature of locally_linear_embedding by heart).
There was a problem hiding this comment.
Regarding the first, fixed.
Regarding the second, kept just so it's uniform with the previous test (but can change both)
|
Should be all set by now. |
|
@CoderPat @jnothman Hi - I am not sure your conclusions here are correct: See for example this paper [1], where a genetic distance metric is used The reconstruction error is dependent on the weights that are calculated, which differ on how the neighbourhood is chosen. Using a different distance metric that better encapsulate the underlying manifold will make it easier to find good reconstruction weights, thus reducing the error. I'd be interested in hearing what you think - I hope this is the right place to raise this Thank you! Edit: See also this reference paper, especially Section 4, for further details on neighbourhood selection [2] [1] Siu, Hoicheong, Li Jin, and Momiao Xiong. "Manifold learning for human population structure studies." PloS one 7.1 (2012): e29901. (Open Access: http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0029901) |
|
Hi @CoderPat , it has been a while, I'm really sorry about that. Are you still interested in working on that? Would you be able to synchronize with upstream? Thanks a lot for your patience! |
|
Didn't even remember this anymore 😅 It should be relatively easy to merge with upstream, I'll try to do it this week |
Co-authored-by: Chiara Marmo <[email protected]>
Co-authored-by: Chiara Marmo <[email protected]>
Co-authored-by: Chiara Marmo <[email protected]>
| if isinstance(X, (BallTree, KDTree, NearestNeighbors)) and \ | ||
| not (X.metric == 'euclidean' | ||
| or (X.metric == 'minkowski' and X.p == 2)): | ||
| raise ValueError("metric must be euclidean or equivalent. " | ||
| "Got metric={} and p={}".format(X.metric, X.p)) |
There was a problem hiding this comment.
I think it is better to make the equivalence explicit here.
| if isinstance(X, (BallTree, KDTree, NearestNeighbors)) and \ | |
| not (X.metric == 'euclidean' | |
| or (X.metric == 'minkowski' and X.p == 2)): | |
| raise ValueError("metric must be euclidean or equivalent. " | |
| "Got metric={} and p={}".format(X.metric, X.p)) | |
| if ( | |
| isinstance(X, (BallTree, KDTree, NearestNeighbors)) and | |
| not (X.metric == 'euclidean' or (X.metric == 'minkowski' and X.p == 2)) | |
| ): | |
| raise ValueError("metric must be 'euclidean' or must be 'minkowski' " | |
| with p=2. Got metric={} and p={}".format(X.metric, X.p)) |
| X = rand.randint(0, 100, size=(20, 3)) | ||
| nbrs = neighbors.NearestNeighbors(n_neighbors=2, metric='manhattan').fit(X) | ||
|
|
||
| msg = "metric must be euclidean or equivalent" |
There was a problem hiding this comment.
| msg = "metric must be euclidean or equivalent" | |
| msg = "metric must be 'euclidean' or must be 'minkowski' with p=2" |
|
|
||
| nbrs = NearestNeighbors(n_neighbors=n_neighbors + 1, n_jobs=n_jobs) | ||
|
|
||
| if isinstance(X, (BallTree, KDTree, NearestNeighbors)) and \ |
There was a problem hiding this comment.
This test, allowing BallTree and KDTree, is not coherent with the current documentation of X:
X : {array-like, NearestNeighbors}
Sample data, shape = (n_samples, n_features), in the form of a
numpy array or a NearestNeighbors object.
|
@CoderPat Do you still have the time/interest to work on this? If not, I can pick it up and finish the last few updates required. |
|
Hey I'm a bit busy this week, but could potentially do it over the weekend |
After looking through the implementation, I don't think any part of the Also, here is another example using a non-Euclidean metric in LLE. Specifically, under their discussion of the algorithm they note that it can be readily generalized to more sophisticated choices of metric, including locally-defined metrics. Thoughts @jnothman ? |
|
Closing, there seems to be some interest in continuing this in a fresh PR. |
Reference Issue
Fixes #9868
What does this implement/fix? Explain your changes.
The LocallyLinearEmbedding class uses the NearestNeighbors class to calculate the K nearest neighbors yet only allowed the use of the euclidean distance metric to calculate those. I added the arguments needed to the class (and all functions used by it) in order to allow any valid metric for nearest neighbors to be used. Also added a test to ensure that another metric can be used.
Any other comments?
I'm not entirely sure of the utility of changing the metric from the euclidean to another one since the error function is calculated based on the euclidean metric. Maybe I also need to change the error calculation function to be dependent on the metric? Either way it is at least possible to choose the neighbors for the embeding based the user choice of metric.
Apreciate any feedback since its my first contribution