0
$\begingroup$

I am working on a three class problem. How do you calculate precision, recall, f-score, and MCC for each class while using MATLAB? Here is my confusion matrix:

2775  0    0
1    591   0
4     0   845

I am calculating Accuracy from the Confusion matrix in this way:

Accuracyy = 100*sum(diag(confusionMat))./sum(confusionMat(:));  

I would like to measure the below performance measures for each class. I know the formulas, but not how to execute this in MATLAB. Please help. I would really appreciate it. Thank you.

  1. $Precision = {{TP} \over {TP \ + \ FP}}$

  2. $Recall = {{TP} \over {TP \ + \ FN}}$

  3. $F-score = {{2 \ * \ TP} \over {2 \ * \ TP \ + FP \ + \ FN}}$

  4. Matthews Correlation Coefficient (MCC)

$\endgroup$

2 Answers 2

2
$\begingroup$

if yHat are your predictions and yval are your y true then

tp = sum((yHaT == 1) & (yval == 1));
fp = sum((yHaT == 1) & (yval == 0));
fn = sum((yHaT == 0) & (yval == 1));

precision = tp / (tp + fp);
recall = tp / (tp + fn);
F1 = (2 * precision * recall) / (precision + recall);
$\endgroup$
1
$\begingroup$

Here, first find the all true positive values using the diag function:

tp_m = diag(cm_test);

and then for each class find the TP, TN, FP, FN using the following code:

 for i = 1:num_labels
    TP = tp_m(i);
    FP = sum(cm_test(:, i), 1) - TP;
    FN = sum(cm_test(i, :), 2) - TP;
    TN = sum(cm_test(:)) - TP - FP - FN;

    Accuracy = (TP+TN)./(TP+FP+TN+FN);

    TPR = TP./(TP + FN);%tp/actual positive  RECALL SENSITIVITY
    if isnan(TPR)
        TPR = 0;
    end
    PPV = TP./ (TP + FP); % tp / predicted positive PRECISION
    if isnan(PPV)
        PPV = 0;
    end
    TNR = TN./ (TN+FP); %tn/ actual negative  SPECIFICITY
    if isnan(TNR)
        TNR = 0;
    end
    FPR = FP./ (TN+FP);
    if isnan(FPR)
        FPR = 0;
    end
    FScore = (2*(PPV * TPR)) / (PPV+TPR);

    if isnan(FScore)
        FScore = 0;
    end
end

Let me know if you need any assistance.

$\endgroup$

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