Skip to content

Core

flowcean.core provides the shared interfaces and learning strategies used across Flowcean. See learning strategies for how environments, learners, and models fit together.

core

Attributes

Data module-attribute

Data = Any

Classes

Adapter

Bases: ABC

Abstract base class for adapters.

A component connecting Flowcean to cyber-physical systems. An adapter handles the interface between learned models and real cyber-physical systems during deployment. It manages communication with the system's sensors (for observations) and actuators (for applying actions), enabling the deployment of trained models in production.

Methods:
start abstractmethod
start() -> None

Start the adapter.

This method is called when the tool loop is started. It should be used to initialize the adapter, start any background processes and establish connections to the CPS.

stop abstractmethod
stop() -> None

Stop the adapter.

This method is called when the tool loop is stopped. It should be used to clean up resources, stop any background processes and close connections to the CPS.

get_data abstractmethod
get_data() -> Data

Get data from the CPS through the adapter.

Retrieve a data record from the CPS. This method should block until data is available. If no more data is available, it should raise a Stop exception.

Returns:

Type Description
Data

The data retrieved from the CPS.

send_data abstractmethod
send_data(data: Data) -> None

Send data to the CPS through the adapter.

This method allows sending data to the CPS. It is used by the tool loop to send the results for the tool evaluation back to the CPS for further processing.

Parameters:

Name Type Description Default
data Data

The data to send.

required

CallbackManager

CallbackManager(callbacks: list[LearnerCallback] | None = None)

Manage multiple callbacks through a single interface.

Initialize the callback manager.

Attributes
callbacks instance-attribute
callbacks = callbacks or []
Methods:
on_learning_start
on_learning_start(learner: Named, context: dict[str, Any] | None = None) -> None

Notify all callbacks that learning has started.

on_learning_progress
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None

Notify all callbacks of learning progress.

on_learning_end
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None

Notify all callbacks that learning has completed.

on_learning_error
on_learning_error(learner: Named, error: Exception) -> None

Notify all callbacks that learning has failed.

CallbackMixin

Mixin to add callback support to learners.

Attributes
callback_manager instance-attribute
callback_manager: CallbackManager

LearnerCallback

Bases: Protocol

Protocol for learner callbacks.

Methods:
on_learning_start abstractmethod
on_learning_start(learner: Named, context: dict[str, Any] | None = None) -> None

Called when learning starts.

on_learning_progress abstractmethod
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None

Called during learning with progress updates.

on_learning_end abstractmethod
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None

Called when learning completes successfully.

on_learning_error abstractmethod
on_learning_error(learner: Named, error: Exception) -> None

Called if learning fails with an error.

LoggingCallback

LoggingCallback(logger: Logger | None = None, level_start: int = INFO, level_progress: int = DEBUG, level_end: int = INFO, level_error: int = ERROR)

Bases: LearnerCallback

Standard Python logging callback.

Initialize the logging callback.

Attributes
logger instance-attribute
logger = logger or logging.getLogger('flowcean.learner')
level_start instance-attribute
level_start = level_start
level_progress instance-attribute
level_progress = level_progress
level_end instance-attribute
level_end = level_end
level_error instance-attribute
level_error = level_error
Methods:
on_learning_start
on_learning_start(learner: Named, context: dict[str, Any] | None = None) -> None

Log learning start event.

on_learning_progress
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None

Log learning progress.

on_learning_end
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None

Log learning completion.

on_learning_error
on_learning_error(learner: Named, error: Exception) -> None

Log learning error.

RichCallback

RichCallback(console: Console | None = None, *, show_metrics: bool = True)

Bases: LearnerCallback

Rich console callback with adaptive progress display.

Initialize the Rich callback.

Attributes
console instance-attribute
console = console or Console()
show_metrics instance-attribute
show_metrics = show_metrics
Methods:
on_learning_start
on_learning_start(learner: Named, context: dict[str, Any] | None = None) -> None

Display learning start message.

on_learning_progress
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None

Update progress display with current progress and metrics.

on_learning_end
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None

Display learning completion message.

on_learning_error
on_learning_error(learner: Named, error: Exception) -> None

Display error message.

RichSpinnerCallback

RichSpinnerCallback(console: Console | None = None)

Bases: LearnerCallback

