-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: fix MRR and MAP calculations #7841
Conversation
Pull Request Test Coverage Report for Build 9467114407Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 9468451282Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
thanks for taking care! I just doubled checked some examples with the result from v1 and it looks fine :) |
Pull Request Test Coverage Report for Build 9515678439Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify a bit the code using list comprehensions
Pull Request Test Coverage Report for Build 9615764716Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still found opportunities for improvement that could make our lives easier in the future (I hope)
if ground_document.content in retrieved_document.content: | ||
score = 1 / (rank + 1) | ||
break | ||
ground_truth_content = [doc.content for doc in ground_truth if doc.content is not None] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ground_truth_content = [doc.content for doc in ground_truth if doc.content is not None] | |
ground_truth_contents = [doc.content for doc in ground_truth if doc.content is not None] |
average_precision = 0.0 | ||
relevant_documents = 0 | ||
|
||
for rank, retrieved_document in enumerate(retrieved): | ||
if retrieved_document.content is None: | ||
continue | ||
ground_truth_content = [doc.content for doc in ground_truth if doc.content is not None] | ||
for rank, retrieved_document in enumerate(retrieved): | ||
if retrieved_document.content is None: | ||
continue | ||
|
||
if ground_document.content in retrieved_document.content: | ||
relevant_documents += 1 | ||
average_precision += relevant_documents / (rank + 1) | ||
if relevant_documents > 0: | ||
score = average_precision / relevant_documents | ||
if retrieved_document.content in ground_truth_content: | ||
relevant_documents += 1 | ||
average_precision += relevant_documents / (rank + 1) | ||
if relevant_documents > 0: | ||
score = average_precision / relevant_documents | ||
individual_scores.append(score) | ||
|
||
score = sum(individual_scores) / len(retrieved_documents) | ||
score = sum(individual_scores) / len(ground_truth_documents) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not your fault but I had a hard time understanding the algorithm.
- I think we could benefit from a better choice of variable names.
- Adding a comment in the code with the formula/link from which we drew inspiration could also help
Resources I took inspiration from: #1, #2
Something like this:
average_precision_numerator = 0.0
relevant_documents = 0
ground_truth_contents = [doc.content for doc in ground_truth if doc.content is not None]
for rank, retrieved_document in enumerate(retrieved):
if retrieved_document.content is None:
continue
if retrieved_document.content in ground_truth_contents:
relevant_documents += 1
precision_at_k= relevant_documents / (rank + 1)
average_precision_numerator+=precision_at_k
average_precision=0
if relevant_documents > 0:
average_precision = average_precision_numerator / relevant_documents
individual_scores.append(average_precision)
score = sum(individual_scores) / len(ground_truth_documents)
(untested)
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I also struggled to understand the function initially. Optimized variables can help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I think precision_at_k
is unnecessary and will be extra storage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it is not necessary, but in my opinion it makes the code more readable.
Feel free to make any changes you think appropriate.
Pull Request Test Coverage Report for Build 9648193977Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
Please incorporate these small suggestions and then merge.
@@ -10,7 +10,7 @@ | |||
@component | |||
class DocumentMAPEvaluator: | |||
""" | |||
A Mean Average Precision (MAP) evaluator for documents. | |||
A Mean Average Precision (MAP) evaluator for documents. For details, please refer to the [resource](https://www.pinecone.io/learn/offline-evaluation/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Mean Average Precision (MAP) evaluator for documents. For details, please refer to the [resource](https://www.pinecone.io/learn/offline-evaluation/). | |
A Mean Average Precision (MAP) evaluator for documents. |
I would not put this link in the docstring, but I would put it as a comment at the beginning of the run
method code to help those who will be working with the code in the future.
@@ -10,7 +10,7 @@ | |||
@component | |||
class DocumentMRREvaluator: | |||
""" | |||
Evaluator that calculates the mean reciprocal rank of the retrieved documents. | |||
Evaluator that calculates the mean reciprocal rank of the retrieved documents. For details, please refer to the [resource](https://www.pinecone.io/learn/offline-evaluation/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Evaluator that calculates the mean reciprocal rank of the retrieved documents. For details, please refer to the [resource](https://www.pinecone.io/learn/offline-evaluation/). | |
Evaluator that calculates the mean reciprocal rank of the retrieved documents. |
Same as above
Pull Request Test Coverage Report for Build 9659678165Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 9659996000Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Related Issues
Proposed Changes:
Fixed errors of MRREvaluator:
Fixed errors of MAPEvaluator:
Fixed errors in pytest file of
test_document_map.py
:test_run_with_complex_data()
How did you test it?
Ran unit tests for both evaluators
Notes for the reviewer
Verify the correctness of the calculated scores for an example.
Checklist
fix:
,feat:
,build:
,chore:
,ci:
,docs:
,style:
,refactor:
,perf:
,test:
.