ScoringMonitor#

class sklearn.callback.ScoringMonitor(*, scoring_train='no_train_score', scoring_val='no_val_score')[source]#

Callback that monitors a score for each iterative step of an estimator.

The specified scorers are called on the training and validation data at each iterative step of the estimator, and the score is logged by the callback. The logs can be retrieved through the get_logs method.

Parameters:
scoring_trainstr, callable, list, tuple or dict, default=”no_train_score”

The scoring method to use to monitor the model on the training data.

If scoring_train represents a single score, one can use:

If scoring_train represents multiple scores, one can use:

  • a list or tuple of unique strings;

  • a callable returning a dictionary where the keys are the metric names and the values are the metric scores;

  • a dictionary with metric names as keys and callables as values.

If scoring_train = 'no_train_score', scores are not computed on the train set.

scoring_valstr, callable, list, tuple, or dict, default=”no_val_score”

The scoring method to use to monitor the model on the validation data.

If scoring_val represents a single score, one can use:

If scoring_val represents multiple scores, one can use:

  • a list or tuple of unique strings;

  • a callable returning a dictionary where the keys are the metric names and the values are the metric scores;

  • a dictionary with metric names as keys and callables as values.

If scoring_val = 'no_val_score', scores are not computed on the validation set.

get_logs(select='most_recent', include_lineage=False)[source]#

Retrieve the logged scores.

Log entries are grouped by runs, which are the outermost enclosing fit calls. If the estimator this callback is registered on is wrapped in meta-estimators, a run corresponds to one fit of the outermost meta-estimator. If it is not wrapped in a meta-estimator, a run simply corresponds to a single fit of the estimator.

For a given run, the scores are logged in a ScoringMonitorLog object containing:

  • run_id: a unique identifier for the run;

  • estimator_name: the name of the (meta-)estimator of the run;

  • timestamp: the timestamp of the start of the run;

  • train_scores: the recorded scores on the training data for the run. Each score value is associated with the context of the task for which the score was computed;

  • train_scores_as_pandas: the recorded training scores as a Pandas DataFrame.

  • val_scores: the recorded scores on the validation data for the run. Each score value is associated with the context of the task for which the score was computed;

  • val_scores_as_pandas: the recorded validation scores as a Pandas DataFrame.

See ScoringMonitorLog for more details about the structure of the recorded scores.

Parameters:
select{“all”, “most_recent”}, default=”most_recent”

Which log run to return:

  • "all": return the logged scores for all runs;

  • "most_recent": return the logged scores for the most recent run.

include_lineagebool, default=False

Whether to include lineage information of the tasks in the log.

If set to True, the log contains extra rows for each task that is an ancestor of a task for which the score was computed. These extra rows can be used to retrieve the context of all ancestor tasks of a given task for which the score was computed. These extra rows have no score entries, which appear as NaN values in train_scores_as_pandas and val_scores_as_pandas.

Returns:
logsScoringMonitorLog or list of ScoringMonitorLog

The logged scores. If select=="most_recent", returns a single ScoringMonitorLog object. Otherwise, returns the list of all run logs.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

on_fit_task_begin(estimator, context, *, X=None, y=None, fitted_estimator=None)[source]#

Method called at the beginning of each fit task of the estimator.

Parameters:
estimatorestimator instance

The estimator calling this callback hook.

contextsklearn.callback.CallbackContext instance

Context of the corresponding task.

Xarray-like

The training data at this task.

yarray-like

The training target values at this task.

fitted_estimatorestimator instance

A new instance of the estimator that is ready to predict, transform, etc … as if fit had stopped at the beginning of this task.

on_fit_task_end(estimator, context, *, X=None, y=None, fitted_estimator=None, X_val=None, y_val=None, **score_params)[source]#

Method called at the end of each fit task of the estimator.

Parameters:
estimatorestimator instance

The estimator calling this callback hook.

contextsklearn.callback.CallbackContext instance

Context of the corresponding task.

Xarray-like

The training data at this task.

yarray-like

The training target values at this task.

fitted_estimatorestimator instance

A new instance of the estimator that is ready to predict, transform, etc … as if fit had stopped at the end of this task.

X_valarray-like

The validation data at this task.

y_valarray-like

The validation target at this task.

**score_paramsdict

Parameters to pass to the score method of the underlying scorer.

Returns:
stopbool

Whether or not to stop the current level of iterations at this task.

set_on_fit_task_end_request(*, X_val: bool | None | str = '$UNCHANGED$', y_val: bool | None | str = '$UNCHANGED$') → ScoringMonitor[source]#

Configure whether metadata should be requested to be passed to the on_fit_task_end method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to on_fit_task_end if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to on_fit_task_end.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
X_valstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for X_val parameter in on_fit_task_end.

y_valstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for y_val parameter in on_fit_task_end.

Returns:
selfobject

The updated object.

setup(estimator, context)[source]#

Method called at the beginning of the fit method of the estimator.

For auto-propagated callbacks, this method is called only once, before running the fit method of the outermost estimator.

Parameters:
estimatorestimator instance

The estimator calling this callback hook.

contextsklearn.callback.CallbackContext instance

Context of the corresponding task. This is usually the root context of the estimator but it can be an intermediate context if the estimator is a sub-estimator of a meta-estimator.

teardown(estimator, context)[source]#

Method called after finishing the fit method of the estimator.

For auto-propagated callbacks, this method is called only once, after finishing the fit method of the outermost estimator.

Parameters:
estimatorestimator instance

The estimator calling this callback hook.

contextsklearn.callback.CallbackContext instance

Context of the corresponding task. This is usually the root context of the estimator but it can be an intermediate context if the estimator is a sub-estimator of a meta-estimator.