F1
F1MicroMultipleFieldsMetric
Bases: MetricCollection[F1MicroSingleFieldMetric]
Source code in src/kibad_llm/metrics/f1.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
__init__(fields=None, format_as_markdown=True, subfield_keys=None, subfield_values=None, sort_fields=False, **kwargs)
Computes F1MicroSingleFieldMetric for multiple fields at once as well as micro (ALL) and macro (AVG) over all fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
list[str] | None
|
List of fields to compute F1MicroSingleFieldMetric for. If not provided, the metric will be computed for all fields found in the data. |
None
|
format_as_markdown
|
bool
|
Whether to format the result as a markdown table. Defaults to True. |
True
|
subfield_keys
|
dict[str, list[str]] | None
|
Optional dict mapping field names to lists of keys used to split
dict-like entries into separate generated fields. For a configured field, the
values of these keys are removed from each nested dict and appended to the field
name, while the remaining key-value pairs are scored as that generated field's
payload. This makes it possible to compute metrics separately for entries such as
|
None
|
subfield_values
|
dict[str, list[str]] | None
|
Optional dict mapping field names to lists of keys that should be
retained as the payload of generated fields after extracting |
None
|
sort_fields
|
bool
|
Whether to sort the fields in the output. Defaults to False. |
False
|
**kwargs
|
Additional keyword arguments for F1MicroSingleFieldMetric, e.g.,
|
{}
|
Source code in src/kibad_llm/metrics/f1.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
F1MicroSingleFieldMetric
Bases: MetricWithPrepareEntryAsSet
Computes micro averaged precision, recall, and F1 score for single- and multi-label classification tasks.
The metric operates on sets and allows for simple preprocessing, see _prepare_entry for details.
WARNING: !Since the metric operates on sets, this can obfuscate if the LLM produces duplicate labels !in multi-label settings. E.g., prediction = ["A", "A", "B"] and reference = ["A", "B"] will !be treated as perfect prediction with tp=2, fp=0, fn=0 even though the prediction contains a !duplicate label "A".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ignore_missing_entries
|
bool
|
If True, instances where either prediction or reference is empty will be ignored in the metric calculation. |
False
|
**kwargs
|
Keyword arguments for entry-to-set preparation. See
|
{}
|
Source code in src/kibad_llm/metrics/f1.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
calculate_scores(state)
staticmethod
Calculates precision, recall and f1 from true positives, false positives and false negatives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, int]
|
dictionary with keys "tp", "fp", "fn" |
required |
returns: dictionary with precision, recall and f1
Source code in src/kibad_llm/metrics/f1.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
reset()
Resets all values of the internal state to zero
Source code in src/kibad_llm/metrics/f1.py
36 37 38 | |