Simplified Rich callback with just a spinner.

Initialize the Rich spinner callback.

Attributes
console instance-attribute
console = console or Console()
Methods:
on_learning_start
on_learning_start(learner: Named, context: dict[str, Any] | None = None) -> None

Display learning start message with spinner.

on_learning_progress
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None

Update spinner state.

on_learning_end
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None

Display learning completion message.

on_learning_error
on_learning_error(learner: Named, error: Exception) -> None

Display error message.

SilentCallback

A callback that intentionally produces no output.

Methods:
on_learning_start
on_learning_start(learner: Named, context: dict[str, Any] | None = None) -> None

Do nothing when learning starts.

on_learning_progress
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None

Do nothing during learning progress.

on_learning_end
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None

Do nothing when learning ends.

on_learning_error
on_learning_error(learner: Named, error: Exception) -> None

Do nothing when learning errors.

Actable

Bases: Protocol

Base class for active environments.

Active environments require actions to be taken to advance.

Methods:
act abstractmethod
act(action: Data) -> None

Act on the environment.

Parameters:

Name Type Description Default
action Data

The action to perform.

required

ActiveEnvironment

Bases: Environment, Stepable, Actable, Protocol

An environment supporting active learning through interaction.

An active environment loads data interactively from simulations or real systems. The learner influences the environment by selecting actions, which the environment responds to with observations and rewards. This supports active learning strategies where the learner explores the environment to optimize its behavior.

Environment

Bases: Named, Protocol

Abstraction for data sources in learning and evaluation.

An environment describes the possible data sources for the learning and evaluation procedure. Environments can be offline (pre-recorded datasets), incremental (streaming data), or active (interactive systems where the learner can influence the environment). All environments support applying transforms to observations.

Attributes
transform class-attribute instance-attribute
transform: Transform = Identity()
Methods:
append_transform
append_transform(transform: Transform) -> Self

Append a transform to the observation.

Parameters:

Name Type Description Default
transform Transform

Transform to append.

required

Returns:

Type Description
Self

This observable with the appended transform.

observe
observe() -> Data

Observe and return the observation.

Finished

Bases: Exception

Exception raised when the environment is finished.

This exception is raised when the environment is finished, and no more data can be retrieved.

IncrementalEnvironment

Bases: Environment, Stepable, Iterable[Data], Protocol

An environment providing incremental (streaming) learning data.

Incremental environments provide data as a continuous stream of samples or small batches. The environment is advanced by stepping through data, observing the current state at each step. This supports incremental learning (also known as passive online learning), where the model is continuously updated as new data arrives.

Methods:
num_steps
num_steps() -> int | None

Return the number of steps in the environment.

Returns:

Type Description
int | None

The number of steps in the environment, or None if the number of

int | None

steps is unknown.

Stepable

Bases: Protocol

Base class for stepable environments.

Stepable environments are environments that can be advanced by a step. Usually, this is combined with an observable to provide a stream of data.

Methods:
step abstractmethod
step() -> None

Advance the environment by one step.

ChainedOfflineEnvironments

ChainedOfflineEnvironments(environments: Iterable[Environment])

Bases: IncrementalEnvironment

Chained offline environments.

This environment chains multiple offline environments together. The environment will first observe the data from the first environment and then the data from the other environments.

Initialize the chained offline environments.

Parameters:

Name Type Description Default
environments Iterable[Environment]

The offline environments to chain.

required
Methods:
step
step() -> None

OfflineEnvironment

Bases: Environment, Protocol

Base class for offline environments.

Offline environments represent static, pre-recorded datasets collected upfront. They support the offline learning strategy where a fixed batch of data is processed at once to train a model. Offline environments can be transformed and chained together to create new datasets.

Methods:
chain

Chain this offline environment with other offline environments.

Chaining offline environments will create a new incremental environment that will first observe the data from this environment and then the data from the other environments.

Parameters:

Name Type Description Default
other Environment

The other offline environments to chain.

()

Returns:

Type Description
ChainedOfflineEnvironments

The chained offline environments.

ActiveLearner

Bases: Named, Protocol

Base class for active learners.

Active learners require actions to be taken to learn.

Methods:
learn_active abstractmethod
learn_active(action: Data, observation: Data) -> Model

Learn from actions and observations.

