Compute F1 score for a given class

60 views (last 30 days)
Desi Jatt
Desi Jatt on 26 Mar 2021
Answered: Ram Patro on 9 Dec 2021
I am given a hyperspectral image (480x320x248 double) and the data set also contains a ground truth label image (480x320 uint8) with 7 known mineral classes (with the categorical labels 5, 11, 18, 20, 22, 23, 25). How can I calculate a vector of true class labels and a vector of predicted cluster labels as inputs and computes F1 score for the 23 class. Number of classes and number of clusters will be different and class and cluster labels may not necessarily match (this is referring to k-means).
I am guessing I need a vector of true class labels and a vector of predicted cluster labels using k-means and a confusion matrix but I am not sure how to do this in MATLAB.
I can provide more info if needed. Your help would be appreciated!!!

Answers (1)

Ram Patro
Ram Patro on 9 Dec 2021
The ground truth data must contain class label information. As the ground truth is in the form of a matrix, you can reshape it to a vector to obtain vector of true class labels.
hsData=[480x320x248]
gtLabel=[480x320]
[M,N,C] = size(hsData);
%reshape input data
DataVector = reshape(hsData,[M*N C]);
gtVector = gtLabel(:);
%find all locations where ground truth label is available
labels=[5, 11, 18, 20, 22, 23, 25];
gtLocs=[];
for i=1:numel(labels)
Locs = find(gtVector==labels(i));
gtLocs = [gtLocs Locs];
end
%prepare data for classification
class_info = gtVector(gtLocs);
data_info = DataVector(gtLocs,:);
Now you can use 'class_info' and 'data_info' for classification.
For classification, you can refer to the examples in:
  • fitcknn function for K- neareset neighbour classifier
After obtaining your classification results, you can refer:
  • confusionmat, confusionchart for preparing the confusion matrix.
  • For computing F1 score for a specific class, you need to pre-process the true and predicted class label vectors. In this step, compare these vectors with a desired class label (say 23) and convert the multiclass true and predicted class label vectors to binary vectors. And then, you can refer this for class specific performance measures.
"confusionmatStats(g​roup,grouphat)" is one of the several submissions in MATLAB File Exchange on MATLAB Central which is a forum for our product users to interact, exchange information and knowledge, without MathWorks' involvement. Feel free to contact the author of this submission directly for specific questions about the implementation.

Community Treasure Hunt

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

Start Hunting!