Distances in Knn: Are they metric functions in the Mathematical sense?

Click For Summary

Discussion Overview

The discussion revolves around whether the distance functions used in the K-nearest neighbors (KNN) algorithm in machine learning must satisfy the properties of a mathematical metric. Participants explore the implications of these properties on the performance of KNN in binary classification tasks.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant questions if distance functions in KNN must satisfy metric properties such as non-negativity, identity of indiscernibles, symmetry, and the triangle inequality.
  • Another participant suggests that while many metrics used in machine learning do satisfy the triangle inequality, some, like cosine distance, do not.
  • A participant proposes that the requirement for distance functions may not be strict, as functions that perform well could still be valid even if they do not meet all metric criteria.
  • Examples of distance functions commonly used in KNN, such as Pythagorean distance, taxicab distance, and Minkowski distance, are mentioned, with some noted to satisfy the corrected metric criteria.
  • There is a discussion about the implications of modifying Euclidean distance (e.g., adding 1) on the algorithm's performance, despite it not satisfying the triangle inequality.
  • Cosine similarity is highlighted as another distance measure that does not satisfy the triangle inequality but retains certain properties beneficial for clustering similar points.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of distance functions meeting all metric properties, with some arguing that certain functions may still be effective despite not being true metrics. The discussion remains unresolved regarding the strict requirements for distance functions in KNN.

Contextual Notes

Participants note that some distance functions may not adhere to all metric properties, such as those used in boolean spaces or modifications of traditional metrics. The implications of these deviations on the algorithm's effectiveness are not fully explored.

WWGD
Science Advisor
Homework Helper
Messages
7,785
Reaction score
13,050
TL;DR
The term 'distance' is used in Knn. Are these distance functions required to satisfy the properties of a metric?
Hi,
Just curious as to whether distances 'd' , used in Knn ; K nearest neighbors, in Machine Learning, are required to be metrics in the Mathematical Sense, i.e., if they are required to satisfy, in a space A:
##d: A \times A \rightarrow \mathbb R^{+} \cup \{0\} ;
d(a,a)=0 ;
d(a,b)=d(b,a) ; d(a,c) < d(a,b)+d(b,c) ##
?
 
Physics news on Phys.org
Suggestion: Expand on what you are talking about.
 
mathman said:
Suggestion: Expand on what you are talking about.
Sure. We're trying to ; a binary classification 0/1 , of test subjects with the following method.
I am simplifying somewhat

Basically an object belongs to the class of objects it is closest to, by some fixed criterion d.

0) Choose a positive integer k
1) We have objects known to belong to both class 0 and class 1
2) We have a collection of objects to test on whether they belong to class 0 or class 1.
3)We have a real -valued non-negative binary function d AxB --> R ; A is in 2), B is in 1)
4) We find the value of d(a,B) , i.e., { d(a,b) : a fixed, for all b in B}
5) a is assigned to the class of the object b for which d(a,b) is the smallest.

I suspect @pbuk, @Office_Shredder or @Dale may know.
 
Last edited:
WWGD said:
... ##d(a,c) < d(a,b)+d(b,c) ##
I think you mean ## d(a,c) \le d(a,b)+d(b,c) ##.

I am not sure we can say that the distance functions are required to satisfy any conditions, however we are only interested in functions that perform well and it may be possible to show that certain conditions must be satisfied in order for this to be the case.

Many metrics in ML do satisfy the triangle equality, however I don't think this is true for the cosine distance and I have a hunch this is also the case for some of the distances in boolean spaces e.g. Kuslinski distance.

Taking the other contemplated 'requirements':
  • ## d: A \times A \rightarrow \mathbb R^{+} \cup \{0\} ; ## clearly not the case for boolean metrics.
  • ## d(a,a)=0 ; ## I don't see this as necessary: Euclidian distance + 1 would work almost as well as Euclidean distance in most cases.
  • ## d(a,b)=d(b,a)## I'll leave a counterexample for this to you :-p
 
  • Like
Likes   Reactions: WWGD
  • Like
Likes   Reactions: jedishrfu and WWGD
I agree with the clever observation that euclidean distance +1 results in the exact same algorithm but does not satisfy the triangle inequality.

Cosine similarity does not satisfy the triangle inequality either, but does have the property that if A and B are similar, then the similarity of A and C is about the same as the similarity of B and C. Otherwise you can't really get clusters of points that are similar to each other and dissimilar from the other clusters.
 
Last edited:
  • Like
Likes   Reactions: pbuk and WWGD

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 23 ·
Replies
23
Views
965
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K