Parameters:

Name Type Description Default
action Data

The action performed.

required
observation Data

The observation of the environment.

required

Returns:

Type Description
Model

The model learned from the data.

propose_action abstractmethod
propose_action(observation: Data) -> Data

Propose an action based on an observation.

Parameters:

Name Type Description Default
observation Data

The observation of an environment.

required

Returns:

Type Description
Data

The action to perform.

SupervisedIncrementalLearner

Bases: Named, Protocol

Base class for incremental supervised learners.

An incremental supervised learner learns from input-output pairs incrementally.

Methods:
learn_incremental abstractmethod
learn_incremental(inputs: Data, outputs: Data) -> Model

Learn from the data incrementally.

Parameters:

Name Type Description Default
inputs Data

The input data.

required
outputs Data

The output data.

required

Returns:

Type Description
Model

The model learned from the data.

SupervisedLearner

Bases: Named, Protocol

Base class for supervised learners.

A supervised learner learns from input-output pairs.

Methods:
learn abstractmethod
learn(inputs: Data, outputs: Data) -> Model

Learn from the data.

Parameters:

Name Type Description Default
inputs Data

The input data.

required
outputs Data

The output data.

required

Returns:

Type Description
Model

The model learned from the data.

ActiveMetric

Bases: Named, Protocol

Base class for metrics for active environments.

Metric

Bases: Named, Protocol

Quantitative measure for evaluating model performance.

Metrics compare predictions with true outputs to assess model quality.

Call flow

call -> prepare(true), prepare(predicted) -> compute(true, predicted)

Methods:
prepare
prepare(data: Data) -> Data

Hook to normalize/collect/select data before computing metric.

Default: identity. Mixins override and call super().prepare(...)

compute
compute(true: Data, predicted: Data) -> Reportable

Implement metric logic on prepared inputs.

ClassifierModel

Bases: Model, Protocol

Protocol for classifier models with threshold-based predictions.

Extends Model with probability predictions and a configurable decision threshold. Implement this protocol for classifiers that support predict_proba.

Attributes
threshold instance-attribute
threshold: float
Methods:
predict_proba
predict_proba(input_features: Data) -> Data

Predict class probabilities.

Parameters:

Name Type Description Default
input_features Data

The inputs for which to predict probabilities.

required

Returns:

Type Description
Data

The predicted probabilities for the positive class.

Model

Bases: Named, Protocol

Base class for models.

A model is used to predict outputs for given inputs.

Attributes
pre_transform class-attribute instance-attribute
pre_transform: Transform = Identity()
post_transform class-attribute instance-attribute
post_transform: Transform = Identity()
Methods:
preprocess
preprocess(input_features: Data) -> Data

Preprocess pipeline step.

predict
predict(input_features: Data) -> Data

Predict outputs for given inputs, applying transforms and hooks.

postprocess
postprocess(output: Data) -> Data

Postprocess pipeline step.

save
save(file: Path | str | BinaryIO) -> None

Save the model to the file.

This method can be used to save a flowcean model to a file or a file-like object. To save a model to a file use

model.save("model.fml")

The resulting file will contain the model any any attached transforms. It can be loaded again using the load method from the Model class.

