I want cosine nearest neighbors with scikit-learn, but specifying algorithm='kd_tree' or 'ball_tree' with metric='cosine' fails.
Why this happens
KD-trees and ball trees rely on properties of true distance metrics. Cosine distance doesn't satisfy them, so scikit-learn doesn't allow it with those algorithms. Only brute-force search supports cosine distance.
Answer 1: Use brute force (or leave the algorithm on auto)
from sklearn.neighbors import NearestNeighbors
nn = NearestNeighbors(n_neighbors=5, metric="cosine",
algorithm="brute").fit(doc_vecs)
distances, indices = nn.kneighbors(query_vec.reshape(1, -1))
similarities = 1 - distances
algorithm="auto" also chooses brute force for cosine. Note that kneighbors returns cosine distances, so convert to similarities if you need them.
