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(*args: Any, **kwargs: Any)

Bases: ClassifierMixin, MultiOutputMixin, LearnerMixin, ABC

A 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 binary 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_labels), that stores the prediction for individual examples and labels

class mlrl.common.mixins.IncrementalClassifierMixin(*args: Any, **kwargs: Any)

Bases: ClassifierMixin, 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 binary 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.IncrementalMarginClassifierMixin(*args: Any, **kwargs: Any)

Bases: LearnerMixin, ABC

A mixin for all machine learning algorithms that can be applied to classification problems, is able to estimate the distance from examples to the decision boundary, and support incremental prediction.

decision_function_incrementally(x, **kwargs) IncrementalPredictor

Returns an IncrementalPredictor that allows to obtain predicted scores 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.IncrementalProbabilisticClassifierMixin

Bases: ABC

A mixin for all probabilistic machine learning algorithms that can be applied to classification problems and support incremental prediction.

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.IncrementalRegressorMixin(*args: Any, **kwargs: Any)

Bases: RegressorMixin, 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.LearnerMixin(*args: Any, **kwargs: Any)

Bases: BaseEstimator, ABC

A mixin for all machine learning algorithms.

class mlrl.common.mixins.MarginClassifierMixin(*args: Any, **kwargs: Any)

Bases: LearnerMixin, ABC

A mixin for all machine learning algorithms that can be applied to classification problems and are able to estimate the distance from examples to the decision boundary.

decision_function(x, **kwargs)

Obtains and returns predicted scores 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 prediction for individual examples and labels

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

Bases: ABC

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

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

class mlrl.common.mixins.RegressorMixin(*args: Any, **kwargs: Any)

Bases: RegressorMixin, MultiOutputMixin, LearnerMixin, 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