Using different Models
Although mother is build around catboost it basically supports other models from the ML community (like all sklearn estimators). For example, RandomForest is already supported. Furthermore, own models can be provided.
Currently supported models
Mother discovers available model wrappers from src/mother/ml/models/m_*.py.
At the moment, the built-in algorithm groups are:
catboostrandomforestlassotabpfnnodetabicl
node and tabicl (like tabpfn) are optional extras and require their dependencies to be
installed before they show up in the registry, e.g.:
You can always verify what is available in your environment:
Built-in model classes
| Algorithm key | Main model classes |
|---|---|
catboost |
CatboostRegressorMother, CatboostGaussianProcessRegressorMother, CatboostClassifierMother, CatboostRankerMother |
randomforest |
RandomForestRegressorMother, RandomForestClassifierMother |
lasso |
LassoRegressorMother, LassoClassifierMother |
tabpfn |
TabPFNRegressorMother, TabPFNClassifierMother |
node |
NODERegressor, NODEClassifier |
tabicl |
TabICLRegressorMother, TabICLClassifierMother |
node wraps Neural Oblivious Decision Ensembles (NODE), a neural network architecture for tabular
data with differentiable oblivious decision trees, optionally paired with a probabilistic "flow"
head (NodeFlow) for uncertainty-aware regression. tabicl wraps
TabICL, an in-context tabular foundation model.
Easy usage patterns
The easiest way is to ask Mother for the model class by algorithm and task type, then instantiate it.
from mother import ml
# Regressor (catboost)
reg_cls = ml.get_model_class_by_algorithm_and_type("catboost", "regression")
reg = reg_cls()
# Classifier (random forest)
clf_cls = ml.get_model_class_by_algorithm_and_type("randomForest", "classification")
clf = clf_cls()
Use an explicit subtype when needed:
from mother import ml
# Lasso multiclass classifier
lasso_multi_cls = ml.get_model_class_by_algorithm_and_type(
"lasso", "classification_multiclass"
)
lasso_multi = lasso_multi_cls()
Or retrieve all classes for one algorithm and pick one:
from mother import ml
all_catboost_models = ml.get_model_class_by_algorithm("catboost")
print([m.__name__ for m in all_catboost_models])
NODE memory usage
NODERegressor and NODEClassifier accept tree_chunk_size, defaulting to 256
trees per slice. The same limit applies to feature selection, tree routing, and
first-batch initialization. Use a smaller positive integer to reduce working
memory, or a larger value for fewer slices. Setting
None evaluates all trees together and disables the associated gradient checkpointing.
Smaller chunks can add computation overhead; this setting does not change the model's
tree count or training batch size.
from mother.ml.models.m_node import NODEClassifier, NODERegressor
regressor = NODERegressor(tree_chunk_size=64)
classifier = NODEClassifier(tree_chunk_size=None)
The setting is preserved by scikit-learn cloning and is not tuned automatically by Optuna. Each tree still selects from all input features, and initialization uses all samples in the initial batch with the same percentile draws. During training, feature selection is recomputed inside each tree slice's checkpoint to avoid retaining all selector activations. Model parameters, gradients, optimizer state, and final outputs still consume memory independently of the chunk size.
Ranking with CatboostRankerMother
CatboostRankerMother wraps CatBoost's learning-to-rank (CatBoostRanker) for tasks where the goal is to
order items within a group (e.g. rank candidates within one experiment batch) rather than predict an
isolated value per item. Every ranking row must carry a group_id identifying which group it belongs to;
ranks are only ever compared within a group, never across groups.
import numpy as np
import pandas as pd
import sklearn
from mother.ml.models.m_catboost import CatboostRankerMother
sklearn.set_config(enable_metadata_routing=True)
rng = np.random.default_rng(0)
X_features = pd.DataFrame(rng.random((12, 3)), columns=["f0", "f1", "f2"])
y = pd.Series(rng.random(12))
groups = pd.Series([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2])
ranker = CatboostRankerMother(logging_level="Silent", num_trees=200).set_fit_request(group_id="group_id")
ranker.fit(X=X_features, y=y, group_id=groups)
By default CatBoost uses the YetiRank loss. Other ranking losses (YetiRankPairwise, PairLogit,
PairLogitPairwise, QueryRMSE, QuerySoftMax) can be set via loss_function, or left to Optuna to choose
automatically during hyperparameter tuning. Two convenience parameters are folded into the loss_function
string automatically:
top: restrictsNDCG/MAP-style losses to the top-kitems per group (any mode exceptClassic).max_pairs: caps how many item pairs thePairLogitfamily samples per group, to keep training fast on large groups.
Predicting and estimating uncertainty per group
Because ranks and rank uncertainty only make sense within a group, use the module-level helpers instead of
calling predict/predict_uncertainty directly on a mixed-group X:
from mother.ml.models.m_catboost import (
ranker_predict_for_groups,
ranker_predict_uncertainty_for_groups,
)
ranks = ranker_predict_for_groups(ranker, X_features, group_id=groups, use_ranks=True)
uncertainty = ranker_predict_uncertainty_for_groups(ranker, X_features, group_id=groups, use_ranks=True)
predict_uncertainty estimates ranking confidence using CatBoost's virtual ensembles: several slightly
different versions of the trained model are compared, and how much they disagree on an item's score/rank
indicates how stable that item's ranking is. mother.ml.utils.groupwise_topk_analysis builds on this to flag
which top-k selections are unstable across groups.
See the ranking tutorial notebook for a full worked example, including groupwise uncertainty and out-of-fold validation.
Tip
If you are unsure about exact class names or capabilities, use:
Prediction and Uncertainty Interface
Starting with the 1.0.1 release line, model wrappers expose a more consistent prediction interface:
predict(...)returns aligned outputs across model backends.predict_uncertainty(...)provides uncertainty outputs for models that support it.
For regression models with uncertainty support, output columns follow a common naming pattern:
predictionuncertainty_datauncertainty_knowledgeuncertainty_total
Depending on model capabilities, one or more uncertainty columns can be present. The returned DataFrame keeps index alignment with the input rows to make downstream merging and analysis robust.
Note
mother_cv now has improved return typing and estimator-return behavior to better support workflows that inspect trained estimators after CV.
Tip
RandomForestClassifierMother
A RandomForest classifier pipeline for the MOTHER framework, integrating hyperparameter optimization via Optuna and providing default parameter management. Inherits from both scikit-learn's RandomForestClassifier and the AbstractMotherPipeline for seamless integration with the MOTHER machine learning workflow.
get_hyperparameter_space
Defines the hyperparameter search space for RandomForestClassifier using Optuna.
Args: X: Feature matrix for training data. y: Target vector for training data. trial (Trial): Optuna trial object for suggesting hyperparameters. prefix (str, optional): Prefix to add to hyperparameter names. Defaults to "".
Returns: dict: Dictionary of hyperparameter names (with prefix) and their suggested values.
default_parameters
Returns the default hyperparameters for the RandomForestClassifier.
Args: prefix (str, optional): Prefix to add to hyperparameter names. Defaults to "".
Returns: dict: Dictionary of default hyperparameter names (with prefix) and their values.
For more information on the parent class just use 'help(ml.get_model_class("RandomForestClassifierMother")'
Using Lasso with Hyperparameter Tuning
Providing your own Model
To provide your own model and make this step as easy as possible, we provide the AbstractMotherPipelineClass.
Bases: ABC
The abstract Mother pipeline is a conventional sklearn estimator / transformer etc. but adds methods for hyperparameter definition. Furthermore, it ensures for non sklearn classes and derived classes that they are compatible to the sklearn pipeline interface. This is done by implementing the get_params and set_params methods.
predict_uncertainty(X, **kwargs)
Coordinating method for uncertainty prediction. This is a simple fallback for models without a specialized predict_uncertainty implementation.
Models with specialized uncertainty estimation (e.g., CatBoost, TabPFN, RandomForest) should override this method with their own implementations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features to predict target values |
required |
**kwargs
|
Additional keyword arguments |
{}
|
Your own model just has to inherit from that class and implement the required functions that provide the hyperparameters you want to tune. For example, see the implementation of the Lasso model. Since lasso basically has one parameter to be tuned, the implementation is fairly easy.
import logging
from typing import Mapping, Optional, Union
import numpy as np
import pandas as pd
from optuna.trial import Trial
from sklearn.linear_model import (
ARDRegression,
Lasso,
LogisticRegression,
MultiTaskLasso,
)
from sklearn.utils._param_validation import StrOptions
from sklearn.utils.multiclass import type_of_target
from sklearn.utils.validation import check_is_fitted
from mother.ml import properties as props
from mother.ml.core import AbstractMotherPipeline
from mother.ml.models import utils
module_logger: logging.Logger = logging.getLogger(__name__)
class LassoRegressorMother(Lasso, AbstractMotherPipeline):
"""
MOTHER class for a LASSO regression including hyperparameter optimization.
As in the CatBoost models, the target type is not detected from the data but set explicitly with
``target_type``; in a pipeline built from settings it is taken from ``settings.model.target_type``.
A single-target model never returns several targets, and vice versa.
- ``target_type="single_target"`` (default) fits a ``Lasso`` on one target. A target with more than
one column raises a ``ValueError``.
- ``target_type="multi_target"`` fits a ``MultiTaskLasso`` on a 2-D target: one joint L1/L2 penalty
over all targets, so a feature is either used for all targets or for none.
``MultiTaskLasso`` does not support ``positive=True`` (non-negative coefficients) or ``precompute``.
If several targets need non-negative coefficients or their own feature selection, fit one
single-target model per target instead.
Uncertainty: with ``uncertainty=True`` the model is fitted as an ``ARDRegression`` (Automatic
Relevance Determination) instead of a ``Lasso``, analogous to ``data_uncertainty=True`` in
``CatboostRegressorMother``. ARD is a Bayesian sparse linear regression: every feature gets its own
prior precision, and features whose precision exceeds ``threshold_lambda`` are pruned (coefficient
0), so it selects features like Lasso, but without an ``alpha`` penalty.
``ARDRegression.predict(X, return_std=True)`` returns the standard deviation of the predictive
distribution, which already contains both uncertainty types. Its variance for a sample ``x`` is
total_variance(x) = x^T Sigma x + 1 / alpha_
- ``x^T Sigma x``: ``Sigma`` (``sigma_``) is the posterior covariance of the coefficients, so this
term is the coefficient uncertainty projected onto the sample. It differs per sample, grows for
samples far from the training data and shrinks with more training data (epistemic).
- ``1 / alpha_``: ``alpha_`` is the estimated precision of the noise, so this term is the noise
variance (aleatoric). ARD assumes homoscedastic noise, so it is identical for all samples; unlike
``RMSEWithUncertainty`` in CatBoost, it does not vary per sample, and a ranking of samples by
uncertainty therefore comes only from the knowledge part.
``predict_uncertainty`` returns these parts as standard deviations:
- ``knowledge_uncertainty``: ``sqrt(x^T Sigma x)``, computed as
``sqrt(total_uncertainty**2 - data_uncertainty**2)`` so that scikit-learn's internal centring and
feature pruning do not have to be repeated;
- ``data_uncertainty``: ``sqrt(1 / alpha_)``;
- ``total_uncertainty``: ``sqrt(knowledge_uncertainty**2 + data_uncertainty**2)``, the standard
deviation returned by ``ARDRegression.predict(X, return_std=True)``.
``ARDRegression`` only supports a single target, sample weights are not supported, and there is no
``positive`` option, so ``uncertainty=True`` requires ``target_type="single_target"``,
``positive=False`` and no ``sample_weight``. With ``uncertainty=True`` the parameters ``alpha_1``,
``alpha_2``, ``lambda_1``, ``lambda_2`` and ``threshold_lambda`` are used and tuned instead of
``alpha``; ``max_iter``, ``tol``, ``fit_intercept`` and ``copy_X`` are passed on.
Missing target values are not supported in any mode; rows with missing targets have to be removed
or imputed beforehand.
"""
_parameter_constraints: dict = {
**Lasso._parameter_constraints,
"target_type": [StrOptions({"single_target", "multi_target"})],
"uncertainty": ["boolean"],
**{
name: ARDRegression._parameter_constraints[name]
for name in ("alpha_1", "alpha_2", "lambda_1", "lambda_2", "threshold_lambda")
},
}
def __init__(
self,
alpha: float = 1.0,
*,
fit_intercept: bool = True,
precompute: bool = False,
copy_X: bool = True,
max_iter: int = 1000,
tol: float = 0.0001,
warm_start: bool = False,
positive: bool = False,
random_state: Optional[int] = None,
selection: str = "cyclic",
target_type: props.TargetType = "single_target",
uncertainty: bool = False,
alpha_1: float = 1e-6,
alpha_2: float = 1e-6,
lambda_1: float = 1e-6,
lambda_2: float = 1e-6,
threshold_lambda: float = 1e4,
) -> None:
"""
Initialize LassoRegressorMother.
Args:
positive (bool): Force non-negative coefficients. Only supported with
``target_type="single_target"`` and ``uncertainty=False``.
target_type (str): Set explicitly, it is not detected from the data.
``"single_target"`` (default) fits ``Lasso`` on one target column.
``"multi_target"`` fits ``MultiTaskLasso`` with a shared feature selection over all
targets; it requires a 2-D target, ``positive=False`` and ``uncertainty=False``.
uncertainty (bool): If ``True``, fit an ``ARDRegression`` instead of a ``Lasso`` so that
``predict_uncertainty`` returns predictive standard deviations. Only for
``target_type="single_target"``. Defaults to ``False``.
alpha_1, alpha_2 (float): Shape and rate of the Gamma prior over the noise precision of
``ARDRegression``. Only used with ``uncertainty=True``.
lambda_1, lambda_2 (float): Shape and rate of the Gamma prior over the weight precisions of
``ARDRegression``. Only used with ``uncertainty=True``.
threshold_lambda (float): Features with a weight precision above this value are pruned by
``ARDRegression``. Only used with ``uncertainty=True``.
The remaining arguments are passed to ``sklearn.linear_model.Lasso``.
"""
super().__init__(
alpha=alpha,
fit_intercept=fit_intercept,
precompute=precompute,
copy_X=copy_X,
max_iter=max_iter,
tol=tol,
warm_start=warm_start,
positive=positive,
random_state=random_state,
selection=selection,
)
self.target_type = target_type
self.uncertainty = uncertainty
self.alpha_1 = alpha_1
self.alpha_2 = alpha_2
self.lambda_1 = lambda_1
self.lambda_2 = lambda_2
self.threshold_lambda = threshold_lambda
def fit(self, X, y, sample_weight=None, check_input=True):
"""
Fit the model with ``Lasso`` (``target_type="single_target"``), ``MultiTaskLasso``
(``target_type="multi_target"``) or ``ARDRegression`` (``uncertainty=True``).
Parameters:
X: array-like
Feature matrix.
y: array-like
``"single_target"``: a target vector or a 2-D target with one column.
``"multi_target"``: a 2-D target with one column per target.
sample_weight: array-like, optional
Sample weights. Not supported with ``uncertainty=True``.
check_input: bool, optional
Passed to ``Lasso.fit`` for single-target fitting.
Returns:
self
"""
self._validate_params()
if self.target_type == "multi_target":
if self.uncertainty:
raise ValueError(
"uncertainty=True is not supported with target_type='multi_target': "
"ARDRegression only supports a single target."
)
if self.positive:
raise ValueError("positive=True is not supported with target_type='multi_target'.")
if np.ndim(y) != 2:
raise ValueError("target_type='multi_target' requires a 2-D target with one column per target.")
# Drop a previous ARD fit so predict_uncertainty cannot reuse a stale ard_.
if hasattr(self, "ard_"):
del self.ard_
return MultiTaskLasso.fit(self, X, y, sample_weight=sample_weight)
if np.ndim(y) == 2 and np.shape(y)[1] > 1:
raise ValueError(
f"target_type='single_target' got a target with {np.shape(y)[1]} columns. "
"Set target_type='multi_target' to fit several targets."
)
if not self.uncertainty:
# Drop a previous ARD fit so predict_uncertainty cannot reuse a stale ard_.
if hasattr(self, "ard_"):
del self.ard_
return super().fit(X, y, sample_weight=sample_weight, check_input=check_input)
if self.positive:
raise ValueError("positive=True is not supported with uncertainty=True.")
if sample_weight is not None:
raise ValueError("sample_weight is not supported with uncertainty=True.")
self.ard_ = ARDRegression(
max_iter=self.max_iter,
tol=self.tol,
alpha_1=self.alpha_1,
alpha_2=self.alpha_2,
lambda_1=self.lambda_1,
lambda_2=self.lambda_2,
threshold_lambda=self.threshold_lambda,
fit_intercept=self.fit_intercept,
copy_X=self.copy_X,
).fit(X, np.ravel(y))
# Lasso.predict only needs the linear coefficients, so the ARD mean prediction is reused as is.
for attribute in ("coef_", "intercept_", "n_features_in_", "feature_names_in_", "n_iter_"):
if hasattr(self.ard_, attribute):
setattr(self, attribute, getattr(self.ard_, attribute))
return self
def predict_uncertainty(self, X, uncertainty_for_opt: bool = False, **kwargs):
"""
Predict targets with uncertainty.
With ``uncertainty=True`` the uncertainty comes from ``ARDRegression`` (standard deviations,
see the class docstring). With ``uncertainty=False`` the generic fallback is used, which only
returns predictions.
Parameters:
X: array-like
Feature matrix.
uncertainty_for_opt: bool, optional
If ``True``, return only the ``total_uncertainty`` column.
**kwargs:
Passed to the generic fallback when ``uncertainty=False``.
Returns:
pd.DataFrame: Columns ``pred``, ``mean_predictions``, ``knowledge_uncertainty``,
``data_uncertainty`` and ``total_uncertainty``.
"""
if not self.uncertainty:
return super().predict_uncertainty(X, **kwargs)
check_is_fitted(self, "ard_")
pred, total = self.ard_.predict(X, return_std=True)
data = np.full_like(total, np.sqrt(1.0 / self.ard_.alpha_))
knowledge = np.sqrt(np.clip(total**2 - data**2, 0.0, None))
index = X.index if isinstance(X, pd.DataFrame) else None
result = pd.DataFrame(
{
"pred": pred,
"mean_predictions": pred,
"knowledge_uncertainty": knowledge,
"data_uncertainty": data,
"total_uncertainty": total,
},
index=index,
)
if uncertainty_for_opt:
return result.loc[:, ["total_uncertainty"]]
return result
def get_hyperparameter_space(self, X, y, trial: Trial, prefix: str = "") -> dict:
"""
Define the hyperparameter search space: ``alpha`` for Lasso and MultiTaskLasso, the Gamma
priors and ``threshold_lambda`` for ARD (``uncertainty=True``).
Parameters:
X: array-like
Feature matrix.
y: array-like
Target vector.
trial: optuna.trial.Trial
Optuna trial object for suggesting hyperparameters.
prefix: str, optional
Prefix to add to hyperparameter names.
Returns:
dict: Dictionary containing hyperparameter names and their suggested values.
"""
if not self.uncertainty:
space = {"alpha": trial.suggest_float(prefix + "alpha", 1e-6, 1e1, log=True)}
else:
space = {
name: trial.suggest_float(prefix + name, 1e-9, 1e-3, log=True)
for name in ("alpha_1", "alpha_2", "lambda_1", "lambda_2")
}
space["threshold_lambda"] = trial.suggest_float(prefix + "threshold_lambda", 1e2, 1e6, log=True)
return utils.add_prefix_to_dict_keys(space, prefix=prefix)
def default_parameters(self, prefix: str = "") -> dict:
"""
Return the default hyperparameters: ``alpha=1e-3`` for Lasso and MultiTaskLasso, the
scikit-learn defaults of ``ARDRegression`` for ``uncertainty=True``.
Parameters:
prefix: str, optional
Prefix to add to hyperparameter names.
Returns:
dict: Dictionary containing default hyperparameter values.
"""
if not self.uncertainty:
return utils.add_prefix_to_dict_keys({"alpha": 1e-3}, prefix=prefix)
return utils.add_prefix_to_dict_keys(
{"alpha_1": 1e-6, "alpha_2": 1e-6, "lambda_1": 1e-6, "lambda_2": 1e-6, "threshold_lambda": 1e4},
prefix=prefix,
)
def set_params(self, **params):
"""
Set the parameters of this estimator.
The parameters are those of ``LassoRegressorMother`` itself. Which class performs
the fit — ``Lasso``, ``MultiTaskLasso`` or ``ARDRegression`` — does not change the
parameter set, and the values given here are the ones the next ``fit`` passes to
that class.
Parameters:
**params: Parameter names and values. Names are the constructor arguments,
including ``target_type``, ``uncertainty`` and the ARD priors.
"""
return super().set_params(**params)
def get_params(self, deep=True) -> dict:
"""
Return the parameters of this estimator.
The result always contains the constructor parameters, so a fitted model still
reports how it was configured. ``uncertainty`` and ``target_type`` say which
class the next ``fit`` uses. When ``uncertainty`` is ``True`` the ARD priors are
part of the same dictionary: ``alpha_1``, ``alpha_2``, ``lambda_1``, ``lambda_2``
and ``threshold_lambda``. Those are the values passed to ``ARDRegression``.
The fitted ``ard_`` object is not one of these parameters. ``clone`` copies only
this dictionary, so the clone is unfitted; fitting it again uses ``uncertainty``
and the ARD priors, or the Lasso parameters, exactly as the original did. Pickling
the estimator keeps ``ard_`` itself, so loading the model restores the fitted ARD
regression.
"""
params = super().get_params(deep=deep)
for name in (
"target_type",
"uncertainty",
"alpha_1",
"alpha_2",
"lambda_1",
"lambda_2",
"threshold_lambda",
):
params[name] = getattr(self, name)
return params
class LassoClassifierMother(LogisticRegression, AbstractMotherPipeline):
"""
MOTHER class for a LASSO (L1-regularised logistic regression) classification of binary and
multiclass targets, including hyperparameter optimization.
As in the other MOTHER models, the target type is not detected from the data but set explicitly
with ``target_type``; in a pipeline built from settings it is taken from
``settings.model.target_type``. ``LogisticRegression`` fits one target column (binary or
multiclass), so only ``target_type="single_target"`` is supported. A multi-target setting or a
target with more than one column raises a ``ValueError``.
With ``solver="auto"`` (default) the solver is chosen at fit time: ``liblinear`` for binary targets
and ``saga`` for multiclass targets, the only scikit-learn solver that supports pure L1 for
multinomial problems. The chosen solver is stored in ``solver_``; the ``solver`` parameter keeps
the value passed by the user. An explicitly passed solver is used as given.
``saga`` converges much better on standardised features, so scale the input features (for example
with a ``StandardScaler`` in the pipeline) for multiclass targets.
"""
_parameter_constraints: dict = {
**LogisticRegression._parameter_constraints,
"solver": [StrOptions({"auto"} | LogisticRegression._parameter_constraints["solver"][0].options)],
"model_type": [StrOptions({"classification_binary", "classification_multiclass"})],
"target_type": [StrOptions({"single_target"})],
}
def __init__(
self,
*,
dual: bool = False,
tol: float = 0.0001,
C: float = 1,
fit_intercept: bool = True,
intercept_scaling: float = 1,
class_weight: Optional[Union[Mapping, str]] = "balanced",
random_state: int = 42,
solver: str = "auto",
max_iter: int = 3000,
verbose: int = 0,
warm_start: bool = False,
n_jobs: Optional[int] = None,
model_type: props.ModelType = "classification_binary",
target_type: props.TargetType = "single_target",
) -> None:
"""
Initialize LassoClassifierMother.
Args:
solver (str): ``"auto"`` or a ``LogisticRegression`` solver that supports pure L1
(``liblinear`` for binary targets, ``saga`` for binary and multiclass targets).
model_type (str): ``"classification_binary"`` or ``"classification_multiclass"``. Kept for
consistency with ``CatboostClassifierMother``; the solver is chosen from the target,
not from ``model_type``.
target_type (str): Set explicitly, it is not detected from the data. Only
``"single_target"`` is supported; multi-target classification is not.
The remaining arguments are passed to ``sklearn.linear_model.LogisticRegression``.
"""
# l1_ratio=1: pure L1 regularisation via the sklearn ≥1.8 API.
# penalty='l1' was deprecated in sklearn 1.8 and will be removed in 1.10;
# l1_ratio=1 is the replacement (analogous to LogisticRegressionCV).
super().__init__(
l1_ratio=1,
dual=dual,
tol=tol,
C=C,
fit_intercept=fit_intercept,
intercept_scaling=intercept_scaling,
class_weight=class_weight,
random_state=random_state,
solver=solver, # type: ignore
max_iter=max_iter,
verbose=verbose,
warm_start=warm_start,
n_jobs=n_jobs,
)
self.model_type = model_type
self.target_type = target_type
@property
def target_type(self) -> props.TargetType:
"""Only ``"single_target"`` is supported; assignment validates immediately."""
return self._target_type
@target_type.setter
def target_type(self, value: props.TargetType) -> None:
if value != "single_target":
raise ValueError(
"target_type for LassoClassifierMother must be 'single_target'. "
"Multi-target classification is not supported."
)
self._target_type = value
def fit(self, X, y, sample_weight=None):
"""
Fit the model. ``solver="auto"`` resolves to ``liblinear`` for binary and ``saga`` for
multiclass targets.
Parameters:
X: array-like
Feature matrix.
y: array-like
A single target vector, or a 2-D target with one column.
sample_weight: array-like, optional
Sample weights.
Returns:
self
"""
# Re-check via the property so a bypassed ``_target_type`` still fails at fit time.
if self.target_type != "single_target":
raise ValueError(
"target_type for LassoClassifierMother must be 'single_target'. "
"Multi-target classification is not supported."
)
self._validate_params()
if np.ndim(y) == 2 and np.shape(y)[1] > 1:
raise ValueError(
f"target_type='single_target' got a target with {np.shape(y)[1]} columns. "
"LassoClassifierMother does not support multi-target classification."
)
solver = self.solver
if solver == "auto":
solver = "saga" if type_of_target(y, input_name="y") == "multiclass" else "liblinear"
self.solver_ = solver
# LogisticRegression.fit reads self.solver and does not accept "auto".
user_solver, self.solver = self.solver, solver
try:
super().fit(X, y, sample_weight=sample_weight)
finally:
self.solver = user_solver
return self
def get_hyperparameter_space(self, X, y, trial: Trial, prefix: str = "") -> dict:
"""
Define the hyperparameter search space for Lasso classification.
Parameters:
X: array-like
Feature matrix.
y: array-like
Target vector.
trial: optuna.trial.Trial
Optuna trial object for suggesting hyperparameters.
prefix: str, optional
Prefix to add to hyperparameter names.
Returns:
dict: Dictionary containing hyperparameter names and their suggested values.
"""
return utils.add_prefix_to_dict_keys(
{
"C": trial.suggest_float(prefix + "C", 1e-6, 1e1, log=True),
},
prefix=prefix,
)
def default_parameters(self, prefix: str = "") -> dict:
"""
Return the default hyperparameters for the Lasso model.
Parameters:
prefix: str, optional
Prefix to add to hyperparameter names.
Returns:
dict: Dictionary containing default hyperparameter values.
"""
return utils.add_prefix_to_dict_keys({"C": 1e0}, prefix=prefix)
def set_params(self, **params):
"""
Set the parameters of this estimator.
The parameters are those of ``LassoClassifierMother`` itself. The logistic
regression fitted underneath does not change the parameter set. ``target_type``
stays ``"single_target"``; a multi-target value raises ``ValueError`` and leaves
the estimator unchanged.
Parameters:
**params: Parameter names and values. Names are the constructor arguments,
including ``model_type`` and ``target_type``.
"""
if "target_type" in params and params["target_type"] != "single_target":
raise ValueError(
"target_type for LassoClassifierMother must be 'single_target'. "
"Multi-target classification is not supported."
)
return super().set_params(**params)
def get_params(self, deep=True) -> dict:
"""
Return the parameters of this estimator.
The result is the constructor parameters of ``LassoClassifierMother``, including
``model_type`` and ``target_type``. It does not change after a binary or
multiclass fit, and the solver stored for that fit (``solver_``) is not part of
it: ``solver`` keeps the value passed by the user.
"""
return super().get_params(deep=deep)
Registering your model using MotherModelRegistry
To register your own model and use it easily within the mother framework you can register your model with the available decorator.
MotherModelRegistry
Singleton registry for dynamically discovering and managing model classes in the 'mother.ml.models' package.
This class scans the models directory for Python files matching the pattern 'm_*.py', imports them, and registers all classes that inherit from AbstractMotherPipeline. It provides mappings for model class names, lower-case lookups, and a list of supported algorithms. The registry is used to facilitate model discovery, retrieval, and algorithm support checks throughout the mother.ml package.
Attributes:
| Name | Type | Description |
|---|---|---|
models_dir |
Path
|
Path to the directory containing model modules. |
model_classes |
dict
|
Mapping of model class names to their class objects. |
model_classes_lower |
dict
|
Mapping of lower-case model class names to their canonical names. |
supported_algorithms |
list
|
List of supported algorithm names discovered from model files. |
list_registered_models()
List all registered models with their algorithms.
register_model(model_class, algorithm=None)
Register a model class manually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
Type[AbstractMotherPipeline]
|
The model class to register |
required |
algorithm
|
Optional[str]
|
Optional algorithm name. If not provided, will be derived from class name |
None
|
unregister_model(model_class_name)
Unregister a model class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class_name
|
str
|
Name of the model class to unregister |
required |
algo_is_supported(algorithm)
describe_model(name)
get_available_algorithms()
get_model_class(name)
Get a model class by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the model class |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Type |
type[AbstractMotherPipeline]
|
The model class |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the model class is not found |
get_model_class_by_algorithm(algorithm)
Get model classes by algorithm name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
Name of the algorithm |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Type |
list[Type[AbstractMotherPipeline]]
|
The model class |
get_model_class_by_algorithm_and_type(algorithm, model_type)
Returns the appropriate model class based on the algorithm and model type.
get_supported_models()
See the following example how to implement your custom RandomForest Classifier.
Example
from mother import ml
from sklearn.ensemble import RandomForestClassifier
@ml.register_model("custom_rf")
class CustomRandomForestMother(RandomForestClassifier, ml.AbstractMotherPipeline):
def get_hyperparameter_space(self, X, y, trial, prefix=""):
return {
f"{prefix}n_estimators": trial.suggest_int("n_estimators", 10, 100),
f"{prefix}max_depth": trial.suggest_int("max_depth", 3, 10),
}
def default_parameters(self, prefix=""):
return {f"{prefix}n_estimators": 50, f"{prefix}max_depth": 5}
print(ml.get_model_class_by_algorithm("custom_rf"))