ahnyeonchan2 commited on
Commit
7f858ba
1 Parent(s): 5233baa
Files changed (4) hide show
  1. README.md +8 -6
  2. mean_reciprocal_rank.py +24 -30
  3. requirements.txt +2 -1
  4. tests.py +6 -9
README.md CHANGED
@@ -5,7 +5,7 @@ datasets:
5
  tags:
6
  - evaluate
7
  - metric
8
- description: "TODO: add a description here"
9
  sdk: gradio
10
  sdk_version: 3.0.2
11
  app_file: app.py
@@ -14,19 +14,21 @@ pinned: false
14
 
15
  # Metric Card for Mean Reciprocal Rank
16
 
17
- ***Module Card Instructions:*** *Fill out the following subsections. Feel free to take a look at existing metric cards if you'd like examples.*
18
 
19
  ## Metric Description
20
- *Give a brief overview of this metric, including what task(s) it is usually used for, if any.*
 
 
21
 
22
  ## How to Use
23
- *Give general statement of how to use the metric*
 
24
 
25
- *Provide simplest possible example for using the metric*
26
 
27
  ### Inputs
28
  *List all input arguments in the format below*
29
- - **input_field** *(type): Definition of input, with explanation if necessary. State any default value(s).*
30
 
31
  ### Output Values
32
 
 
5
  tags:
6
  - evaluate
7
  - metric
8
+ description: "a statistic measure for evaluating any process that produces a list of possible responses to a sample of queries, ordered by probability of correctness."
9
  sdk: gradio
10
  sdk_version: 3.0.2
11
  app_file: app.py
 
14
 
15
  # Metric Card for Mean Reciprocal Rank
16
 
17
+ a statistic measure for evaluating any process that produces a list of possible responses to a sample of queries, ordered by probability of correctness.
18
 
19
  ## Metric Description
20
+ The reciprocal rank of a query response is the multiplicative inverse of the rank of the first correct answer: 1 for first place, 1⁄2 for second place, 1⁄3 for third place and so on. The mean reciprocal rank is the average of the reciprocal ranks of results for a sample of queries Q
21
+
22
+ {\text{MRR}}={\frac {1}{|Q|}}\sum _{{i=1}}^{{|Q|}}{\frac {1}{{\text{rank}}_{i}}}.\!
23
 
24
  ## How to Use
25
+ Provide a list of gold ranks, where each item is rank of gold item of which the first rank starts with zero.
26
+
27
 
 
28
 
29
  ### Inputs
30
  *List all input arguments in the format below*
31
+ - **input_field** *(List[int]): a list of integer where each integer is the rank of gold item
32
 
33
  ### Output Values
34
 
mean_reciprocal_rank.py CHANGED
@@ -19,47 +19,39 @@ import datasets
19
 
20
  # TODO: Add BibTeX citation
21
  _CITATION = """\
22
- @InProceedings{huggingface:module,
23
- title = {A great new module},
24
- authors={huggingface, Inc.},
25
- year={2020}
26
  }
27
  """
28
 
29
  # TODO: Add description of the module here
30
  _DESCRIPTION = """\
31
- This new module is designed to solve this great ML task and is crafted with a lot of care.
 
32
  """
33
 
34
 
35
  # TODO: Add description of the arguments of the module here
36
  _KWARGS_DESCRIPTION = """
37
- Calculates how good are predictions given some references, using certain scores
38
  Args:
39
- predictions: list of predictions to score. Each predictions
40
- should be a string with tokens separated by spaces.
41
- references: list of reference for each prediction. Each
42
- reference should be a string with tokens separated by spaces.
43
  Returns:
44
- accuracy: description of the first score,
45
- another_score: description of the second score,
46
  Examples:
47
- Examples should be written in doctest format, and should illustrate how
48
- to use the function.
49
-
50
- >>> my_new_module = evaluate.load("my_new_module")
51
- >>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
52
  >>> print(results)
53
- {'accuracy': 1.0}
54
  """
55
 
56
- # TODO: Define external resources urls if needed
57
- BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
58
-
59
 
60
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
61
  class MeanReciprocalRank(evaluate.Metric):
62
- """TODO: Short description of my evaluation module."""
63
 
64
  def _info(self):
65
  # TODO: Specifies the evaluate.EvaluationModuleInfo object
@@ -72,24 +64,26 @@ class MeanReciprocalRank(evaluate.Metric):
72
  # This defines the format of each prediction and reference
