mlrl.common.mixins module

Author: Michael Rapp (michael.rapp.ml@gmail.com)

Provides mixins that additional functionality to machine learning algorithms.

class mlrl.common.mixins.ClassifierMixin

Bases: BaseEstimator, ClassifierMixin, MultiOutputMixin, ABC

A mixin for all machine learning algorithms that can be applied to classification problems.

KWARG_PREDICT_SCORES = 'predict_scores'
fit(x, y, **kwargs)

Fits a model to given training examples and their corresponding ground truth labels.

Parameters:
  • x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the training examples

  • y – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_labels), that stores the labels of the training examples according to the ground truth

Returns:

The fitted learner

predict(x, **kwargs)

Obtains and returns predictions for given query examples. If the optional keyword argument predict_scores is set to True, scores are obtained instead of binary predictions.

Parameters:
  • predict_scores – True, if scores should be obtained, False, if binary predictions should be obtained

  • x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the query examples

Returns:

A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray of shape (num_examples, num_labels), that stores the prediction for individual examples and labels

predict_proba(x, **kwargs)

Obtains and returns probability estimates for given query examples.

Parameters:

x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the query examples

Returns:

A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray of shape (num_examples, num_labels), that stores the probabilities for individual examples and labels

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') ClassifierMixin

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit 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 fit.

  • 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

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

Metadata routing for x parameter in fit.

Returns

selfobject

The updated object.

set_predict_proba_request(*, x: bool | None | str = '$UNCHANGED$') ClassifierMixin

Request metadata passed to the predict_proba method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict_proba 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 predict_proba.

  • 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

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

Metadata routing for x parameter in predict_proba.

Returns

selfobject

The updated object.

set_predict_request(*, x: bool | None | str = '$UNCHANGED$') ClassifierMixin

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict 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 predict.

  • 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

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

Metadata routing for x parameter in predict.

Returns

selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') ClassifierMixin

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score 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 score.

  • 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

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

Metadata routing for sample_weight parameter in score.

Returns

selfobject

The updated object.

class mlrl.common.mixins.IncrementalClassifierMixin

Bases: ABC

A mixin for all machine learning algorithms that can be applied to classification problems and support incremental prediction. For example, when dealing with ensemble models that consist of several ensemble members, it is possible to consider only a subset of the ensemble members for prediction.

predict_incrementally(x, **kwargs) IncrementalPredictor

Returns an IncrementalPredictor that allows to obtain predictions for given query examples incrementally.

Parameters:
  • x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the query examples

  • predict_scores – True, if scores should be obtained, False, if binary predictions should be obtained

Returns:

The IncrementalPredictor that has been created

predict_proba_incrementally(x, **kwargs) IncrementalPredictor

Returns an IncrementalPredictor that allows to obtain probability estimates for given query examples incrementally.

Parameters:

x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the query examples

Returns:

The IncrementalPredictor that has been created

class mlrl.common.mixins.IncrementalPredictor

Bases: ABC

A base class for all classes that allow to obtain incremental predictions from a machine learning algorithm.

abstractmethod apply_next(step_size: int)

Updates the current predictions by considering several of the remaining ensemble members. If not enough ensemble members are remaining, only the available ones will be used for updating the current predictions.

Parameters:

step_size – The number of additional ensemble members to be considered for prediction

Returns:

A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray of shape (num_examples, num_outputs), that stores the updated prediction for individual examples and outputs

abstractmethod get_num_next() int

Returns the number of remaining ensemble members that have not been used yet.

Returns:

The number of remaining ensemble members

has_next() bool

Returns whether there are any remaining ensemble members that have not been used yet or not.

Returns:

True, if there are any remaining ensemble members, False otherwise

class mlrl.common.mixins.IncrementalRegressorMixin

Bases: ABC

A mixin for all machine learning algorithms that can be applied to regression problems and support incremental prediction. For example, when dealing with ensemble models that consist of several ensemble members, it is possible to consider only a subset of the ensemble members for prediction.

predict_incrementally(x, **kwargs) IncrementalPredictor

Returns an IncrementalPredictor that allows to obtain predictions for given query examples incrementally.

Parameters:

x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the query examples

Returns:

The IncrementalPredictor that has been created

class mlrl.common.mixins.NominalFeatureSupportMixin

Bases: ABC

A mixin for all machine learning algorithms that natively support nominal features.

The fit method of such algorithms must accept the indices of nominal features as the keyword argument NominalFeatureSupportMixin.KWARG_NOMINAL_FEATURE_INDICES.

KWARG_NOMINAL_FEATURE_INDICES = 'nominal_feature_indices'
class mlrl.common.mixins.OrdinalFeatureSupportMixin

Bases: ABC

A mixin for all machine learning algorithms that natively support ordinal features.

The fit method of such algorithms must accept the indices of ordinal features as the keyword argument OrdinalFeatureSupportMixin.KWARG_ORDINAL_FEATURE_INDICES.

KWARG_ORDINAL_FEATURE_INDICES = 'ordinal_feature_indices'
class mlrl.common.mixins.RegressorMixin

Bases: BaseEstimator, RegressorMixin, MultiOutputMixin, ABC

A mixin for all machine learning algorithms that can be applied to regression problems.

fit(x, y, **kwargs)

Fits a model to given training examples and their corresponding ground truth regression scores.

Parameters:
  • x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the training examples

  • y – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_outputs), that stores the regression scores of the training examples according to the ground truth

Returns:

The fitted learner

predict(x, **kwargs)

Obtains and returns predictions for given query examples.

Parameters:

x – A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray, shape (num_examples, num_features), that stores the feature values of the query examples

Returns:

A numpy.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray of shape (num_examples, num_outputs), that stores the prediction for individual examples and outputs

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') RegressorMixin

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit 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 fit.

  • 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

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

Metadata routing for x parameter in fit.

Returns

selfobject

The updated object.

set_predict_request(*, x: bool | None | str = '$UNCHANGED$') RegressorMixin

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict 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 predict.

  • 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

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

Metadata routing for x parameter in predict.

Returns

selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RegressorMixin

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score 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 score.

  • 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

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

Metadata routing for sample_weight parameter in score.

Returns

selfobject

The updated object.