Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • P pyod
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 144
    • Issues 144
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 16
    • Merge requests 16
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Yue Zhao
  • pyod
  • Issues
  • #445
Closed
Open
Issue created Oct 09, 2022 by Administrator@rootContributor

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?

Assignee
Assign to
Time tracking