ValueError: Unrecognized metric 'cosine' - Cosine similarity isn't working for KNN
Created by: BexTuychiev
I have the following code:
from pyod.models.knn import KNN
knn = KNN(metric="cosine").fit(df)
but I am getting the error in the issue title. What can be the problem and how do I fix it? I tried setting algorithm
to brute
but didn't work. Here is the full stack trace:
KeyError Traceback (most recent call last)
File sklearn/metrics/_dist_metrics.pyx:252, in sklearn.metrics._dist_metrics.DistanceMetric.get_metric()
KeyError: 'cosine'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
Input In [95], in <cell line: 8>()
10 knn = KNN(n_neighbors=10, metric=metric, method=method)
12 # Find the inliers with the current KNN
---> 13 inliers = evaluate_outlier_classifier(knn, males_transformed, .55)
15 # Calculate and store RMSE into scores
16 scores[(metric, method)] = evaluate_regressor(inliers)
Input In [69], in evaluate_outlier_classifier(model, data, threshold)
1 def evaluate_outlier_classifier(model, data, threshold=.75):
----> 2 model.fit(data)
4 probs = model.predict_proba(data)
5 inliers = data[probs[:, 1] <= threshold]
File ~/ENTER/lib/python3.9/site-packages/pyod/models/knn.py:207, in KNN.fit(self, X, y)
203 self.tree_ = BallTree(X, leaf_size=self.leaf_size,
204 metric=self.metric,
205 **self.metric_params)
206 else:
--> 207 self.tree_ = BallTree(X, leaf_size=self.leaf_size,
208 metric=self.metric)
210 dist_arr, _ = self.neigh_.kneighbors(n_neighbors=self.n_neighbors,
211 return_distance=True)
212 dist = self._get_dist_by_method(dist_arr)
File sklearn/neighbors/_binary_tree.pxi:966, in sklearn.neighbors._ball_tree.BinaryTree.__init__()
File sklearn/metrics/_dist_metrics.pyx:254, in sklearn.metrics._dist_metrics.DistanceMetric.get_metric()
ValueError: Unrecognized metric 'cosine'
@yzhao062, can you please help?