Skip to content
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

KNN classifier labels #186

Closed
manisci opened this issue Jun 3, 2022 · 2 comments
Closed

KNN classifier labels #186

manisci opened this issue Jun 3, 2022 · 2 comments

Comments

@manisci
Copy link

manisci commented Jun 3, 2022

Hi,

Thank you for the great documentation and scripts. I think there is something missing in the method "_find_nearest" in the class "KNNClassifier". I will try to illustrate it with an example, let's assume the labels for the closest neighbors, sorted by closeness is nearest_label = [1 0 0 0 1] and we are using uniform distances so nearest_distance is [1 1 1 1 1]. We expect the prediction to be 0, since there are 3 zeros, but using the code in "_find_nearest", we would get 1.

import numpy as np
nearest_labels = np.array([1, 0, 0, 0, 1])
nearest_distances = np.array([1, 1, 1, 1, 1])
labels_distances = np.vstack((nearest_labels, nearest_distances));
labels_distances = labels_distances[:, labels_distances[0, :].argsort()]
i = np.nonzero(np.diff(labels_distances[0, :]))[0] + 1
i = np.insert(i, 0, 0)
label_scores = np.add.reduceat(labels_distances[1, :], I)
max_labels = nearest_labels[np.argmax(label_scores)]
print (max_labels)

In particular, I don't understand the reasoning for this line

max_labels = nearest_labels[np.argmax(label_scores)]

I believe this should be replaced by

ordered_labels = np.array(sorted(list(set(nearest_labels))))
max_labels = ordered_labels [np.argmax(label_scores)]

This would give the expected result of 0.

@manisci manisci changed the title KNN classifier scores and labels KNN classifier labels Jun 3, 2022
@eonu
Copy link
Owner

eonu commented Jun 3, 2022

Hey @manisci thanks for pointing this out, glad to see another user.

Will take a look!

eonu added a commit that referenced this issue Jun 7, 2022
eonu added a commit that referenced this issue Jun 7, 2022
@eonu
Copy link
Owner

eonu commented Jun 12, 2022

Fixed in #187.

@eonu eonu closed this as completed Jun 12, 2022
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

No branches or pull requests

2 participants