mlrl.common.rule_learners module

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

Provides base classes for implementing rule learning algorithms.

class mlrl.common.rule_learners.ClassificationRuleLearner(random_state: int | None, feature_format: str | None, output_format: str | None, prediction_format: str | None)

Bases: RuleLearner, ClassifierMixin, IncrementalClassifierMixin, ABC

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

class NativeIncrementalProbabilityPredictor(feature_matrix: RowWiseFeatureMatrix, incremental_predictor)

Bases: NativeIncrementalPredictor

Allows to obtain probability estimates from a ClassificationRuleLearner incrementally by using its native support of 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)

Bases: NonNativeIncrementalPredictor

Allows to obtain probability estimates from a ClassificationRuleLearner incrementally.

apply_next(step_size: int)

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

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

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$') ClassificationRuleLearner

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$') ClassificationRuleLearner

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$') ClassificationRuleLearner

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.rule_learners.RegressionRuleLearner(random_state: int | None, feature_format: str | None, output_format: str | None, prediction_format: str | None)

Bases: RuleLearner, RegressorMixin, IncrementalRegressorMixin, ABC

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

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

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$') RegressionRuleLearner

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$') RegressionRuleLearner

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.rule_learners.RuleLearner(random_state: int | None, feature_format: str | None, output_format: str | None, prediction_format: str | None)

Bases: BaseEstimator, NominalFeatureSupportMixin, OrdinalFeatureSupportMixin, ABC

A scikit-learn implementation of a rule learning algorithm.

class NativeIncrementalPredictor(feature_matrix: RowWiseFeatureMatrix, incremental_predictor)

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)

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.rule_learners.SparsePolicy(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

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'
static parse(parameter_name: str, value: str | None) SparsePolicy

Parses and returns a parameter value that specifies a SparsePolicy to be used for converting matrices into sparse or dense formats. If the given value is invalid, a ValueError is raised.

Parameters:
  • parameter_name – The name of the parameter

  • value – The value to be parsed or None, if the default value should be used

Returns:

A SparsePolicy

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