How to write MATLAb code to generate confusion matrices and calcultes recall and precision?

4 次查看(过去 30 天)
Hi,
I've a data file of 101 records with 21 classes. First of all, I want to generate 21 separate confusion matrices for these 21 classes and then want to calculate recall and precision for these 21 confusion matrices. Please guide me that how can I write MATLAB code for this task?
Thank you.

回答(1 个)

MHN
MHN 2016-2-5
编辑:MHN 2016-2-5
You do not have to make 21 separate confusion matrices. You should just make one confusion matrix. E.g. let Y be a vector with 12 elements that shows the real classes of your instances. and let Y_hat be the predicted class of the instances. Then you can easily compute the confusion matrix by the following code:
Y = [1 1 1 1 2 2 2 2 3 3 3 3];
Y_hat = [1 1 1 3 2 3 1 1 3 3 3 3];
C = confusionmat(Y,Y_hat)
C is the confusion matrix.
The same for 101 instances and 21 classes. e.g (I have used a random vector as a real classes and then randomly changed 20 of them to make Y_hat which could be the result of a prediction):
Y = randi(21,101,1);
Y_hat = Y;
Y_hat(randi(101,20,1)) = randi(21,20,1);
[c,order] = confusionmat(Y,Y_hat);

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by