1
$\begingroup$

I am trying to create a kNN search from scratch for a project. I think I have the concept of it and how it works, but I can't understand what exactly is a class.

I have a matrix of $X*Y$ where $X$ is the number of elements and $Y$ their vector. So if the values are completely random, is every element/row a different class, or do I specify the classes some other way?

$\endgroup$
2
$\begingroup$

Given the context of your question I assume your are referring to the k-NN classifier. The idea of the classification method is you have a set of feature vectors $f_i$, in general, $f_i \in \mathbb{R}^d$ where $d$ is the dimension of the vectors. Additionally you have a class $c_i$ for each $f_i$. To classify an unseen feature vector $f_j$ you select the $k$ nearest neighbors (under some distance, usually the euclidean distance) and the most common class among the neighbors is your prediction.

So, summarizing, the input of the algorithm are the feature vectors $f_i$ and the classes $c_i$. For more on the algorithm see this wikipedia link.

The image (from wikipedia) below illustrates a typical example, in this example the classes are: red triangle,blue square and the point to predict is the green circle. Each feature vector is a two dimensional point, i.e. $f_i, f_j \in \mathbb{R}^2$. In this situation the predicted class will be red triangle.

enter image description here

For an explanation using both Python and R see this.

$\endgroup$
1
$\begingroup$

Additional to feynman410's answer:

The class is your attribute that the classifier wants to predict. For example say you want to predict the color of an object and it turns out the object can be red, blue, purple, green and yellow. Then each group of objects that have the same color form a class. Now in order to predict the class (in this case the color) of the object your algorithm needs some objects where it already knows their attributes and the class (color). Hence also the word "classifier", which means predicting the class of something.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.