This method uses pickle to serialize the model, so child classes should ensure that all attributes are pickleable. If this is not the case, the child class should override this method to implement custom serialization logic, or use the __getstate__ and __setstate__ methods to control what is serialized (see https://docs.python.org/3/library/pickle.html#pickling-class-instances).

Parameters:

Name Type Description Default
file Path | str | BinaryIO

The file like object to save the model to.

required
load staticmethod
load(file: Path | str | BinaryIO) -> Model

Load a model from file.

This method can be used to load a previously saved flowcean model from a file or a file-like object. To load a model from a file use

model = Model.load("model.fml")

The load method will automatically determine the model type and and any attached transforms and will load them into the correct model class.

As this method uses the pickle module to load the model, it is not safe to load models from untrusted sources as this could lead to arbitrary code execution!

Parameters:

Name Type Description Default
file Path | str | BinaryIO

The file like object to load the model from.

required

Report

Bases: dict[str, ReportEntry]

A structured container for evaluation results of multiple models.

The Report maps model names to their metric results. For each model:

  • top-level keys are metric names,
  • values are either:
    • a single Reportable (e.g., scalar metric result), or
    • a nested mapping from submetric names to Reportable objects (e.g., per-class F1 scores, per-feature regression errors, or multi-output results).

This hierarchical structure allows uniform representation of both simple metrics and complex hierarchical metrics across multiple models.

Example:

report = Report( ... { ... "model_a": { ... "accuracy": 0.95, ... "f1": {"class_0": 0.91, "class_1": 0.89}, ... }, ... "model_b": { ... "mae": {"feature_x": 0.2, "feature_y": 0.3}, ... }, ... } ... )

Methods:
great_table
great_table() -> GT
pretty_print
pretty_print(header_style: StyleType = 'bold magenta', metric_style: StyleType = 'cyan', value_style: StyleType = 'green', title_style: StyleType = 'bold yellow') -> None

Pretty print the report to the terminal.

Reportable

Bases: Protocol

Action dataclass

Action(actuators: list[ActiveInterface])

An action in an active environment.

The action contains 'actuators', which represent setpoints in the environment. Each actuator targets exactly one input feature.

Parameters:

Name Type Description Default
actuators list[ActiveInterface]

List of interface objects, which are setpoints

required
Attributes
actuators instance-attribute
actuators: list[ActiveInterface]

ActiveInterface dataclass

ActiveInterface(uid: str, value: int | float | NDArray[Any] | None, value_min: SupportsFloat | NDArray[Any] | list[Any], value_max: SupportsFloat | NDArray[Any] | list[Any], shape: Sequence[int], dtype: type[floating[Any]] | type[integer[Any]])

Interface to a feature in an active environment.

Represents a single feature of the environment, which can be either an input, an output, or the reward of the environment.

Parameters:

Name Type Description Default
uid str

Identifier of the feature inside the environment

required
value int | float | NDArray[Any] | None

The value of the feature

required
value_min SupportsFloat | NDArray[Any] | list[Any]

Simple representation of the minimum value

required
value_max SupportsFloat | NDArray[Any] | list[Any]

Simple representation of the maximum value

required
shape Sequence[int]

Tuple representing the shape of the value

required
dtype type[floating[Any]] | type[integer[Any]]

Data type of this interface, e.g., numpy.float32

required
Attributes
uid instance-attribute
uid: str
value instance-attribute
value: int | float | NDArray[Any] | None
value_min instance-attribute
value_min: SupportsFloat | NDArray[Any] | list[Any]
value_max instance-attribute
value_max: SupportsFloat | NDArray[Any] | list[Any]
shape instance-attribute
shape: Sequence[int]
dtype instance-attribute
dtype: type[floating[Any]] | type[integer[Any]]

Observation dataclass

Observation(sensors: list[ActiveInterface], rewards: list[ActiveInterface])

An observation of an active environment.

The observation contains 'sensors', which are the raw observations of featured values, and rewards, which are a rated quantification of the environment state.

Parameters:

Name Type Description Default
sensors list[ActiveInterface]

List of interface objects, i.e., raw observations

required
rewards list[ActiveInterface]

List of interface objects, i.e., rated state

required
Attributes
sensors instance-attribute
sensors: list[ActiveInterface]
rewards instance-attribute
rewards: list[ActiveInterface]

StopLearning

Bases: Exception

Stop learning.

This exception is raised when the learning process should stop.

ChainedTransforms

ChainedTransforms(*transforms: Transform)

Bases: Invertible, Transform

A composition of multiple transforms applied sequentially.

Chained transforms are applied left-to-right. Useful for building preprocessing pipelines.

Initialize the chained transforms.

Parameters:

Name Type Description Default
transforms Transform

The transforms to chain.

()
Attributes
transforms instance-attribute
transforms: Sequence[Transform] = transforms
Methods:
apply
apply(data: Data) -> Data
chain
chain(other: Transform) -> Transform
fit
fit(data: Data) -> Self
fit_incremental
fit_incremental(data: Data) -> Self
inverse
inverse() -> Transform

Identity

Identity()

Bases: Invertible, Transform

A no-op transform that returns data unchanged.

Often used as a placeholder or default transform.

Initialize the identity transform.

Methods:
apply
apply(data: Data) -> Data
inverse
inverse() -> Transform
chain
chain(other: Transform) -> Transform

Invertible

Bases: Protocol

Protocol for transforms that support inversion.

An invertible transform can undo its effect via inverse().

Example
>>> scaler = Standardize().fit(data)
>>> restored = scaler.inverse()(scaler(data))
Methods:
inverse abstractmethod
inverse() -> Transform

Return a new transform that inverts this one.

Returns:

Type Description
Transform

The inverse of the transform.

InvertibleTransform

Bases: Transform, Invertible, Protocol

Lambda

Lambda(func: Callable[[Data], Data], *, inverse_func: Callable[[Data], Data] | None = None)

Bases: Transform, Invertible

A transform wrapping a function.

Useful for quick one-off transformations without creating a dedicated class.

Example
>>> to_float = Lambda(lambda df: df.cast(pl.Float64))
>>> normalized = Lambda(
...     lambda df: (df - df.mean()) / df.std(),
...     inverse_func=lambda df: df * df.std() + df.mean(),
... )

Initialize the lambda transform.

Parameters:

Name Type Description Default
func Callable[[Data], Data]

Function that transforms data.

required
inverse_func Callable[[Data], Data] | None

Optional function that inverts func.

None
Attributes
func instance-attribute
func: Callable[[Data], Data] = func
inverse_func instance-attribute
inverse_func: Callable[[Data], Data] | None = inverse_func
Methods:
apply
apply(data: Data) -> Data
inverse
inverse() -> Transform

Transform

Bases: Named, Protocol

Base protocol for all transforms in Flowcean.

A transform is a reusable operation that modifies data. Examples include preprocessing (e.g., standardization), feature engineering (e.g., feature selection, PCA), or augmentation (noise injection, synthetic features).

Transforms are composable via the | operator, allowing complex transformation pipelines to be expressed in a clean and functional style:

Example
>>> transform = Select(features=["x"]) | Standardize()
>>> transformed = transform(dataset)
Methods:
apply abstractmethod
apply(data: Data) -> Data

Apply the transform to data.

Parameters:

Name Type Description Default
data Data

The data to transform.

required

Returns:

Type Description
Data

The transformed data.

chain
chain(other: Transform) -> Transform

Chain this transform with other.

This can be used to chain multiple transforms together. Chained transforms are applied left-to-right:

Example
chained = TransformA().chain(TransformB())
chained(data)  # Equivalent to TransformB(TransformA(data))

Parameters:

Name Type Description Default
other Transform

The transforms to chain.

required

Returns:

Type Description
Transform

A new chained transform.

fit
fit(data: Data) -> Self

Fit the transform to data.

Many transforms (e.g. scaling, PCA) require statistics from the dataset before applying. Default implementation is a no-op. This is meant to be idempotent, i.e., calling fit() multiple times should have the same effect as calling it once.

Parameters:

Name Type Description Default
data Data

The data to fit to.

required
fit_incremental
fit_incremental(data: Data) -> Self

Incrementally fit the transform to streaming/batched data.

Default implementation is a no-op.

Parameters:

Name Type Description Default
data Data

The data to fit to.

required

Functions:

create_callback_manager

create_callback_manager(callbacks: list[LearnerCallback] | LearnerCallback | None) -> CallbackManager

Create a CallbackManager from supported callback inputs.

Parameters:

Name Type Description Default
callbacks list[LearnerCallback] | LearnerCallback | None

Callbacks to manage. Can be: - None: Uses no callbacks - Single callback: Wraps in a list - List of callbacks: Uses directly

required

Returns:

Type Description
CallbackManager

CallbackManager instance.

get_default_callbacks

get_default_callbacks() -> list[LearnerCallback]

Return the default callbacks for learners.

Returns:

Type Description
list[LearnerCallback]

An empty list so learners stay silent unless callbacks are provided.

learn_active

learn_active(environment: ActiveEnvironment, learner: ActiveLearner) -> Model

Learn from an active environment.

Learn from an active environment by interacting with it and learning from the observations. The learning process stops when the environment ends or when the learner requests to stop.

Parameters:

Name Type Description Default
environment ActiveEnvironment

The active environment.

required
learner ActiveLearner

The active learner.

required

Returns:

Type Description
Model

The model learned from the environment.

deploy

deploy(environment: ActiveEnvironment | IncrementalEnvironment, model: Model, input_transforms: Transform | None = None, output_transforms: Transform | None = None) -> None

Deploy a trained model to a custom environment.

Parameters:

Name Type Description Default
environment ActiveEnvironment | IncrementalEnvironment

custom system environment

required
model Model

the trained model

required
input_transforms Transform | None

system specific transforms for model input

None
output_transforms Transform | None

system specific transforms for system input

None

learn_incremental

learn_incremental(environment: IncrementalEnvironment, learner: SupervisedIncrementalLearner, inputs: list[str], outputs: list[str], input_transform: Transform | None = None, output_transform: InvertibleTransform | None = None) -> Model

Learn from a incremental environment.

Learn from a incremental environment by incrementally learning from the input-output pairs. The learning process stops when the environment ends.

Parameters:

Name Type Description Default
environment IncrementalEnvironment

The incremental environment.

required
learner SupervisedIncrementalLearner

The supervised incremental learner.

required
inputs list[str]

The input feature names.

required
outputs list[str]

The output feature names.

required
input_transform Transform | None

The transform to apply to the input features. Will be part of the final model.

None
output_transform InvertibleTransform | None

The transform to apply to the output features. Its inverse will be part of the final model.

None

Returns:

Type Description
Model

The model learned from the environment.

evaluate_offline

evaluate_offline(models: Model | Iterable[Model], environment: OfflineEnvironment, inputs: Sequence[str], outputs: Sequence[str], metrics: Sequence[Metric]) -> Report

Evaluate a model on an offline environment.

Evaluate a model on an offline environment by predicting the outputs from the inputs and comparing them to the true outputs.

Parameters:

Name Type Description Default
models Model | Iterable[Model]

The models to evaluate.

required
environment OfflineEnvironment

The offline environment.

required
inputs Sequence[str]

The input feature names.

required
outputs Sequence[str]

The output feature names.

required
metrics Sequence[Metric]

The metrics to evaluate the model with.

required

Returns:

Type Description
Report

The evaluation report.

learn_offline

learn_offline(environment: OfflineEnvironment, learner: SupervisedLearner, inputs: list[str], outputs: list[str], *, input_transform: Transform | None = None, output_transform: InvertibleTransform | None = None) -> Model

Learn from an offline environment.

Learn from an offline environment by learning from the input-output pairs.

Parameters:

Name Type Description Default
environment OfflineEnvironment

The offline environment.

required
learner SupervisedLearner

The supervised learner.

required
inputs list[str]

The input feature names.

required
outputs list[str]

The output feature names.

required
input_transform Transform | None

The transform to apply to the input features. Will be part of the final model.

None
output_transform InvertibleTransform | None

The transform to apply to the output features. Its inverse will be part of the final model.

None

Returns:

Type Description
Model

The model learned from the environment.

tune_threshold

tune_threshold(model: ClassifierModel, environment: OfflineEnvironment, inputs: Sequence[str], outputs: Sequence[str], metric: Metric, *, thresholds: Sequence[float] | None = None, num_thresholds: int = 19) -> tuple[float, dict[float, float]]

Find optimal decision threshold for a classifier.

Evaluates the model at multiple threshold values and returns the threshold that maximizes the given metric.

Parameters:

Name Type Description Default
model ClassifierModel

The classifier model to tune.

required
environment OfflineEnvironment

The offline environment with validation/test data.

required
inputs Sequence[str]

The input feature names.

required
outputs Sequence[str]

The output feature names.

required
metric Metric

The metric to optimize (e.g., FBetaScore, Accuracy).

required
thresholds Sequence[float] | None

Specific thresholds to evaluate. If None, generates num_thresholds evenly spaced values between 0.05 and 0.95.

None
num_thresholds int

Number of thresholds to evaluate if thresholds is None (default: 19).

19

Returns:

Type Description
float

Tuple of (best_threshold, results_dict) where results_dict maps

dict[float, float]

each threshold to its metric score.

Example

from flowcean.sklearn.metrics.classification import FBetaScore metric = FBetaScore(beta=1.0) best_threshold, results = tune_threshold( ... model, eval_env, inputs, outputs, metric ... ) print(f"Best threshold: {best_threshold:.3f}") model.threshold = best_threshold # Apply the best threshold