# KNN(k-nearest neighbor) algorithm as supervised and as unsupervised algorithm. What are the main differences? How can it be both?

On internet and in articles KNN ist mostly described as supervised algorithm. But recently I have find also few articles where it is mentioned as unsupervised algorithm.I cannot find articles that are reviewing/comparing both of them. Can you recommend someliterature/articles where I could read more about it? Can you explain how can KNN be both? What are the differences?

The $$k$$-nearest neighbors supervised learning algorithm works by taking a point, calculating the distance in the feature space (predictors) between that point and all other points, determining the $$k$$ points that are closest, and using the labels ($$y$$) on those points to make a prediction. That prediction might be an average in order to do a regression. It might be the category with the most representation among those $$k$$-nearest neighbors. It might be a probability based on the relative number of categories represented in those $$k$$-nearest neighbors.
A place where $$k$$-nearest neighbors could be used in an unsupervised way if if you just want to know the $$k$$-nearest neighbors in the feature space. This could be used, for instance, for some kind of matching procedure.