You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimizing recall calculation for micro-averaging #8126
precision_score will suffer from the same lag as they both are calling precision_recall_fscore_support which in turn will evaluate all four.
Probably we can make more use of warn_for argument?
Can I work on it and make a PR?
The jump from .7s to 10s seems very surprising. The calculation of F1
should be much less burden than of p and r, which also share much of their
work.
how many samples? how many classes? Or how many labels? Or just provide the
dataset.
When you say you calculate recall by itself, with what code?
On 29 Dec 2016 12:01 am, "Mehul Ahuja" <[email protected]> wrote:
precision_score will suffer from the same lag as they both are calling
precision_recall_fscore_support which in turn will evaluate all four.
Probably we can make more use of warn_for argument?
Can I work on it and make a PR?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#8126 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz63_-27XdMO2_Zy18GKoYFIIt3A64ks5rMl2kgaJpZM4LW4zQ>
.
The jump from .7s to 10s seems very surprising. The calculation of F1
should be much less burden than of p and r, which also share much of their
work.
I was honestly shocked as well when I found that out
how many samples? how many classes? Or how many labels? Or just provide the
dataset.
I have a 5551 x 16890 numpy matrix of 0s and 1s. I am working on a dataset from citeulike to build a recommender that recommends research papers. The dataset is not publically available and I do not own it so I am sorry I can't provide it. So basically there are two labels, 1 indicates that the user has the document in his library, 0 indicates either that he disliked the paper or he didn't read it. So for me it wouldn't make sense to calculate the precision of the recommender, only the recall.
When you say you calculate recall by itself, with what code?
Here is a sample code that I am using somewhere in my evaluator class.
def calculate_recall(self, ratings, predictions):
"""
The method given original ratings and predictions returns the recall of the recommender
@param (int[][]) ratings matrix
@param (int[][]) predictions matrix (only 0s or 1s)
@returns (float) recall, ranges from 0 to 1
"""
denom = sum(sum(ratings))
nonzeros = ratings.nonzero()
nonzeros_predictions = predictions[nonzeros]
return sum(nonzeros_predictions) / denom
@HashCode55
I already worked on it a bit, but if you want to go ahead please!
Clearly, our code is not optimised for micro-averaging with a sparse large-scale multilabel problem using dense arrays. But the problem is certainly not in the calculation of P and F1. See benchmarks below, where your approach is faster in all cases (though there is some overhead in scikit-learn to handle various types of input), but is little faster than calculating all of P, R and F, and not substantially faster when using a sparse data structure.
Please update your issue description, and feel free to offer a patch.
Hi @jnothman,
Apart from the input handling, when we supply recall_score with a dense matrix we are actually casting it to compressed sparse matrix in _check_targets which might be the culprit here. I tried to check how much time it is actually taking -
Density is - 0.01
Time taken - 2.71723198891
Density is - 0.1
Time taken - 5.73207402229
Density is - 0.25
Time taken - 6.15723586082
Density is - 0.5
Time taken - 5.69532895088
Density is - 0.75
Time taken - 9.40600514412
Density is - 0.9
Time taken - 13.1301279068
And for the same reason, if we give it a sparse matrix already, it doesn't take any time to cast and hence the benchmarks above...
I don't think the indexing is faster than the original implementation here, and as @HashCode55 pointed out here, the time-consuming part is actually _check_targets , which is the same conclusion as mine in #11179-comment. So changing the implementation in calculating tp will not make if faster, in fact, I know that the multiplication implementation is blazingly fast.
Here recall1 is the indexing implementation, and recall2 is the multiplication (original) implementation. It seems like the original implementation is much faster.
In [38]: defrecall1(y_true, y_pred):
...: n_true=y_true.sum()
...: nonzeros=y_pred.nonzero()
...: tp=y_true[nonzeros].sum() # I think this should be y_true instead of y_pred
...: returntp/n_true
...:
In [39]: defrecall2(y_true, y_pred):
...: n_true=y_true.sum()
...: tp= (y_true*y_pred).sum()
...: returntp/n_true
...:
In [40]: density=0.5In [41]: y_true=np.random.rand(5551, 16890) <densityIn [42]: y_pred=np.random.rand(5551, 16890) <densityIn [43]: %timeitrecall1(y_true, y_pred)
1.98s ± 19.5msperloop (mean ± std. dev. of7runs, 1loopeach)
In [44]: %timeitrecall2(y_true, y_pred)
226ms ± 5.67msperloop (mean ± std. dev. of7runs, 1loopeach)
In [45]: density=0.1In [46]: y_true=np.random.rand(5551, 16890) <densityIn [47]: y_pred=np.random.rand(5551, 16890) <densityIn [48]: %timeitrecall1(y_true, y_pred)
742ms ± 9.18msperloop (mean ± std. dev. of7runs, 1loopeach)
In [49]: %timeitrecall2(y_true, y_pred)
220ms ± 5.05msperloop (mean ± std. dev. of7runs, 1loopeach)
Let's close this, and perhaps consider opening an issue to improve efficiency around _check_targets and validation in metrics (e.g. avoiding duplication of unique_labels).
Activity
rombiee commentedon Dec 28, 2016
precision_scorewill suffer from the same lag as they both are callingprecision_recall_fscore_supportwhich in turn will evaluate all four.Probably we can make more use of
warn_forargument?Can I work on it and make a PR?
jnothman commentedon Dec 28, 2016
ndomar commentedon Dec 28, 2016
@jnothman
I was honestly shocked as well when I found that out
I have a 5551 x 16890 numpy matrix of 0s and 1s. I am working on a dataset from citeulike to build a recommender that recommends research papers. The dataset is not publically available and I do not own it so I am sorry I can't provide it. So basically there are two labels, 1 indicates that the user has the document in his library, 0 indicates either that he disliked the paper or he didn't read it. So for me it wouldn't make sense to calculate the precision of the recommender, only the recall.
Here is a sample code that I am using somewhere in my evaluator class.
@HashCode55
I already worked on it a bit, but if you want to go ahead please!
jnothman commentedon Dec 29, 2016
Clearly, our code is not optimised for micro-averaging with a sparse large-scale multilabel problem using dense arrays. But the problem is certainly not in the calculation of P and F1. See benchmarks below, where your approach is faster in all cases (though there is some overhead in scikit-learn to handle various types of input), but is little faster than calculating all of P, R and F, and not substantially faster when using a sparse data structure.
Please update your issue description, and feel free to offer a patch.
Resutlts look like:
[-]Calculating recall takes too much time[/-][+]Optimizing recall calculation for micro-averaging[/+]ndomar commentedon Dec 29, 2016
@jnothman I updated the title and the description, will provide a patch later. I have to read the wiki on contributing first. Thank you!
rombiee commentedon Jan 1, 2017
Hi @jnothman,
Apart from the input handling, when we supply
recall_scorewith a dense matrix we are actually casting it to compressed sparse matrix in_check_targetswhich might be the culprit here. I tried to check how much time it is actually taking -This is from routine
_check_targetsResults :
And for the same reason, if we give it a sparse matrix already, it doesn't take any time to cast and hence the benchmarks above...
jnothman commentedon Jan 1, 2017
Yes, perhaps that was a poor decision on our part. However, your use of
nonzeroand fancy indexing shows promise against our current implementation.rombiee commentedon Jan 4, 2017
@jnothman So how should we proceed from here?
jnothman commentedon Jan 4, 2017
rombiee commentedon Jan 4, 2017
@jnothman Oh okay! If he's working on it then its fine. I'll find some other issue.
ndomar commentedon Jan 7, 2017
Yes I am already working on it, apologies if I am late or something but I just got back from my vacation.
jnothman commentedon Feb 11, 2018
I have proposed in #10083 (comment) how I would go about implementing and trialling this improvement.
ShangwuYao commentedon Jun 18, 2018
I don't think the indexing is faster than the original implementation here, and as @HashCode55 pointed out here, the time-consuming part is actually
_check_targets, which is the same conclusion as mine in #11179-comment. So changing the implementation in calculating tp will not make if faster, in fact, I know that the multiplication implementation is blazingly fast.Here
recall1is the indexing implementation, andrecall2is the multiplication (original) implementation. It seems like the original implementation is much faster.jnothman commentedon Jun 18, 2018
Thanks! I'd forgotten that conclusion here.
Let's close this, and perhaps consider opening an issue to improve efficiency around
_check_targetsand validation in metrics (e.g. avoiding duplication ofunique_labels).thomasjpfan commentedon May 30, 2022
As noted in #8126 (comment), I am closing this issue.