0
$\begingroup$

I am working on 3 class problem.How to calculate precision,recall ,f-score,MCC of each class while using MATLAB. Here is my confusion matrix:

2775  0    0
1    591   0
4     0   845

I am calculating Accuracy from Confusion matrix in this way:

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

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

  1. Precision=TP / (TP + FP)

  2. Recall= TP / (TP + FN)

  3. F-score = 2*TP /(2*TP+ FP + FN)

  4. Matthews Correlation Coefficient (MCC)

$\endgroup$
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 value using 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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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