Imp.Predict.Aggregation (Imp v0.5.0)

Copy Markdown View Source

Aggregation helpers for multiple predictions or raw values.

majority/2 is the common helper for self-consistency style workflows: run a program several times, then keep the most common answer. Pass field: :answer when aggregating %Imp.Prediction{} values by a named output field. Without a field, maps and structs are still safe to compare because the default normalizer falls back to inspect/1 for values that do not implement String.Chars.

Ties keep the first value from the winning normalized group.

Example

iex> predictions = [
...>   Imp.Prediction.new(answer: "Paris"),
...>   Imp.Prediction.new(answer: "paris"),
...>   Imp.Prediction.new(answer: "Lyon")
...> ]
iex> Imp.Predict.Aggregation.majority(predictions, field: :answer)
"Paris"

Summary

Functions

Normalizes values for majority grouping.

Returns the most common value after normalization.

Functions

default_normalize(value)

Normalizes values for majority grouping.

Strings, atoms, numbers, and other String.Chars values use to_string/1. Maps and structs fall back to inspect/1, avoiding crashes when callers aggregate full prediction values.

majority(predictions, opts \\ [])

Returns the most common value after normalization.

Options:

  • :field extracts a field from predictions or maps before voting.
  • :normalize supplies a custom one-argument grouping function.