0% found this document useful (0 votes)
307 views

Classification - K Nearest Neighbor Classifier Example Matlab Code Found at Mathworks Cannot Understand - Stack Overflow

The document is a question posted on Stack Overflow asking for an explanation of MATLAB code examples for a k-nearest neighbor classifier. One user provided a detailed response explaining each part of the code: - The first line constructs a training set of vectors from multivariate normal distributions centered around [1 1] and [-1 -1], with standard deviations defining two classes. - The second line creates a group vector labeling the first 100 vectors as class 1 and the second 100 as class 2. - The third line generates 100 random data rows between -5 and 5 for two dimensions, to test the classifier. The responder advised using MATLAB's help and documentation functions to better understand unfamiliar code.

Uploaded by

Namrata Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
307 views

Classification - K Nearest Neighbor Classifier Example Matlab Code Found at Mathworks Cannot Understand - Stack Overflow

The document is a question posted on Stack Overflow asking for an explanation of MATLAB code examples for a k-nearest neighbor classifier. One user provided a detailed response explaining each part of the code: - The first line constructs a training set of vectors from multivariate normal distributions centered around [1 1] and [-1 -1], with standard deviations defining two classes. - The second line creates a group vector labeling the first 100 vectors as class 1 and the second 100 as class 2. - The third line generates 100 random data rows between -5 and 5 for two dimensions, to test the classifier. The responder advised using MATLAB's help and documentation functions to better understand unfamiliar code.

Uploaded by

Namrata Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

3/22/13

classification - k nearest neighbor classifier example matlab code found at mathworks cannot understand - Stack Overflow

k nearest neighbor classifier example matlab code found at mathworks cannot


understand

I understood the first example that they have provided because they have clearly explained what
happens in each line. But for the second example which is usually the one that would be used in practice
the most is not explained and iam having a hard time trying to understand it :(. The following are the
code lines that iam having trouble with
training = [mvnrnd([ 1 1], eye(2), 100); ...
mvnrnd([-1 -1], 2*eye(2), 100)];
group = [repmat(1,100,1); repmat(2,100,1)];
and
sample = unifrnd(-5, 5, 100, 2);
and this is the link -> http://www.mathworks.in/help/toolbox/bioinfo/ref/knnclassify.html
Could someone please explain this as this will not only be beneficial to me, but for all others as well.
matlab

classification

classifier

asked Nov 25 '11 at 15:13


user574183
185 1 11

1 Answer
The first line of the code you site constructs a training set of vectors, drawn from a multivariate normal
distribution, centered around [ 1 1] and [-1 -1] respectively, with standard deviations of 1 and 1 for the
sigma x and sigma y for the first class, and 2 and 2 for sigma x and sigma y for the second class. Take
100 of those vectors for each group ( or class).
Then you construct the group vector, which contains group labels: the first 100 are from class 1
( repmat(1,100,1) is actually the same as ones(100,1)) and the second 100 are from class 2
( repmat(2,100,1) == ones(100,1)*2).
The second chunk of code you cite actually just generates a matrix containing 100 random data rows, all
in the range [-5 , 5] having 2 dimensions (so 2 columns). This matrix gets used to test the classification
on.
You might also take the habit of using the matlab help or doc function on functions you don't
know/understand.
answered Nov 25 '11 at 15:38
jpjacobs
5,576 9 21

+1 for the help/doc comment. He/She might have done it already, but I like to see some evidence of that in
the question. Jim Clay Nov 25 '11 at 17:01

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

stackoverflow.com/questions/8271034/k-nearest-neighbor-classifier-example-matlab-code-found-at-mathworks-cannot-unde

1/1

You might also like