John Alberto
John Alberto

Reputation: 437

Understanding knn algorithm (classification) in MATLAB

I'm still not very familiar with using MATLAB so I apologize if my question seems a bit dumb. I'm trying to learn the K-NN classification, and my professor said I should start with MATLAB. I have a vector, lets call it x, that contains 2 columns of data. The first column is feature 1, and the second is feature 2. Each row in x represents one data point. I also have another vector, let's call it c, which contains class labels for each data point (1 or 2), there are only 2 classes. Here's the problem: I'm supposed to use the function "knnsearch" to find the k-neighbors, and build a K-NN classifier. I know which points of my data are the training, validation, and testing sets. I'm then supposed to look at the number of points being misclassified, and see how this changes as k is increased.

I think I have an idea of how knnsearch works, but have no idea where to go from there. Can anyone help? Even tips about how the algorithm works would be helpful at this point because I've spent over 11 hours trying to figure out this problem.

Upvotes: 0

Views: 2216

Answers (2)

gsamaras
gsamaras

Reputation: 73444

If you want just to find the k-NN, you could use the function MATLAB provides:

IDX = knnsearch(X,Y)

Here you can read more.

Moreover, you might find this answer useful.

Upvotes: 0

Sam Roberts
Sam Roberts

Reputation: 24147

You may find that the ClassificationKNN class is a better fit for your needs than than the knnsearch function. knnsearch just finds nearest neighbours; ClassificationKNN applies that to build a classification model. You can do it yourself as well if you want, but ClassificationKNN is a lot easier.

For example:

% X is your nx2 array of training data
% Y is your nx1 array of training labels

model = ClassificationKNN.fit(X,Y);

% newX is an mx2 array of test data

predictedY = predict(model, newX)

It should be as simple as that. If you look in the documentation for Statistics Toolbox, there quite a few more demonstrations of ClassificationKNN.

Upvotes: 1

Related Questions