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,ABCA mixin for all machine learning algorithms that can be applied to classification problems.
- 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
fitmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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
xparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, x: bool | None | str = '$UNCHANGED$') ClassifierMixin¶
Request metadata passed to the
predict_probamethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_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
xparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, x: bool | None | str = '$UNCHANGED$') ClassifierMixin¶
Request metadata passed to the
predictmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.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
xparameter inpredict.
Returns¶
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') ClassifierMixin¶
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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_weightparameter inscore.
Returns¶
- selfobject
The updated object.
- class mlrl.common.mixins.IncrementalClassifierMixin¶
Bases:
ABCA 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:
ABCA base class for all classes that allow to obtain incremental predictions from a machine learning algorithm.
- abstract 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
- class mlrl.common.mixins.IncrementalRegressorMixin¶
Bases:
ABCA 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:
ABCA mixin for all machine learning algorithms that natively support nominal features.
- set_nominal_feature_indices(indices)¶
Sets the indices of all nominal features.
- Parameters:
indices – A np.ndarray or Iterable that stores the indices to be set
- class mlrl.common.mixins.OrdinalFeatureSupportMixin¶
Bases:
ABCA mixin for all machine learning algorithms that natively support ordinal features.
- set_ordinal_feature_indices(indices)¶
Sets the indices of all ordinal features.
- Parameters:
indices – A np.ndarray or Iterable that stores the indices to be set
- class mlrl.common.mixins.RegressorMixin¶
Bases:
BaseEstimator,RegressorMixin,MultiOutputMixin,ABCA 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
fitmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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
xparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, x: bool | None | str = '$UNCHANGED$') RegressorMixin¶
Request metadata passed to the
predictmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.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
xparameter inpredict.
Returns¶
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RegressorMixin¶
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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_weightparameter inscore.
Returns¶
- selfobject
The updated object.