Question: Use Matlab to implement a kNN classifier Implement a kNN classifier. The format of the function should be y = kNNClassifier (x, X, t, K)^1.

Use Matlab to implement a kNN classifier

Use Matlab to implement a kNN classifier Implement a kNN classifier. The

Implement a kNN classifier. The format of the function should be y = kNNClassifier (x, X, t, K)^1. The input pattern to be classified is x, the training data is contained in row vectors in the data matrix X, the target values are in the vector t and the number of nearest neighbours specified by K. You have to implement the following steps: Compute the Euclidean distance from the input pattern to all the patterns in the training data. You can use distance(ii)=norm(x-X (ii,: ),2) where ii is the index into the training data. Obtain ordered indices according to the distance. You can use [dist, NNindex]=sort (distance). Note that NNindex is the ordered indices. Obtain the indices of the K nearest neighbours by NNindex(1: K). Obtain the class labels of the K nearest neighbours with t (NNindex(1: K)). Determine what class is in the majority of the K nearest neighbours and set y to this value. a) Verify that your algorithm works with a single input pattern. You can create training data as in previous exercises, using mu_1 = [1 1] mu_2 = [-1 -1] sigma_1 = [1 0.5 0.5 1] sigma_2 = [1 -0.5 -0.5 1]. b) Create another set of data (using the same parameters) and calculate the test error for K=5. c) See how the test error varies with different values of K. d) Apply your algorithm to the Iris database. State the mis-classficiation rate for different values of K

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!