-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
knn predict unreasonably slow b/c of use of scipy.stats.mode #13783
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
Comments
@amueller Do you mean something like this
|
That isn't going to apply to every row, and involves n_classes passes over
each. Basically because we know the set of class labels, we shouldn't need
to be doing unique.
Yes, we could construct a CSR sparse matrix and sum_duplicates and get the
argmax. Or we could just run bincount and argmax for each row. The question
is if it speeds things up enough to be worth the code.
It might also be possible to use np.add.at to effectively do bincount in
2d...?
|
Pretty sure doing a CSR construction would speed it up by several orders of magnitude. |
You're welcome to submit a pull request!
|
Cool! Will try it out |
Proposed a WIP solution in #14543 |
At #9597 (comment), @TomDLT pointed out that argmax of |
Yes, I'm happy with #9597 and using the argmax as well. Will try to make some benchmarks. |
I found that def predict(knn, X):
pro_y = knn.predict_proba(X)
y = np.argmax(pro_y, axis=1)
return y |
Yes, I need to finish #14543 to fix it |
spends all it's time in unique within stats.mode, not within the distance calculation.
mode
runsunique
for every row.I'm pretty sure we can replace the call to mode by some call to making a csr matrix and then argmax.
How much is it worth optimizing this? I feel KNN should be fast in low dimensions and people might actually use this. Having the bottleneck in the wrong place just feels wrong to me ;)
The text was updated successfully, but these errors were encountered: