mlrl.common.learners module

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

Provides base classes for implementing rule learning algorithms.

class mlrl.common.learners.ClassificationRuleLearner(*args: Any, **kwargs: Any)

Bases: IncrementalClassifierMixin, RuleLearner, ABC

A scikit-learn implementation of a rule learning algorithm that can be applied to classification problems.

class mlrl.common.learners.ProbabilisticClassificationRuleLearner(*args: Any, **kwargs: Any)

Bases: MarginClassifierMixin, IncrementalMarginClassifierMixin, ProbabilisticClassifierMixin, IncrementalProbabilisticClassifierMixin, ClassificationRuleLearner, ABC

A scikit-learn implementation of a probabilistic rule learning algorithm that can be applied to classification problems.

class NativeIncrementalProbabilityPredictor(feature_matrix: RowWiseFeatureMatrix, incremental_predictor, label_encoder: LabelEncoder | None = None, dtype: dtype | None = None)

Bases: NativeIncrementalPredictor

Allows to obtain probability estimates from a ProbabilisticClassificationRuleLearner incrementally by using its native support for this functionality.

apply_next(step_size: int)

See mlrl.common.mixins.IncrementalPredictor.apply_next()

class NonNativeIncrementalProbabilityPredictor(feature_matrix: RowWiseFeatureMatrix, model: RuleModel, max_rules: int, predictor, label_encoder: LabelEncoder | None = None, dtype: dtype | None = None)

Bases: NonNativeIncrementalPredictor

Allows to obtain probability estimates from a ProbabilisticClassificationRuleLearner incrementally.

apply_next(step_size: int)

See mlrl.common.mixins.IncrementalPredictor.apply_next()

class mlrl.common.learners.RegressionRuleLearner(*args: Any, **kwargs: Any)

Bases: IncrementalRegressorMixin, RuleLearner, ABC

A scikit-learn implementation of a rule learning algorithm that can be applied to regression problems.

class mlrl.common.learners.RuleLearner(*args: Any, **kwargs: Any)

Bases: NominalFeatureSupportMixin, OrdinalFeatureSupportMixin, LearnerMixin, ABC

A scikit-learn implementation of a rule learning algorithm.

class ConstantIncrementalPredictor(constant_predictions: ndarray, label_encoder: LabelEncoder | None = None)

Bases: IncrementalPredictor

Allows to obtain constant predictions from a RuleLearner incrementally.

apply_next(step_size: int)

See mlrl.common.mixins.IncrementalPredictor.apply_next()

static binary(num_examples: int, num_outputs: int, value, label_encoder: LabelEncoder | None = None) RuleLearner.ConstantIncrementalPredictor

Creates a ConstantIncrementalPredictor that predicts a binary value.

Parameters:
  • num_examples – The number of examples to predict for

  • num_outputs – The number of outputs to predict for

  • value – The value to be predicted

  • label_encoder – An optional LabelEncoder that should be used to decode the predictions

get_num_next() int

See mlrl.common.mixins.IncrementalPredictor.get_num_next()

static probability(num_examples: int, num_outputs: int, value) ConstantIncrementalPredictor

Creates a ConstantIncrementalPredictor that predicts a probability.

Parameters:
  • num_examples – The number of examples to predict for

  • num_outputs – The number of outputs to predict for

  • value – The value to be predicted

static score(num_examples: int, num_outputs: int, value) ConstantIncrementalPredictor

Creates a ConstantIncrementalPredictor that predicts a score.

Parameters:
  • num_examples – The number of examples to predict for

  • num_outputs – The number of outputs to predict for

  • value – The value to be predicted

KWARG_DTYPE = 'dtype'
KWARG_EXAMPLE_WEIGHTS = 'sample_weight'
KWARG_MAX_RULES = 'max_rules'
KWARG_SPARSE_FEATURE_VALUE = 'sparse_feature_value'
class NativeIncrementalPredictor(feature_matrix: RowWiseFeatureMatrix, incremental_predictor, label_encoder: LabelEncoder | None = None, dtype: dtype | None = None)

Bases: IncrementalPredictor

Allows to obtain predictions from a RuleLearner incrementally by using its native support of this functionality.

apply_next(step_size: int)

See mlrl.common.mixins.IncrementalPredictor.apply_next()

get_num_next() int

See mlrl.common.mixins.IncrementalPredictor.get_num_next()

has_next() bool

See mlrl.common.mixins.IncrementalPredictor.has_next()

class NonNativeIncrementalPredictor(feature_matrix: RowWiseFeatureMatrix, model: RuleModel, max_rules: int, predictor, label_encoder: LabelEncoder | None = None, dtype: dtype | None = None)

Bases: IncrementalPredictor

Allows to obtain predictions from a RuleLearner incrementally.

apply_next(step_size: int)

See mlrl.common.mixins.IncrementalPredictor.apply_next()

get_num_next() int

See mlrl.common.mixins.IncrementalPredictor.get_num_next()

class mlrl.common.learners.SparsePolicy(*values)

Bases: StrEnum

Specifies all valid textual representation of policies to be used for converting matrices into sparse or dense formats.

AUTO = 'auto'
FORCE_DENSE = 'dense'
FORCE_SPARSE = 'sparse'
should_enforce_sparse(matrix, sparse_format: SparseFormat, dtype, sparse_values: bool = True) bool

Returns whether it is preferable to convert a given matrix into a scipy.sparse.csr_array or scipy.sparse.csc_array, depending on the format of the given matrix and this SparsePolicy:

If the policy is SparsePolicy.AUTO, the matrix will be converted into the given sparse format, if possible and if the sparse matrix is expected to occupy less memory than a dense matrix. To be able to convert the matrix into a sparse format, it must be a scipy.sparse.spmatrix or scipy.sparse.sparray in the LIL, DOK, COO, CSR or CSC format.

If the policy is SparsePolicy.FORCE_SPARSE, the matrix will always be converted into the specified sparse format, if possible. Dense matrices will never be converted into a sparse format.

If the policy is SparsePolicy.FORCE_DENSE, the matrix will always be converted into a dense matrix.

Parameters:
  • matrix – A np.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

  • sparse_format – The SparseFormat to be used

  • dtype – The type of the values that should be stored in the matrix

  • sparse_values – True, if the values must explicitly be stored when using a sparse format, False otherwise

Returns:

True, if it is preferable to convert the matrix into a sparse matrix of the given format, False otherwise

mlrl.common.learners.configure_rule_learner(learner: RuleLearner, config: RuleLearnerConfig, parameters: set[Parameter])

Configures a rule learner by taking into account a given set of parameters.

Parameters:
  • learner – The rule learner to be configured

  • config – The configuration to be modified

  • parameters – A set that contains the parameters to be taken into account

mlrl.common.learners.convert_into_sklearn_compatible_probabilities(probabilities: ndarray) ndarray

Converts given probability estimates into a format that is compatible with scikit-learn.

Parameters:

probabilities – A np.ndarray that stores probability estimates

Returns:

A np.ndarray that is compatible with scikit-learn