Metric
amltk.optimization.metric
#
The metric definition.
Metric
dataclass
#
Metric(
name: str,
*,
minimize: bool = True,
bounds: tuple[float, float] | None = None,
fn: Callable[P, float] | None = None
)
Bases: Generic[P]
A metric with a given name, optimal direction, and possible bounds.
bounds
class-attribute
instance-attribute
#
The bounds of the metric, if any.
fn
class-attribute
instance-attribute
#
A function to attach to this metric to be used within a trial.
minimize
class-attribute
instance-attribute
#
Whether to minimize or maximize the metric.
__call__
#
__call__(*args: args, **kwargs: kwargs) -> float
Call the associated function with this metric.
Source code in src/amltk/optimization/metric.py
as_scorer
#
as_scorer(
*,
response_method: (
SklearnResponseMethods
| Sequence[SklearnResponseMethods]
| None
) = None,
**scorer_kwargs: Any
) -> _Scorer
Convert a metric to a sklearn scorer.
PARAMETER | DESCRIPTION |
---|---|
response_method |
The response method to use for the scorer. This can be a single method or an iterable of methods.
TYPE:
|
scorer_kwargs |
Additional keyword arguments to pass to the
scorer during the call. Forwards to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
_Scorer
|
The sklearn scorer. |
Source code in src/amltk/optimization/metric.py
compare
#
compare(v1: float, v2: float) -> Comparison
Check if v1
is better than v2
.
Source code in src/amltk/optimization/metric.py
distance_to_optimal
#
The distance to the optimal value, using the bounds if possible.
Source code in src/amltk/optimization/metric.py
from_str
classmethod
#
from_str(s: str) -> Self
Create an metric from a str.
from amltk.optimization import Metric
s = "loss (minimize)"
metric = Metric.from_str(s)
print(metric)
s = "accuracy [0.0, 1.0] (maximize)"
metric = Metric.from_str(s)
print(metric)
PARAMETER | DESCRIPTION |
---|---|
s |
The string to parse.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
The parsed metric. |
Source code in src/amltk/optimization/metric.py
loss
#
normalized_loss
#
The normalized loss of a value if possible.
If both sides of the bounds are finite, we can normalize the value to be between 0 and 1.
Source code in src/amltk/optimization/metric.py
MetricCollection
dataclass
#
A collection of metrics.
metrics
class-attribute
instance-attribute
#
The metrics in this collection.
as_sklearn_scorer
#
as_sklearn_scorer(
*,
response_methods: (
Mapping[
str,
SklearnResponseMethods
| Sequence[SklearnResponseMethods],
]
| None
) = None,
scorer_kwargs: (
Mapping[str, Mapping[str, Any]] | None
) = None,
raise_exc: bool = True
) -> _MultimetricScorer
Convert this collection to a sklearn scorer.
Source code in src/amltk/optimization/metric.py
from_collection
classmethod
#
from_collection(
metrics: (
Metric | Iterable[Metric] | Mapping[str, Metric]
)
) -> MetricCollection
Create a metric collection from an iterable of metrics.
Source code in src/amltk/optimization/metric.py
from_empty
classmethod
#
from_empty() -> MetricCollection