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(feature_format: str | None, output_format: str | None, prediction_format: str | None)¶
Bases:
RuleLearner,ClassifierMixin,IncrementalClassifierMixin,ABCA scikit-learn implementation of a rule learning algorithm that can be applied to classification problems.
- class NativeIncrementalProbabilityPredictor(feature_matrix: RowWiseFeatureMatrix, incremental_predictor)¶
Bases:
NativeIncrementalPredictorAllows to obtain probability estimates from a ClassificationRuleLearner incrementally by using its native support of this functionality.
- class NonNativeIncrementalProbabilityPredictor(feature_matrix: RowWiseFeatureMatrix, model: RuleModel, max_rules: int, predictor)¶
Bases:
NonNativeIncrementalPredictorAllows to obtain probability estimates from a ClassificationRuleLearner incrementally.
- set_fit_request(*, x: bool | None | str = '$UNCHANGED$') ClassificationRuleLearner¶
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$') ClassificationRuleLearner¶
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$') ClassificationRuleLearner¶
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$') ClassificationRuleLearner¶
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.learners.RegressionRuleLearner(feature_format: str | None, output_format: str | None, prediction_format: str | None)¶
Bases:
RuleLearner,RegressorMixin,IncrementalRegressorMixin,ABCA 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
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$') RegressionRuleLearner¶
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$') RegressionRuleLearner¶
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.learners.RuleLearner(feature_format: str | None, output_format: str | None, prediction_format: str | None)¶
Bases:
BaseEstimator,NominalFeatureSupportMixin,OrdinalFeatureSupportMixin,ABCA scikit-learn implementation of a rule learning algorithm.
- KWARG_EXAMPLE_WEIGHTS = 'sample_weights'¶
- KWARG_MAX_RULES = 'max_rules'¶
- KWARG_SPARSE_FEATURE_VALUE = 'sparse_feature_value'¶
- class NativeIncrementalPredictor(feature_matrix: RowWiseFeatureMatrix, incremental_predictor)¶
Bases:
IncrementalPredictorAllows to obtain predictions from a RuleLearner incrementally by using its native support of this functionality.
- class NonNativeIncrementalPredictor(feature_matrix: RowWiseFeatureMatrix, model: RuleModel, max_rules: int, predictor)¶
Bases:
IncrementalPredictorAllows to obtain predictions from a RuleLearner incrementally.
- class mlrl.common.learners.SparsePolicy(*values)¶
Bases:
StrEnumSpecifies 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