2 minute read

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.

image


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

image



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

X=(x1,,xn),Y=(y1,,yn)

d(X,Y)=(x1y1)2++(xnyn)2

Manhattan Distance (L1 Distance)Permalink

dManhattan(X,Y)=ni=1|xiyi|

Mahalanobis DistancePermalink

It takes into account the covariances of the data set. For two vectors x and y,

dMahalanobis(x,y)=(xy)T1(xy)

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

Σ1=[a11 a12a21 a22]

if dMahalanobis(x,y)=d, then we have

x21a11+2x1x2a12+x22a22=d2

image
image


Pearson Correlation DistancePermalink

For x=(x1,,xn) and y=(y1,,yn)

dCorr(x,y)=1ρxy[0,2]

where ¯x=1nni=1xi,¯y=1nni=1yi, and

ρxy=ni=1(xi¯x)(yi¯y)ni=1(xi¯x)2ni=1(yi¯y)2

image


Spearman’s Rank CorrelationPermalink

ρ(r)xy=ni=1nj=1(rjri)(sjsi)ni=1nj=1(rjri)2

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

ρ(r)xy=16n(n21)ni=1(risi)2


ni=1nj=1(rjri)(sjsi)
=ni=1nj=1risi+ni=1nj=1rjsjni=1nj=1risjni=1nj=1rjsi
=2nni=1risj2ni=1rinj=1sj
=2nni=1risi2(12n(n+1))2
=2nni=1risi12n2(n+1)2

From ni=1(risi)2=2r2i2risi, we have
ni=1nj=1(rjri)(sjsi)
=2nni=1r2i12n2(n+1)2nni=1(risi)2
=16n2(n21)nni=1(risi)2.

Further,

ni=1nj=1(rjri)2=2nni=1r2i2ni=1nj=1rirj
=2nni=1r2i2(ni=1ri)2=16n2(n21)

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