Is there any function to calculate Precision and Recall using Matlab?
I have problem about calculating the precision and recall for classifier in matlab. I use fisherIris data (that consists of 150 datapoints, 50-setosa, 50-versicolor, 50-virginica). I have classified using kNN algorithm. Here is my confusion matrix:
correct classification rate is 96% (144/150), but how to calculate precision and recall using matlab, is there any function? I know the formulas for that precision=tp/(tp+fp),and recall=tp/(tp+fn), but I am lost in identifying components. For instance, can I say that true positive is 144 from the matrix? what about false positive and false negative? Please help!!! I would really appreciate! Thank you! | |||||||||||||||||||||
|
To add to pederpansen's answer, here are some anonymous Matlab functions for calculating precision, recall and F1-score for each class, and the mean F1 score over all classes:
| |||||
|
As Dan pointed out in his comment, precision and recall are usually defined for binary classification problems only. But you can calculate precision and recall separately for each class. Let's annotate your confusion matrix a little bit:
Here I assumed the usual convention holds, i.e. columns are used for true values and rows for values predicted by your learning algorithm. (If your matrix was built the other way round, simply take the transpose of the confusion matrix.) The true positives ( If we define false positives (
Here I used the class names instead of the row indices for illustration. Please have a look at the answers to this question for further information on performance measures in the case of multi-class classification problems - particularly if you want to end up with single number instead of one number for each class. Of course, the easiest way to do this is just averaging the values for each class. UpdateI realized that you were actually looking for a Matlab function to do this. I don't think there is any built-in function, and on the Matlab File Exchange I only found a function for binary classification problems. However, the task is so easy you can easily define your own functions like so:
This will return a column vector containing the precision and recall values for each class, respectively. Now you can simply call
to obtain the average precision and recall values of your model. |
'Matlab ' 카테고리의 다른 글
matlab feval VS predict (0) | 2017.03.07 |
---|---|
MATLAB's glmfit vs fitglm , difference (0) | 2017.03.07 |
matlab에서 SVM으로 ROC curve 그릴 때 필요한 score(posterior probability)를 구하는 방법 (0) | 2017.03.04 |
Why do I receive the error "License Manager Error -9"? (0) | 2017.02.23 |
check whether have another matlab function somewhere on your path (0) | 2017.02.22 |