Ask Your Question

help me understand kNN

asked 2017-11-02 06:28:06 -0600

Finch_the_second gravatar image

updated 2017-11-02 07:35:36 -0600

Hello guys,

I read all the documentation about how kNN works but I have some questions still:

Lets say I try to use kNN to recognize capital letters. I generate my training data by creating a black/white 10x20 matrix for every character.

Now I want to find the letters in my picture (e.g. a Screenshot of some random text) and already have a resized 10x20 Mat for every letter. My "k" of kNN is 1. Does the algorythm now check every pixels 1 nearest neighbour? So it applys the kNN algorythm 20x10 = 200 times for each character it found? And afterwars it replys the letter with the most accordance? If not, how does it work?

Would be nice if you could help me understand it!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-02 12:51:23 -0600

Tetragramm gravatar image

Conveniently, OpenCV tutorials has an example on just this thing. Take a look!

edit flag offensive delete link more


I read those tutorials before I asked the question but they didn't really help me to understand what's going on in my example.

Finch_the_second gravatar imageFinch_the_second ( 2017-11-03 05:40:23 -0600 )edit

So what do you not understand? The concept behind kNN? I think that's what you're asking, not the implementation details.

Each feature is a dimension of data, the value of each sample is it's location in that dimension. Calculate the distance from each training pt to your sample pt, and use the k nearest neighbors to get your classification.

You've got 200 dimensions to your data, and I dunno how many sample points.

Tetragramm gravatar imageTetragramm ( 2017-11-03 09:30:47 -0600 )edit

I thought way too complicated and got confused by the Dimensions. My pictures are 2D but in this case every pixel is a dimension, so every sample has 200 dimensions which get compared by knn. Got it, thx!

Finch_the_second gravatar imageFinch_the_second ( 2017-11-20 08:57:57 -0600 )edit

Question Tools

1 follower


Asked: 2017-11-02 06:28:06 -0600

Seen: 225 times

Last updated: Nov 02 '17