Line 28: Line 28:
 
The basic idea of k-NN is to extend the neighborhood, until the k nearest values are included. If we consider the neighborhood around x as a sphere, the volume of the sphere is given by,
 
The basic idea of k-NN is to extend the neighborhood, until the k nearest values are included. If we consider the neighborhood around x as a sphere, the volume of the sphere is given by,
 
<math>v_{k}(x_{0}) = \underset{h}{min}\left \{ \frac{\pi ^{h/2}}{\Gamma (\frac{n}{2} + 1)} \right \}</math>
 
<math>v_{k}(x_{0}) = \underset{h}{min}\left \{ \frac{\pi ^{h/2}}{\Gamma (\frac{n}{2} + 1)} \right \}</math>
<center>[[Image:Eqn2.gif|border]]</center>
 
  
where [[Image:Eqn3.gif|border]]
+
where <math>\Gamma (n) = (n - 1)!</math>
  
If x<sub>l</sub> is the k<sup>th</sup> closest sample point to x, then [[Image:Eqn4.gif|border]]
+
If x<sub>l</sub> is the k<sup>th</sup> closest sample point to x, then <math> h_{k} = \parallel x_{l} - x_{0}\parallel </math>
  
[[Image:Eqn5.gif]]
+
<math>v_{k}(x_{0}) =  \frac{\pi ^{n/2}}{\Gamma (\frac{n}{2} + 1)} . h_{k}^{n}</math>
  
