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 }}
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: docs/fundamentals/documentarray/evaluation.md
+60-13Lines changed: 60 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,10 +69,10 @@ da_prediction['@m'].summary()
69
69
To evaluate the matches against a ground truth array, you simply provide a DocumentArray to the evaluate function like `da_groundtruth` in the call below:
Thereby, `da_groundtruth` should contain the same documents as in `da_prediction` where each `matches` attribute contains exactly those documents which are relevant to the respective root document.
75
+
Thereby, `da_groundtruth` should contain the same Documents as in `da_prediction` where each `matches` attribute contains exactly those Documents which are relevant to the respective root Document.
76
76
The `metrics` argument determines the metric you want to use for your evaluation, e.g., `precision_at_k`.
77
77
78
78
In the code cell below, we evaluate the array `da_prediction` with the noisy matches against the original one `da_original`:
@@ -111,7 +111,8 @@ for d in da_prediction:
111
111
Note that the evaluation against a ground truth DocumentArray only works if both DocumentArrays have the same length and their nested structure is the same.
112
112
It makes no sense to evaluate with a completely different DocumentArray.
113
113
114
-
While evaluating, Document pairs are recognized as correct if they share the same identifier. By default, it simply uses {attr}`~docarray.Document.id`. One can customize this behavior by specifying `hash_fn`.
114
+
While evaluating, Document pairs are recognized as correct if they share the same identifier. By default, it simply uses {attr}`~docarray.Document.id`.
115
+
You can customize this behavior by specifying `hash_fn`.
115
116
116
117
Let's see an example by creating two DocumentArrays with some matches with identical texts.
117
118
@@ -157,8 +158,8 @@ It is correct as we define the evaluation as checking if the first two character
157
158
158
159
## Evaluation via labels
159
160
160
-
Alternatively, you can add labels to your documents to evaluate them.
161
-
In this case, a match is considered relevant to its root document if it has the same label:
161
+
Alternatively, you can add labels to your Documents to evaluate them.
162
+
In this case, a match is considered relevant to its root Document if it has the same label:
162
163
163
164
```python
164
165
import numpy as np
@@ -198,7 +199,7 @@ Some of those metrics accept additional arguments as `kwargs` which you can simp
198
199
```{danger}
199
200
These metric scores might change if the `limit` argument of the match function is set differently.
200
201
201
-
**Note:** Not all of these metrics can be applied to a Top-K result, i.e., `ndcg_at_k` and `r_precision` are calculated correctly only if the limit is set equal or higher than the number of documents in the `DocumentArray` provided to the match function.
202
+
**Note:** Not all of these metrics can be applied to a Top-K result, i.e., `ndcg_at_k` and `r_precision` are calculated correctly only if the limit is set equal or higher than the number of Documents in the `DocumentArray` provided to the match function.
202
203
```
203
204
204
205
You can evaluate multiple metric functions at once, as you can see below:
@@ -215,13 +216,57 @@ da_prediction.evaluate(
215
216
216
217
In this case, the keyword argument `k` is passed to all metric functions, even though it does not fulfill any specific function for the calculation of the reciprocal rank.
217
218
219
+
### The max_rel parameter
220
+
221
+
Some metric functions shown in the table above require a `max_rel` parameter.
222
+
This parameter should be set to the number of relevant Documents in the Document collection.
223
+
Without the knowledge of this number, metrics like `recall_at_k` and `f1_score_at_k` cannot be calculated.
224
+
225
+
In the `evaluate` function, you can provide a keyword argument `max_rel`, which is then used for all queries.
226
+
In the example below, we can use the datasets `da_prediction` and `da_original` from the beginning, where each query has nine relevant Documents.
If the pre-defined metrics do not fit your use-case, you can define a custom metric function.
221
266
It should take as input a list of binary relevance judgements of a query (`1` and `0` values).
222
267
The evaluate function already calculates this binary list from the `matches` attribute so that each number represents the relevancy of a match.
223
268
224
-
Let's write a custom metric function, which counts the number of relevant documents per query:
269
+
Let's write a custom metric function, which counts the number of relevant Documents per query:
225
270
226
271
```python
227
272
defcount_relevant(binary_relevance):
@@ -282,20 +327,22 @@ print(result)
282
327
{'reciprocal_rank': 0.7583333333333333}
283
328
```
284
329
330
+
For metric functions which require a `max_rel` parameter, the `embed_and_evaluate` function (described later in this section) automatically constructs the dictionary for `num_relevant_documents_per_label` based on the `index_data` argument.
331
+
285
332
### Batch-wise matching
286
333
287
-
The ``embed_and_evaluate`` function is especially useful, when you need to evaluate the queries on a very large document collection (`example_index` in the code snippet above), which is too large to store the embeddings of all documents in main-memory.
288
-
In this case, ``embed_and_evaluate`` matches the queries to batches of the document collection.
334
+
The ``embed_and_evaluate`` function is especially useful, when you need to evaluate the queries on a very large Document collection (`example_index` in the code snippet above), which is too large to store the embeddings of all Documents in main-memory.
335
+
In this case, ``embed_and_evaluate`` matches the queries to batches of the Document collection.
289
336
After the batch is processed all embeddings are deleted.
290
337
By default, the batch size for the matching (`match_batch_size`) is set to `100_000`.
291
338
If you want to reduce the memory footprint, you can set it to a lower value.
292
339
293
340
### Sampling Queries
294
341
295
-
If you want to evaluate a large dataset, it might be useful to sample query documents.
342
+
If you want to evaluate a large dataset, it might be useful to sample query Documents.
296
343
Since the metric values returned by the `embed_and_evaluate` are mean values, sampling should not change the result significantly if the sample is large enough.
297
-
By default, sampling is applied for `DocumentArray` objects with more than 1,000 documents.
298
-
However, it is only applied on the `DocumentArray` itself and not on the document provided in `index_data`.
344
+
By default, sampling is applied for `DocumentArray` objects with more than 1,000 Documents.
345
+
However, it is only applied on the `DocumentArray` itself and not on the Documents provided in `index_data`.
299
346
If you want to change the number of samples, you can ajust the `query_sample_size` argument.
300
347
In the following code block an evaluation is done with 100 samples:
301
348
@@ -323,7 +370,7 @@ da.embed_and_evaluate(
323
370
{'precision_at_k': 0.13649999999999998}
324
371
```
325
372
326
-
Please note that in this way only documents which are actually evaluated obtain an `.evaluations` attribute.
373
+
Please note that in this way only Documents which are actually evaluated obtain an `.evaluations` attribute.
327
374
328
375
To test how close it is to the exact result, we execute the function again with `query_sample_size` set to 1,000:
0 commit comments