Inconsistant Accuracy , Precision , Recall and F1 Score
Show older comments
Hi there,
I'm attempting to calculate Precision, Recall, and F1 score, but I see NaN in my calculations. Could you please assist me in resolving a computation bug?
Thank you
act =[
0
0.0269
0.0354
0
51.9578
34.4936
10.0596
4.2331
3.4373
2.0611
2.9576
3.1177
1.9092
1.8423
5.5713
4.7685
3.8489
3.8223
52.2738
26.4217];
pred = -0 + (100+0)*rand(size(act,1),1); %between a = 0 to b = 100
[m,order] = confusionmat(act,pred);
cmtx = m';
diagonal = diag(cmtx); % Bug here
SumOfrows = sum(cmtx,2);
precision = diagonal ./ SumOfrows;
OverallPrecision = mean(precision);
SumOfCol = sum(cmtx,1);
recall = diagonal ./ SumOfCol';
OverallRecall = mean(recall);
f1Score = 2* ((OverallPrecision*OverallRecall)/ OverallPrecision+OverallRecall);
precision,
OverallPrecision,
recall,
OverallRecall,
f1Score
20 Comments
dpb
on 20 Jun 2023
No idea why you would consider it a "bug"; your input vector of predicted values is totally bogus in terms of the actual data vector you used -- there's no chance in about the number of stars in the galaxy that any value at all will match and thereby return any match on the diagonal.
Secondarily, when the range is normalized, the input size of 23 elements gets expanded to 39 and there are then quite a number of rows/columns that contain no hits in them; when those are summed, they're still zero and dividing by zero creates NaN.
You could get rid o the NaN by removing all zero rows/columns from m first, but it's still nonsensical given the input.
the cyclist
on 20 Jun 2023
I am sorry if this sounds harsh, but there is so much wrong here that it is difficult to know how to advise you.
Here is the biggest, overarching problem. You are using a method (precision, recall, F1 score based on confusion matrix) that is based on the predicion of categories of data, but are applying it to continuous measurements. (You have also used random predictions of those continuous values, but I assume you built a bad model on purpose?)
@dpb has explained technically why you are seeing nonsense output. But why conceptually are you trying to do it this way?
Life is Wonderful
on 21 Jun 2023
Edited: Life is Wonderful
on 21 Jun 2023
the cyclist
on 21 Jun 2023
In a typical prediction application, you would have a set of explanatory variables, that are used to predict a response variable. One tries to find a mathematical model that gives the best prediction.
You have only shown us some actual values, and a very strange random guess at the prediction. You don't mention having any other variables that could be used to predict the response.
Life is Wonderful
on 21 Jun 2023
Edited: Life is Wonderful
on 21 Jun 2023
dpb
on 21 Jun 2023
As @the cyclist so appropriately pointed out (what I neglected to mention in simply showing you why the calculations resulted in NaN), the whole concept of what you're doing is simply a wrongheaded approach to whatever it is that is the underlying problem -- applying a categorical method to continuous data is simply incorrect and it doesn't matter whether the actual numbers returned are finite or not; they're meaningless either way.
The only hope here is to go back to the basics of what is the actual problem trying to solve (that is, the research question or hypothesis to be tested, NOT just the attempt to apply a confusion matrix to continuous data somehow to create finite number) and develop a clear statement of the problem. THEN one can begin to assess what would be appropriate analysis techniques, but it's impossible to address the fundamental problem here with no understanding of what the data are, how were obtained and what it is that is attempted to be inferred from them. As noted, you have shown no corollary variable(s) from which to even begin to try to make some sort of predictive model; predicting a correlation from random variables is a nonstarter from the beginning; ain't agonna' happen.
the cyclist
on 21 Jun 2023
Again, I don't mean to sound harsh, but you seem to be picking MATLAB functions without any understanding of what they actually do. It is also extremely confusing why you keep creating random values for comparisons. Why are you not using your actual data? The predictions should never be random.
The first, most important thing you do not seem to understand is the difference between trying to predict values that are in categories (e.g. "red", "green"), versus continuous values (e.g. 1.23, 3.45, 7.77). In technical terms, are you trying to solve a classification problem, or a regression problem? This is important for both the method used to make the prediction, and for the method used assess the quality of that prediction.
Both of the example cases you show are problematic, because they confuse all of the above. They mix up methods intended for binary classification (when your data have more than 2 categories) with regression methods such as fitlm.
I think it would be helpful for you to read through the two documentation pages I posted. This forum is probably not the best for teaching you all you do not know.
dpb
on 21 Jun 2023
Agree again w/ @the cyclist; the problem here is not a MATLAB question and Answers is not a forum for statistical consulting. If this is something from a school project or thesis, then it's time to consult your prof/advisor for some guidance; as @the cyclist has noted you're totally at sea here....
the cyclist
on 21 Jun 2023
Thank you for sharing your actual data. That is helpful.
Accuracy, precision, recall, and f1 score are not sensible metrics for these data, in any way that I can imagine. Why do you believe those metrics are meaningful for your problem?
Forget those metrics (for now). Forget prediction, models, etc.
What question are you trying to answer about these data?
Life is Wonderful
on 21 Jun 2023
Edited: Life is Wonderful
on 21 Jun 2023
the cyclist
on 21 Jun 2023
You are correct that statistical modeling might be able to help with your problem. But those particular metrics you are trying to use are not useful.
Are o-mse and t-mse the only data you have about the video? Looking at those data, how would a human being know if there are any mistakes?
What are "o-mse" and "t-mse", really? It's extremely difficult to conceptualize solutions without a clear understanding of just what the problem is -- and we don't know what those variables really represent.
It would seem highly unlikely to be able to predict anything about whatever an error would have been in "analysing" the video frames from the o-mse data; it's essentially a short ramp to a plateau; there's no info in that.
It's not clear what the t-mse data represent as noted; what does that have to do with the video and some error/mistake in analyzing it?
The thing that is lacking here is any definition of what was/was not a mistake for each frame; without that there's really nothing that can be said about prediction cuz it's unknown what the result was; ergo, how's one going to predict?
Back to @the cyclist's point about particular test statistics, to make this into a dataset for which a categorization metric could be used would be to have some metric that you recorded be an "Error/No Error" binary value for each measurement -- that would be the reference. Then you have the problem of what is available that has any correlation to that metric to use as one or more predictors.
Life is Wonderful
on 22 Jun 2023
Edited: Life is Wonderful
on 22 Jun 2023
load("mse.mat","o_mse","t_mse")
figure
hold on
plot(o_mse)
plot(t_mse)
set(gca,"YLim",[52.95 53.10]);
legend(["o\_mse","t\_mse"])
Here I have zoomed way in to observe what o_mse does at the spikes in t_mse. I still don't really understand what you are trying to predict.
Life is Wonderful
on 22 Jun 2023
dpb
on 22 Jun 2023
Nor I. At that resolution, there is at least some variablility in the o variable and there is some variation in its magnitude that does correlate with the spikes, sorta', but it's certainly not consistent in nature; some have a local minimum; others don't. Some have a leading change in slope that could be detected, at least one shows almost nothing in that vein, either...
But
Step 1. No idea because still never explained what the output is
Step 2. See Step 1.
Life is Wonderful
on 23 Jun 2023
Edited: Life is Wonderful
on 23 Jun 2023
Well, that's certainly a different result; those two have almost perfect correlation.
However, as to what can be inferred from the above and what sort of an analysis to apply is far too complex a question to answer in this forum; would need in-depth consultation in order to have a sufficient understanding of the problem in order to be able to do so.
Good luck in finding someone in your environment that has such expertise...
Life is Wonderful
on 24 Jun 2023
Answers (0)
Categories
Find more on Support Vector Machine Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