We approximate the density p(x) by, [[Image:Eqn6.gif]]
+
We approximate the density p(x) by, <math>\bar{\rho_{k}}(x_{0}) = \frac{k - \# \ of \ samples \ on \ boundary \ of \ circle \ with \radius \ h_{k} }{N.V_{k}(x_{0})}</math>
 
   
 
   
Most of the time this estimate is, [[Image:Eqn7.gif]] and [[Image:Eqn8.gif]]
+
Most of the time this estimate is, <math>\bar{\rho_{k}}(x_{0}) = \frac{k - 1 }{N.V_{k}(x_{0})}</math> and <math>E(\bar{\rho_{k}}(x_{0})) = \rho(x_{0})</math>
  
 
== How to classify data using k-NN Density Estimate ==
 
== How to classify data using k-NN Density Estimate ==
Line 51: Line 50:
 
We now pick a k<sub>i</sub> for each class and a window function, and we try to approximate the density at x<sub>0</sub> for each class and then pick the class with the largest density based on, <br/>
 
We now pick a k<sub>i</sub> for each class and a window function, and we try to approximate the density at x<sub>0</sub> for each class and then pick the class with the largest density based on, <br/>
  
{equation here}
+
<math>\rho(x_{i}\mid \omega _{i}) = \frac{k_{i} - 1 }{N_{i}.V_{k_{i}}(x_{0})}</math>
  
If the priors of the classes are unknown, we use ROC curves to estimate the priors, based on,
+
If the priors of the classes are unknown, we use ROC curves to estimate the priors.
 
+
{equation here}
+
  
 
<b>Method 2:</b> </br>
 
<b>Method 2:</b> </br>
Line 62: Line 59:
  
 
We then approximate p(x, w<sub>i</sub>) by, <br/>
 
We then approximate p(x, w<sub>i</sub>) by, <br/>
{equation here}</br>
+
<math>\rho(x_{i}\mid \omega _{i}) = \frac{k_{i} - 1 }{N_{i}.V_{k_{i}}(x_{0})}</math></br>
  
 
where V<sub>i</sub> is the volume of the smallest window that contains k samples and k<sub>i</sub> is the number of samples among these k that belongs to class i. <br/>
 
where V<sub>i</sub> is the volume of the smallest window that contains k samples and k<sub>i</sub> is the number of samples among these k that belongs to class i. <br/>
  
 
We pick a class i<sub>0</sub> such that,
 
We pick a class i<sub>0</sub> such that,
Prob(wi0|x0) >= Prob(wi|x0), for all i = 1,2,...,C
+
<math>Prob (\omega _{io}\mid x_{0})\geq Prob (\omega _{i}\mid x_{0}), \forall \ i = 1,2,..,c</math>
 +
<math>\Rightarrow \rho(x_{0}\mid \omega _{i0}).Prob (\omega _{io})\geq \rho(x_{0}\mid \omega _{i}).Prob (\omega _{i}), \forall \ i = 1,2,..,c</math>
 +
<math>\Rightarrow \rho(x_{0}, \omega _{i0}) \geq \rho(x_{0},\omega _{i}), \forall \ i = 1,2,..,c</math>
  
 +
<math>k_{i0} \geq k_{i}, \forall \ i = 1,2,...,c</math>
  
 
It boils down to assigning a class based on the majority vote of the k-nearest neighbors.
 
It boils down to assigning a class based on the majority vote of the k-nearest neighbors.

Revision as of 10:36, 30 April 2014


K-Nearest Neighbors Density Estimation

A slecture by CIT student Raj Praveen Selvaraj

Partly based on the ECE662 Spring 2014 lecture material of Prof. Mireille Boutin.



Introduction

This slecture discusses about the K-Nearest Neighbors(k-NN) approach to estimate the density of a given distribution. The approach of K-Nearest Neighbors is very popular in signal and image processing for clustering and classification of patterns. It is an non-parametric density estimation technique which lets the region volume be a function of the training data. We will discuss the basic principle behind the k-NN approach to estimate density at a point X and then move on to building a classifier using the k-NN Density estimate.

Basic Principle

The general formulation for density estimation states that, for N Observations x1,x2,x3,...,xn the density at a point x can be approximated by the following function,

Knn1.jpg

where V is the volume of some neighborhood(say A) around x and k denotes the number of observations that are contained within the neighborhood. The basic idea of k-NN is to extend the neighborhood, until the k nearest values are included. If we consider the neighborhood around x as a sphere, the volume of the sphere is given by, $ v_{k}(x_{0}) = \underset{h}{min}\left \{ \frac{\pi ^{h/2}}{\Gamma (\frac{n}{2} + 1)} \right \} $

where $ \Gamma (n) = (n - 1)! $

If xl is the kth closest sample point to x, then $ h_{k} = \parallel x_{l} - x_{0}\parallel $

$ v_{k}(x_{0}) = \frac{\pi ^{n/2}}{\Gamma (\frac{n}{2} + 1)} . h_{k}^{n} $

We approximate the density p(x) by, $ \bar{\rho_{k}}(x_{0}) = \frac{k - \# \ of \ samples \ on \ boundary \ of \ circle \ with \radius \ h_{k} }{N.V_{k}(x_{0})} $

Most of the time this estimate is, $ \bar{\rho_{k}}(x_{0}) = \frac{k - 1 }{N.V_{k}(x_{0})} $ and $ E(\bar{\rho_{k}}(x_{0})) = \rho(x_{0}) $

How to classify data using k-NN Density Estimate

Having seen how the density at a given point x is estimated based on the value of k and the given observations x1,x2,x3,...,xn, let's discuss about using the k-NN density estimate for classification. </br>

Method 1:

Let x0 from Rn be a point to classify.

Given are samples xi1,xx2,..,xxn for i classes.

We now pick a ki for each class and a window function, and we try to approximate the density at x0 for each class and then pick the class with the largest density based on,

$ \rho(x_{i}\mid \omega _{i}) = \frac{k_{i} - 1 }{N_{i}.V_{k_{i}}(x_{0})} $

If the priors of the classes are unknown, we use ROC curves to estimate the priors.

Method 2: </br>

Given are samples xi1,xx2,..,xxn from a Gaussian Mixture. We choose a single value of k and and one window function,

We then approximate p(x, wi) by,
$ \rho(x_{i}\mid \omega _{i}) = \frac{k_{i} - 1 }{N_{i}.V_{k_{i}}(x_{0})} $</br>

where Vi is the volume of the smallest window that contains k samples and ki is the number of samples among these k that belongs to class i.

We pick a class i0 such that, $ Prob (\omega _{io}\mid x_{0})\geq Prob (\omega _{i}\mid x_{0}), \forall \ i = 1,2,..,c $ $ \Rightarrow \rho(x_{0}\mid \omega _{i0}).Prob (\omega _{io})\geq \rho(x_{0}\mid \omega _{i}).Prob (\omega _{i}), \forall \ i = 1,2,..,c $ $ \Rightarrow \rho(x_{0}, \omega _{i0}) \geq \rho(x_{0},\omega _{i}), \forall \ i = 1,2,..,c $

$ k_{i0} \geq k_{i}, \forall \ i = 1,2,...,c $

It boils down to assigning a class based on the majority vote of the k-nearest neighbors.

Computational Complexity

k-NN Algorithm stores all the training data samples. Suppose we have n samples each of dimension k, k-NN takes,

  • O(d) time to calculate a single distance
  • O(nd) time to find one closest neighbor
  • O(knd) time to find k closest neighbors
  • Giving a total complexity of O(knd)

Advantages and DisAdvantages of k-NN

K-NN is a simple and intuitive algorithm that can be applied to any kind of distribution. It gives a very good classification rate when the number of samples is large enough. But choosing the best "k" for the classifier may be difficult. The time and space complexity of the algorithm is very high, and we need to make several optimizations for efficiently running the algorithm.

Neverthless, it's one among the most popular techniques used for classification.





Post your slecture material here. Guidelines:

  • If you are making a text slecture
    • Type text using wikitext markup languages
    • Type all equations using latex code between <math> </math> tags.
    • You may include links to other Project Rhea pages.

Questions and comments

If you have any questions, comments, etc. please post them on this page.


Back to ECE662, Spring 2014

Alumni Liaison

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett