MATLAB Answers

youb mr
0

How do I improve my result of KNN classification using confusion matrix?

Asked by youb mr
on 16 Nov 2019 at 9:24
Latest activity Commented on by Ridwan Alam on 20 Nov 2019 at 11:52
Hello everyone.
I'm trying to classify a data set containing two classes using a Knn classifer.
and would like to evaluate the performance using its confusion matrix. But how can I use it with the KNN classifier?
This is my code of KNN classifer
model=ClassificationKNN.fit(X,Y,'NumNeighbors',9);
[~,result1]=predict(model,x);

  2 Comments

You forgot to attach X and Y in a .mat file
save('answers.mat', 'X', 'Y');
Have you tried the "Classification Learner" App on the App tab of the tool ribbon?
You tagged it with image processing. What about this is at all related to image processing???
how i can use confusion_matrix in this situation

Sign in to comment.

1 Answer

Answer by Ridwan Alam on 20 Nov 2019 at 0:25

yhat = predict(model,x);
[C,order] = confusionmat(y,yhat);
Use this help file to understand how to use C and order:

  2 Comments

Error using confusionmat (line 98)
G and GHAT need to have same number of rows
Error in knn (line 189)
C = confusionmat(Y,yhat)
Here, I am assuming you have trained the model with “X” and “Y”, and are testing with “x” and “y”. “X” and “x” are different data, if in matrix format, they should have same number of columns but different row sizes.
“yhat” is the prediction of your model for test data “x” (not “X”). Confusionmat compares “yhat” with the ground truth or labels “y” (not “Y”) for the test data “x”.

Sign in to comment.