73
  features=datasets.Features({
74
  'predictions': datasets.Value('int64'),
75
- 'references': datasets.Value('int64'),
76
  }),
77
  # Homepage of the module for documentation
78
- homepage="http://module.homepage",
79
  # Additional links to the codebase or references
80
- codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
81
- reference_urls=["http://path.to.reference.url/new_module"]
82
  )
83
 
84
  def _download_and_prepare(self, dl_manager):
85
  """Optional: download external resources useful to compute the scores"""
86
- # TODO: Download external resources if needed
87
  pass
88
 
89
- def _compute(self, predictions, references):
90
  """Returns the scores"""
91
  # TODO: Compute the different scores of the module
92
- accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
 
 
 
 
93
  return {
94
- "accuracy": accuracy,
95
  }
 
19
 
20
  # TODO: Add BibTeX citation
21
  _CITATION = """\
22
+ @InProceedings{posicube:module,
23
+ title = {Mean reciprocal mean},
24
+ authors={Pocicube, Inc.},
25
+ year={2022}
26
  }
27
  """
28
 
29
  # TODO: Add description of the module here
30
  _DESCRIPTION = """\
31
+ This module is designed to evaluate a system ranks the list of item.
32
+ mean reciprocal rank is a statistic measure for evaluating any process that produces a list of possible responses to a sample of queries, ordered by probability of correctness
33
  """
34
 
35
 
36
  # TODO: Add description of the arguments of the module here
37
  _KWARGS_DESCRIPTION = """
38
+ Calculates how good are ranks, using certain scores
39
  Args:
40
+ predictions: list of predicted ranks of gold item, the first rank starts with 0
 
 
 
41
  Returns:
42
+ mean reciprocal rank: mean of inverse of rank of gold item
 
43
  Examples:
44
+
45
+ >>> mrr = evaluate.load("poscicube/mean_reciprocal_rank")
46
+ >>> results = mrr.compute(predictions=[0, 4])
 
 
47
  >>> print(results)
48
+ {'mrr': 0.6}
49
  """
50
 
 
 
 
51
 
52
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
53
  class MeanReciprocalRank(evaluate.Metric):
54
+ """a statistic measure for evaluating any process that produces a list of possible responses to a sample of queries, ordered by probability of correctness."""
55
 
56
  def _info(self):
57
  # TODO: Specifies the evaluate.EvaluationModuleInfo object
 
64
  # This defines the format of each prediction and reference
65
  features=datasets.Features({
66
  'predictions': datasets.Value('int64'),
 
67
  }),
68
  # Homepage of the module for documentation
69
+ homepage="https://huggingface.co/spaces/posicube/mean_reciprocal_rank",
70
  # Additional links to the codebase or references
71
+ codebase_urls=["https://huggingface.co/spaces/posicube/mean_reciprocal_rank"],
72
+ reference_urls=["https://en.wikipedia.org/wiki/Mean_reciprocal_rank"]
73
  )
74
 
75
  def _download_and_prepare(self, dl_manager):
76
  """Optional: download external resources useful to compute the scores"""
 
77
  pass
78
 
79
+ def _compute(self, predictions):
80
  """Returns the scores"""
81
  # TODO: Compute the different scores of the module
82
+ q = len(predictions)
83
+ sum_rr = 0.0
84
+ for p in predictions:
85
+ sum_rr += 1/(p+1)
86
+ mrr = sum_rr / q
87
  return {
88
+ "mrr": mrr
89
  }
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- git+https://github.com/huggingface/evaluate@main
 
 
1
+ git+https://github.com/huggingface/evaluate@main
2
+ datasets
tests.py CHANGED
@@ -1,17 +1,14 @@
1
  test_cases = [
2
  {
3
- "predictions": [0, 0],
4
- "references": [1, 1],
5
- "result": {"metric_score": 0}
6
  },
7
  {
8
- "predictions": [1, 1],
9
- "references": [1, 1],
10
- "result": {"metric_score": 1}
11
  },
12
  {
13
- "predictions": [1, 0],
14
- "references": [1, 1],
15
- "result": {"metric_score": 0.5}
16
  }
17
  ]
 
1
  test_cases = [
2
  {
3
+ "predictions": [0, 0, 2],
4
+ "result": {"mrr": 0.7777777777777778}
 
5
  },
6
  {
7
+ "predictions": [0, 0],
8
+ "result": {"mrr": 1}
 
9
  },
10
  {
11
+ "predictions": [0, 4],
12
+ "result": {"mrr": 0.6}
 
13
  }
14
  ]