[ML] K-Nearest Neighbors (KNN)
K Nearest Neighbors; KNNPermalink
K Nearest Neighbors AlgorithmPermalink
The K-nearest neighbors (KNN) algorithm is a simple, easy-to-implement supervised machine learning algorithm that can be used to solve both classification and regression problems. The KNN algorithm assumes that similar things exist in close proximity. In other words, similar things are near to each other.
Consider a new blue data and the objective is to classify it.
When used in regression, the value of a new data z is given by the average of the values of its K neighbors in N.
The KNN algorithm has to compute all distances between a new data and neighbor data, which results in high computational cost. There exist some algorithms to reduce the computational cost such as
- Locality Sensitive Hashing
- Network based Indexer
- Optimized product quantization
Example. Decision boundary - Comparison with Different K
Distance metricsPermalink
Similarity can be measured in terms of distance. There are a number of different distance metrics as given below.
Euclidean Distance (L2 Distance)Permalink
d(X,Y)=√(x1−y1)2+⋯+(xn−yn)2
Manhattan Distance (L1 Distance)Permalink
Mahalanobis DistancePermalink
It takes into account the covariances of the data set. For two vectors x and y,
where Σ denotes the covariance matrix of the data set.
To understand Mahalanobis distance, consider two points x=(x1,x2) and y=(0,0) in R2. For a12=a21 and
if dMahalanobis(x,y)=d, then we have
Pearson Correlation DistancePermalink
For x=(x1,⋯,xn) and y=(y1,⋯,yn)
where ¯x=1n∑ni=1xi,¯y=1n∑ni=1yi, and
Spearman’s Rank CorrelationPermalink
where ri and si denote the ranks of xi and yi in x and y, respectively.
Note. When there are no ties, we see that
∑ni=1∑nj=1(rj−ri)(sj−si)
=∑ni=1∑nj=1risi+∑ni=1∑nj=1rjsj−∑ni=1∑nj=1risj−∑ni=1∑nj=1rjsi
=2n∑ni=1risj−2∑ni=1ri∑nj=1sj
=2n∑ni=1risi−2(12n(n+1))2
=2n∑ni=1risi−12n2(n+1)2
From ∑ni=1(ri−si)2=2∑r2i−2∑risi, we have
∑ni=1∑nj=1(rj−ri)(sj−si)
=2n∑ni=1r2i−12n2(n+1)2−n∑ni=1(ri−si)2
=16n2(n2−1)−n∑ni=1(ri−si)2.
Further,
∑ni=1∑nj=1(rj−ri)2=2n∑ni=1r2i−2∑ni=1∑nj=1rirj
=2n∑ni=1r2i−2(∑ni=1ri)2=16n2(n2−1)
By substituting into the original formula these results, done .◼
ReferencePermalink
[1] Christopher M. Bishop, Pattern Recognition and Machine Learning, Springer 2006.
[2] Spearman’s rank correlation coefficient
Leave a comment