Models

QSAR regressors and classifiers behind one scikit-learn-compatible facade, plus the algorithms that are specifically chemometric: PLS with VIP scores, a Tanimoto-kernel Gaussian process, and consensus ensembles.

The facades

QSARRegressor and QSARClassifier dispatch on a name, so comparing backends is a one-word change:

>>> from qsarkit.models import QSARRegressor
>>> X, y = demo_fingerprints(256), DEMO_Y
>>> for name in ("rf", "svm", "pls", "ridge"):
...     model = QSARRegressor(name, random_state=0).fit(X, y)
...     print(f"{name:6} {model.score(X, y):.3f}")
rf     0.952
svm    0.880
pls    0.900
ridge  0.944

Warning

Those are training scores, shown only to demonstrate the interface. A model scored on its own training data tells you nothing about whether it generalizes, and on 24 compounds with 256 features every one of these will look good. Compare backends on a held-out set or by cross-validation — see Validation.

Any estimator, not just the menu

name also accepts anything following the scikit-learn fit/ predict protocol — XGBoost, LightGBM, CatBoost, or your own wrapper — as an instance or a class:

>>> from sklearn.linear_model import Ridge
>>> model = QSARRegressor(Ridge(alpha=2.0)).fit(X, y)
>>> type(model.estimator_).__name__, model.estimator_.alpha
('Ridge', 2.0)

The instance you pass is cloned, never mutated, so one configured template can seed several models:

>>> template = Ridge(alpha=1.0)
>>> _ = QSARRegressor(template).fit(X, y)
>>> hasattr(template, "coef_")
False

Passing a class lets the facade build it, which is what model_args and model_params are for:

>>> model = QSARRegressor(Ridge, model_params={"alpha": 5.0}).fit(X, y)
>>> model.estimator_.alpha
5.0

fit_params, predict_params and predict_proba_params reach arguments that belong to the call rather than the constructor — XGBoost’s eval_set, LightGBM’s callbacks, CatBoost’s verbose, or a plain sample_weight:

>>> import numpy as np
>>> weights = np.linspace(0.5, 1.5, len(y))
>>> model = QSARRegressor(Ridge, fit_params={"sample_weight": weights}).fit(X, y)
>>> model.predict(X).shape
(24,)

Because the facade is a real estimator, a custom backend still composes with the rest of scikit-learn:

>>> from sklearn.base import is_regressor
>>> from sklearn.model_selection import GridSearchCV
>>> is_regressor(QSARRegressor(Ridge))
True
>>> search = GridSearchCV(
...     QSARRegressor(Ridge),
...     {"model_params": [{"alpha": 0.1}, {"alpha": 10.0}]},
...     cv=3,
... ).fit(X, y)
>>> sorted(search.best_params_["model_params"])
['alpha']

PLS and VIP

Partial least squares is the chemometric workhorse: it handles more descriptors than compounds, which is the normal QSAR situation and the regime where ordinary regression fails.

>>> from qsarkit.models import PLSRegressor
>>> pls = PLSRegressor(n_components=3).fit(X, y)
>>> pls.predict(X).shape
(24,)
>>> pls.vip_scores_.shape
(256,)

VIP scores above 1 mark the variables carrying the fit — the standard threshold for descriptor selection in a PLS model:

>>> int((pls.vip_scores_ > 1.0).sum())
58

Gaussian processes with uncertainty

>>> from qsarkit.models import GaussianProcessQSAR
>>> gp = GaussianProcessQSAR(random_state=0).fit(X, y)
>>> mean, std = gp.predict(X, return_std=True)
>>> mean.shape, std.shape
((24,), (24,))

The kernel is chosen from the data: Tanimoto for non-negative fingerprint-like input, RBF otherwise. A Tanimoto kernel is not positive semi-definite on signed descriptors, so hardcoding it would produce a model that silently fails to converge.

Consensus

>>> from qsarkit.models import ConsensusModel
>>> ensemble = ConsensusModel([
...     ("rf", QSARRegressor("rf", random_state=0)),
...     ("ridge", QSARRegressor("ridge")),
... ]).fit(X, y)
>>> ensemble.predict(X).shape
(24,)

API

QSAR modeling estimators: kernels, regressors, classifiers and ensembling.

This subpackage provides scikit-learn-compatible estimators tailored to QSAR/QSPR modeling on molecular fingerprints and descriptors: a Tanimoto kernel for Gaussian processes, thin QSAR-sane wrappers around common scikit-learn regressors, a PLS regressor with VIP variable-importance scores, a naive-baseline model for OECD-principle-4 comparisons, a consensus/ensembling dispatcher, and unified name-dispatching facades (QSARRegressor, QSARClassifier).

class qsarkit.models.QSARRegressor(name, random_state=None, model_params=None, model_args=None, fit_params=None, predict_params=None)[source]

Bases: _CustomEstimatorMixin, RegressorMixin, BaseEstimator

Unified, name-dispatching QSAR regressor facade.

Wraps a broad menu of regression algorithms behind one scikit-learn estimator, so pipelines and benchmarking code can sweep across algorithm families by changing a single string rather than importing and configuring each estimator by hand.

Parameters:
  • name (Union[Literal['rf', 'svm', 'gbm', 'xgboost', 'lightgbm', 'knn', 'pls', 'ridge', 'lasso', 'elasticnet', 'mlp', 'gp'], Any]) –

    Which algorithm to build. One-line rationale for each default:

    • "rf": RandomForestQSAR — robust, scale-insensitive default for tabular molecular descriptors (Svetnik et al. 2003).

    • "svm": SVMQSAR — RBF support-vector regression, strong on non-linear SAR with few hundred compounds (Burbidge et al. 2001).

    • "gbm": HistGradientBoostingRegressor — fast, regularized gradient boosting with no extra install required.

    • "xgboost": xgboost.XGBRegressor if installed, otherwise the "gbm" fallback with a warning (Chen & Guestrin 2016).

    • "lightgbm": lightgbm.LGBMRegressor if installed, otherwise the "gbm" fallback with a warning (Ke et al. 2017).

    • "knn": JaccardKNeighborsRegressor — similarity-based read-across on fingerprints.

    • "pls": PLSRegressor — handles more descriptors than compounds via latent-variable projection (Wold et al. 2001).

    • "ridge": Ridge — L2-regularized linear baseline (Hoerl & Kennard 1970).

    • "lasso": Lasso — L1-regularized, sparse linear model for descriptor selection (Tibshirani 1996).

    • "elasticnet": ElasticNet — L1/L2 compromise, robust to correlated descriptors (Zou & Hastie 2005).

    • "mlp": NeuralNetworkQSAR — non-linear feed-forward network (Winkler 2004).

    • "gp": GaussianProcessQSAR — Tanimoto- kernel Gaussian process with predictive uncertainty (Ralaivola et al. 2005).

  • random_state (Optional[int]) – Seed forwarded to the underlying estimator, where applicable. Ignored when name is an estimator instance, which is used as configured.

  • model_params (Optional[Dict[str, Any]]) – Keyword arguments for the underlying estimator’s constructor, overriding any default (e.g. model_params={"n_estimators": 200} for name="rf"). When name is an instance, these are applied with set_params.

  • model_args (Optional[Sequence[Any]]) – Positional arguments for the constructor. Only meaningful when name is an estimator class; passing them alongside an instance raises, since the instance is already built.

  • fit_params (Optional[Dict[str, Any]]) – Extra keyword arguments passed to the estimator’s fit. Some libraries only accept certain options there rather than in the constructor – XGBoost’s eval_set, LightGBM’s callbacks, CatBoost’s verbose, or a sample_weight array.

  • predict_params (Optional[Dict[str, Any]]) – Extra keyword arguments passed to the estimator’s predict.

Variables:

estimator (BaseEstimator) – The fitted underlying estimator.

Examples

>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=40, n_features=5, random_state=0)
>>> model = QSARRegressor(name="rf", random_state=0).fit(X, y)
>>> model.predict(X).shape
(40,)

name also accepts any object following the scikit-learn API, so the facade is not limited to the built-in menu. An instance is used as configured:

>>> from sklearn.linear_model import Ridge
>>> model = QSARRegressor(Ridge(alpha=2.0)).fit(X, y)
>>> type(model.estimator_).__name__, model.estimator_.alpha
('Ridge', 2.0)

The instance you pass is cloned, never mutated, so one configured template can seed several models:

>>> template = Ridge(alpha=1.0)
>>> _ = QSARRegressor(template).fit(X, y)
>>> hasattr(template, "coef_")       # still unfitted
False

A class is constructed from model_args/model_params:

>>> model = QSARRegressor(Ridge, model_params={"alpha": 5.0}).fit(X, y)
>>> model.estimator_.alpha
5.0

fit_params reaches arguments the constructor does not take – the same mechanism serves sample_weight here and eval_set for XGBoost or CatBoost:

>>> import numpy as np
>>> weights = np.linspace(0.5, 1.5, len(y))
>>> model = QSARRegressor(Ridge, fit_params={"sample_weight": weights}).fit(X, y)
>>> model.predict(X).shape
(40,)

References

estimator_: BaseEstimator
fit(X, y)[source]

Build (from name) and fit the underlying regressor.

Parameters:
Returns:

The fitted estimator.

Return type:

QSARRegressor

Raises:

ValueError – If name is not a recognized algorithm name.

predict(X)[source]

Predict by delegating to the fitted underlying regressor.

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Return type:

ndarray[tuple[Any, ...], dtype[double]]

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.QSARClassifier(name, random_state=None, model_params=None, model_args=None, fit_params=None, predict_params=None, predict_proba_params=None)[source]

Bases: _CustomEstimatorMixin, ClassifierMixin, BaseEstimator

Unified, name-dispatching QSAR classifier facade.

The classification counterpart of QSARRegressor: wraps a broad menu of classification algorithms behind one scikit-learn estimator, selected by a single name string.

Parameters:
  • name (Union[Literal['rf', 'svm', 'gbm', 'xgboost', 'lightgbm', 'knn', 'pls', 'ridge', 'lasso', 'elasticnet', 'mlp', 'gp'], Any]) –

    Which algorithm to build. One-line rationale for each default:

    • "rf": RandomForestClassifier — robust default ensemble for tabular fingerprints/descriptors (Svetnik et al. 2003).

    • "svm": SVC (RBF, probability=True) — strong non-linear classifier on molecular fingerprints (Cortes & Vapnik 1995; Burbidge et al. 2001).

    • "gbm": HistGradientBoostingClassifier — fast, regularized gradient boosting, no extra install.

    • "xgboost": xgboost.XGBClassifier if installed, otherwise the "gbm" fallback with a warning (Chen & Guestrin 2016).

    • "lightgbm": lightgbm.LGBMClassifier if installed, otherwise the "gbm" fallback with a warning (Ke et al. 2017).

    • "knn": JaccardKNeighborsClassifier — similarity-based read-across on fingerprints.

    • "pls": an internal binary PLS-DA wrapper around PLSRegressor (Barker & Rayens 2003).

    • "ridge": RidgeClassifier — fast linear baseline; has no native predict_proba (calling it raises AttributeError).

    • "lasso": LogisticRegression(penalty="l1", solver="liblinear") — sparse linear classifier (Tibshirani 1996).

    • "elasticnet": LogisticRegression(penalty="elasticnet", solver="saga") — L1/L2 compromise (Zou & Hastie 2005).

    • "mlp": MLPClassifier — non-linear feed-forward network (Winkler 2004).

    • "gp": GaussianProcessClassifier with a TanimotoKernel — probabilistic fingerprint-similarity classifier (Ralaivola et al. 2005).

  • random_state (Optional[int]) – Seed forwarded to the underlying estimator, where applicable. Ignored when name is an estimator instance, which is used as configured.

  • model_params (Optional[Dict[str, Any]]) – Keyword arguments for the underlying estimator’s constructor, overriding any default (e.g. model_params={"n_estimators": 200} for name="rf"). When name is an instance, these are applied with set_params.

  • model_args (Optional[Sequence[Any]]) – Positional arguments for the constructor. Only meaningful when name is an estimator class; passing them alongside an instance raises, since the instance is already built.

  • fit_params (Optional[Dict[str, Any]]) – Extra keyword arguments passed to the estimator’s fit. Some libraries only accept certain options there rather than in the constructor – XGBoost’s eval_set, LightGBM’s callbacks, CatBoost’s verbose, or a sample_weight array.

  • predict_params (Optional[Dict[str, Any]]) – Extra keyword arguments passed to the estimator’s predict.

  • predict_proba_params (Optional[Dict[str, Any]]) – Extra keyword arguments passed to the estimator’s predict_proba.

Variables:
  • estimator (BaseEstimator) – The fitted underlying scikit-learn (or xgboost/lightgbm) estimator.

  • classes (ndarray) – Class labels, taken from the fitted underlying estimator.

Examples

>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=40, n_features=5, random_state=0)
>>> model = QSARClassifier(name="rf", random_state=0).fit(X, y)
>>> model.predict(X).shape
(40,)

As for QSARRegressor, name accepts any estimator following the scikit-learn API – CatBoost, XGBoost, LightGBM or your own – as a class or an instance:

>>> from sklearn.tree import DecisionTreeClassifier
>>> model = QSARClassifier(
...     DecisionTreeClassifier, model_params={"max_depth": 3}, random_state=0
... ).fit(X, y)
>>> type(model.estimator_).__name__
'DecisionTreeClassifier'
>>> model.predict_proba(X).shape
(40, 2)

classes_ is taken from the estimator when it exposes one, and otherwise from the labels seen during fit, so a wrapper that omits the attribute still works:

>>> model.classes_.tolist()
[0, 1]

References

estimator_: BaseEstimator
classes_: ndarray[tuple[Any, ...], dtype[generic]]
fit(X, y)[source]

Build (from name) and fit the underlying classifier.

Parameters:
Returns:

The fitted estimator.

Return type:

QSARClassifier

Raises:

ValueError – If name is not a recognized algorithm name.

predict(X)[source]

Predict by delegating to the fitted underlying classifier.

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Return type:

ndarray[tuple[Any, ...], dtype[generic]]

predict_proba(X)[source]

Class probabilities, by delegating to the fitted classifier.

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Raises:
  • ModelNotFittedError – If called before fit().

  • AttributeError – If the underlying estimator has no predict_proba (e.g. name="ridge", whose RidgeClassifier backend has no native probability estimates).

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.PLSRegressor(n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)[source]

Bases: PLSRegression

PLS regressor that additionally reports VIP variable-importance scores.

A thin subclass of sklearn.cross_decomposition.PLSRegression that, on top of the usual PLS fit, computes the Variable Importance in Projection (VIP) score for every input feature. PLS is a workhorse QSAR technique — it handles the many-correlated-descriptors, few-compounds regime that ordinary least squares cannot, by projecting onto a small number of latent variables that jointly explain X and y — but the latent-variable loadings it produces are not directly interpretable per descriptor. VIP scores solve that by aggregating each descriptor’s contribution across all retained components, weighted by how much y-variance each component explains, giving a single per-descriptor importance number comparable to a feature-importance ranking. A VIP score around 1 or above is the conventional threshold for “important” in chemometrics, since the average of all squared VIP scores is exactly 1 by construction.

Parameters:
  • n_components (int) – Number of PLS components (latent variables) to fit.

  • scale (bool) – Whether to standardize (z-score) X and y before fitting.

  • max_iter (int) – Maximum number of iterations of the NIPALS inner loop.

  • tol (float) – Convergence tolerance for the NIPALS inner loop.

  • copy (bool) – Whether to copy X and y in fit() before scaling.

Variables:

vip_scores (ndarray of shape (n_features,)) – Variable Importance in Projection score for each input feature, computed after fit(). Non-negative; the mean of their squares is 1.0 by construction.

Examples

>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=60, n_features=5, n_informative=2, random_state=0)
>>> model = PLSRegressor(n_components=2).fit(X, y)
>>> model.vip_scores_.shape
(5,)
>>> bool((model.vip_scores_ >= 0).all())
True

References

  • Wold, S., Sjostrom, M. & Eriksson, L. (2001). “PLS-regression: a basic tool of chemometrics.” Chemometrics and Intelligent Laboratory Systems, 58(2), 109-130. https://doi.org/10.1016/S0169-7439(01)00155-1

  • Wold, S., Ruhe, A., Wold, H. & Dunn, W. J. (1984). “The Collinearity Problem in Linear Regression. The Partial Least Squares (PLS) Approach to Generalized Inverses.” SIAM J. Sci. Stat. Comput., 5(3), 735-743. https://doi.org/10.1137/0905052

vip_scores_: ndarray[tuple[Any, ...], dtype[float64]]
fit(X, y)[source]

Fit the PLS model and compute VIP scores.

Parameters:
Returns:

The fitted estimator, with vip_scores_ populated.

Return type:

PLSRegressor

set_predict_request(*, copy='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the predict method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

copy (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for copy parameter in predict.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

set_transform_request(*, copy='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the transform method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform 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 transform.

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

Parameters:

copy (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for copy parameter in transform.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.ConsensusModel(estimators, task='regression', method='averaging', weights=None, final_estimator=None, cv=5)[source]

Bases: BaseEstimator

Consensus QSAR model built from several member estimators.

Consensus (ensemble) modeling is one of the most robust, widely reproduced findings in QSAR practice: averaging or stacking several structurally different models (a random forest, an SVM, a PLS model, …) routinely outperforms any single member, because the members’ errors are only partially correlated and combining them cancels out some of each model’s idiosyncratic mistakes. Rather than reimplementing ensembling machinery, this class is a thin, task-aware dispatcher onto scikit-learn’s own VotingRegressor / VotingClassifier (method="averaging") and StackingRegressor / StackingClassifier (method="stacking").

Parameters:
  • estimators (Sequence[Tuple[str, BaseEstimator]]) – Named member estimators, in the format scikit-learn’s own voting/stacking ensembles expect.

  • task (Literal['regression', 'classification']) – Prediction task, selecting which family of scikit-learn ensemble is built.

  • method (Literal['averaging', 'stacking']) – "averaging" combines member predictions by a (possibly weighted) vote/mean; "stacking" trains a meta-estimator on out-of-fold member predictions.

  • weights (Optional[Sequence[float]]) – Per-member weights used only when method="averaging".

  • final_estimator (Optional[BaseEstimator]) – Meta-estimator used only when method="stacking". Defaults to the stacking ensemble’s own default (ridge/logistic regression) when None.

  • cv (int) – Number of cross-validation folds used to generate the out-of-fold predictions that train the meta-estimator, when method="stacking".

Variables:

estimator (estimator) – The fitted internal scikit-learn voting/stacking ensemble.

Examples

>>> from sklearn.linear_model import LinearRegression, Ridge
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=40, n_features=4, random_state=0)
>>> model = ConsensusModel(
...     estimators=[("lr", LinearRegression()), ("ridge", Ridge())],
...     task="regression",
...     method="averaging",
... ).fit(X, y)
>>> model.predict(X).shape
(40,)

References

estimator_: BaseEstimator
fit(X, y)[source]

Build and fit the internal voting/stacking ensemble.

Parameters:
Returns:

The fitted estimator.

Return type:

ConsensusModel

Raises:

ValueError – If task/method are not among the allowed literals, or estimators is empty.

predict(X)[source]

Predict by delegating to the fitted internal ensemble.

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Return type:

ndarray[tuple[Any, ...], dtype[generic]]

score(X, y, sample_weight=None)[source]

Score the consensus: R^2 for regression, accuracy for classification.

Implemented explicitly rather than inherited, because this estimator switches task at construction time and so cannot carry either RegressorMixin or ClassifierMixin.

Parameters:
Return type:

float

predict_proba(X)[source]

Class probabilities by delegating to the fitted internal ensemble.

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Raises:
  • ModelNotFittedError – If called before fit().

  • AttributeError – If the internal ensemble does not support predict_proba (e.g. stacking classification with a final_estimator that itself has no predict_proba).

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.TanimotoKernel(sigma_0=1.0, sigma_0_bounds=(1e-05, 100000.0))[source]

Bases: Kernel

Tanimoto (Jaccard) kernel for fingerprint-valued Gaussian processes.

Implements k(x, y) = sigma_0^2 * T(x, y), where T is the Tanimoto coefficient |x AND y| / |x OR y| computed by qsarkit.neighbors.tanimoto_similarity_matrix(). Standard Gaussian-process kernels (RBF, dot-product, Matern, …) measure similarity through a Euclidean or dot-product geometry, which is the wrong geometry for sparse binary fingerprints: two fingerprints that share no bits but both have many zeros still look “close” in Euclidean space, even though they encode structurally unrelated molecules. The Tanimoto kernel instead puts the GP prior directly on the similarity measure chemists already use for virtual screening and read-across, so the resulting posterior mean and (crucially for QSAR) posterior standard deviation are calibrated to fingerprint chemistry rather than to an arbitrary embedding geometry.

k(x, y) = sigma_0^2 * T(x, y) is a valid (positive semi-definite) kernel because the Tanimoto coefficient itself is known to be conditionally positive definite over binary vectors (Ralaivola et al. 2005, building on Gower’s 1971 result for similarity coefficients), and scaling a PSD kernel by a positive constant preserves PSD-ness.

Parameters:
  • sigma_0 (float) – Output-scale hyperparameter. Squared to give the kernel’s amplitude, i.e. k(x, x) = sigma_0**2 for a binary fingerprint x (self-similarity is always 1 under Tanimoto).

  • sigma_0_bounds (Union[Tuple[float, float], str]) – Lower and upper bound on sigma_0 used by the GP’s hyperparameter optimizer, or the string "fixed" to keep sigma_0 constant during fitting (as with any scikit-learn kernel hyperparameter).

Examples

>>> import numpy as np
>>> X = np.array([[1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1]], dtype=float)
>>> kernel = TanimotoKernel(sigma_0=2.0)
>>> K = kernel(X)
>>> K.shape
(3, 3)
>>> bool(np.allclose(np.diag(K), 4.0))
True

References

property hyperparameter_sigma_0: Hyperparameter

The sigma_0 hyperparameter descriptor scikit-learn introspects.

diag(X)[source]

Return the diagonal of the kernel matrix k(X, X).

Cheaper than computing the full matrix and extracting the diagonal, since every diagonal entry is simply sigma_0**2 (Tanimoto self-similarity is always 1).

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Return type:

ndarray[tuple[Any, ...], dtype[double]]

is_stationary()[source]

Whether the kernel is stationary (depends only on x - y).

Returns:

Always False: the Tanimoto coefficient depends on the sets of “on” bits themselves, not merely on their difference.

Return type:

bool

class qsarkit.models.GaussianProcessQSAR(kernel=None, *, alpha=1e-06, optimizer='fmin_l_bfgs_b', n_restarts_optimizer=0, normalize_y=True, copy_X_train=True, n_targets=None, random_state=None)[source]

Bases: GaussianProcessRegressor

Gaussian-process regressor defaulting to a Tanimoto fingerprint kernel.

A thin subclass of sklearn.gaussian_process.GaussianProcessRegressor whose only behavioural change is the default kernel: when kernel=None (the constructor default, exactly as in the parent class), fit() builds a TanimotoKernel rather than scikit-learn’s own RBF default — mirroring the parent class’s own convention of resolving kernel=None to a concrete kernel lazily inside fit(), so __init__ keeps storing exactly what was passed to it (sklearn’s get_params/clone contract).

The QSAR motivation for a Gaussian process at all is uncertainty quantification: unlike a point-estimate regressor, a fitted GP returns both a predictive mean and a predictive standard deviation (predict(X, return_std=True)), and that per-compound standard deviation is a natural, model-native applicability-domain signal — it grows exactly where the model is extrapolating away from the training fingerprints. Using the Tanimoto kernel rather than RBF means that “far from training data” is measured in the similarity metric fingerprints were designed for, not in a Euclidean geometry that is not meaningful for sparse binary vectors.

Parameters:
  • kernel (Optional[Any]) – Covariance function. If None (default), a TanimotoKernel is constructed inside fit().

  • alpha (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]]) – Value added to the diagonal of the kernel matrix during fitting, interpreted as observation noise variance. The default is larger than scikit-learn’s 1e-10 to better tolerate the assay noise typical of biological QSAR endpoints.

  • optimizer (Optional[Any]) – Optimizer used to find the kernel hyperparameters maximizing the log-marginal-likelihood, or None to keep the initial hyperparameters fixed.

  • n_restarts_optimizer (int) – Number of restarts of the optimizer from hyperparameters sampled log-uniformly from their bounds, in addition to the initial run.

  • normalize_y (bool) – Whether to subtract the mean and scale the training targets to unit variance before fitting. True by default here (unlike scikit-learn’s False) since QSAR endpoints (pIC50, logS, …) are rarely already zero-mean.

  • copy_X_train (bool) – Whether a copy of the training data is stored.

  • n_targets (Optional[int]) – Number of targets expected when fitting multi-output data.

  • random_state (Optional[int]) – Seed controlling the optimizer’s random restarts.

Examples

>>> import numpy as np
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=30, n_features=6, random_state=0)
>>> X_binary = (X > np.median(X, axis=0)).astype(float)
>>> model = GaussianProcessQSAR(random_state=0).fit(X_binary, y)
>>> mean, std = model.predict(X_binary, return_std=True)
>>> mean.shape
(30,)
>>> bool((std >= 0).all())
True

References

kernel: Any | None
fit(X, y)[source]

Fit the Gaussian process, defaulting to a Tanimoto kernel.

Parameters:
Returns:

The fitted estimator.

Return type:

GaussianProcessQSAR

set_predict_request(*, return_cov='$UNCHANGED$', return_std='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the predict method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:
  • return_cov (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_cov parameter in predict.

  • return_std (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_std parameter in predict.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.RandomForestQSAR(n_estimators=500, *, criterion='squared_error', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='sqrt', max_leaf_nodes=None, min_impurity_decrease=0.0, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, ccp_alpha=0.0, max_samples=None, monotonic_cst=None)[source]

Bases: RandomForestRegressor

Random-forest regressor tuned with QSAR-sane defaults.

A thin subclass of sklearn.ensemble.RandomForestRegressor that only changes the defaults (larger forest, sqrt-of-features splitting) to values that repeatedly perform well on molecular descriptor/fingerprint QSAR benchmarks, while leaving every parameter of the parent estimator overridable. Random forests are a strong default QSAR model: they are insensitive to feature scaling, handle the mixed continuous/binary descriptor matrices typical of cheminformatics without preprocessing, and are robust to the correlated, high-dimensional descriptor sets QSAR studies routinely produce.

Parameters:
  • n_estimators (int) – Number of trees. QSAR forests benefit from more trees than the scikit-learn default (100) because descriptor sets are often high-dimensional and correlated, so more trees are needed to stabilize the feature-subsampling variance.

  • max_features (Union[str, int, float, None]) – Number of features considered at each split. "sqrt" is the classical Breiman recommendation for regression forests and decorrelates trees built from correlated molecular descriptors.

  • random_state (Optional[int]) – Seed for reproducibility. None every other parameter is forwarded verbatim to RandomForestRegressor.

Examples

>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=40, n_features=5, random_state=0)
>>> model = RandomForestQSAR(n_estimators=10, random_state=0).fit(X, y)
>>> model.predict(X).shape
(40,)

References

  • Breiman, L. (2001). “Random Forests.” Machine Learning, 45(1), 5-32. https://doi.org/10.1023/A:1010933404324

  • Svetnik, V., Liaw, A., Tong, C., Culberson, J. C., Sheridan, R. P. & Feuston, B. P. (2003). “Random Forest: A Classification and Regression Tool for Compound Classification and QSAR Modeling.” J. Chem. Inf. Comput. Sci., 43(6), 1947-1958. https://doi.org/10.1021/ci034160g

set_fit_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.SVMQSAR(kernel='rbf', degree=3, gamma='scale', coef0=0.0, tol=0.001, C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False, max_iter=-1)[source]

Bases: SVR

Support-vector regressor tuned with QSAR-sane defaults.

A thin subclass of sklearn.svm.SVR that keeps the parent’s full parameter set but defaults to an RBF kernel with gamma="scale" — the combination most consistently reported to work well for QSAR on molecular descriptors and fingerprints without extensive tuning. Support-vector regression is attractive for QSAR because its epsilon-insensitive loss is robust to the noisy, assay-variability- laden activity values typical of biological data, and the kernel trick lets it capture non-linear structure-activity relationships without hand-engineered interaction terms.

Parameters:
  • kernel (str) – Kernel used by the support-vector regressor.

  • C (float) – Regularization strength (inverse); larger values fit the training data more closely.

  • gamma (Union[str, float]) – Kernel coefficient for “rbf”, “poly” and “sigmoid”.

  • epsilon (float) – Width of the epsilon-insensitive tube within which no penalty is incurred.

Examples

>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=40, n_features=5, random_state=0)
>>> model = SVMQSAR().fit(X, y)
>>> model.predict(X).shape
(40,)

References

set_fit_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.NeuralNetworkQSAR(loss='squared_error', hidden_layer_sizes=(100, 50), activation='relu', *, solver='adam', alpha=0.001, batch_size='auto', learning_rate='constant', learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=True, random_state=None, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=True, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10, max_fun=15000)[source]

Bases: MLPRegressor

Feed-forward neural network regressor tuned with QSAR-sane defaults.

A thin subclass of sklearn.neural_network.MLPRegressor that keeps the full parent parameter set but defaults to a two-hidden-layer architecture with early stopping and moderate L2 regularization — settings that guard against the overfitting risk of neural networks on the small-to-medium (hundreds to low thousands of compounds) QSAR datasets typical of real drug-discovery projects, where a network with the scikit-learn defaults (a single 100-unit layer, no regularization, no early stopping) would otherwise happily memorize the training set.

Parameters:
  • hidden_layer_sizes (Tuple[int, ...]) – Sizes of the hidden layers. Two layers give the network enough capacity for non-linear structure-activity relationships without the data requirements of a deeper architecture.

  • activation (str) – Activation function of the hidden layers.

  • alpha (float) – L2 regularization strength, an order of magnitude above scikit-learn’s default to counter overfitting on small QSAR datasets.

  • early_stopping (bool) – Hold out part of the training data and stop when validation score stops improving, which is a cheap and effective safeguard against overfitting on limited QSAR data.

  • random_state (Optional[int]) – Seed for reproducible weight initialization and data shuffling.

Examples

>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=60, n_features=5, random_state=0)
>>> model = NeuralNetworkQSAR(max_iter=200, random_state=0).fit(X, y)
>>> model.predict(X).shape
(60,)

References

set_fit_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_partial_fit_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the partial_fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to partial_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 partial_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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in partial_fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class qsarkit.models.BaselineModel(task='regression', strategy=None)[source]

Bases: BaseEstimator

Naive baseline model for the OECD “beat the null model” check.

OECD (2007) Guidance Document No. 69, validation principle 4, requires that a QSAR model be reported together with “appropriate measures of goodness-of-fit” — and implicit in that requirement, echoed throughout the QSAR validation literature, is that a model which cannot outperform a trivial, feature-blind predictor (predict the training mean, predict the majority class, …) has told you nothing about the structure-activity relationship. BaselineModel is exactly that trivial predictor, packaged as a scikit-learn estimator so it can sit in the same cross-validation harness as the real model and produce a directly comparable score. Any QSAR model reported without first clearing this bar is not informative.

Parameters:
  • task (Literal['regression', 'classification']) – Which naive strategy family to use.

  • strategy (Optional[str]) – Strategy forwarded to the underlying scikit-learn dummy estimator. For task="regression" one of "mean", "median", "quantile", "constant" (default "mean"). For task="classification" one of "most_frequent", "stratified", "uniform", "prior" (default "most_frequent").

Variables:

dummy (DummyRegressor or DummyClassifier) – The fitted scikit-learn dummy estimator doing the actual work.

Examples

>>> import numpy as np
>>> X = np.zeros((10, 3))
>>> y = np.arange(10.0)
>>> model = BaselineModel(task="regression").fit(X, y)
>>> float(model.predict(X)[0]) == float(np.mean(y))
True

References

dummy_: DummyRegressor | DummyClassifier
fit(X, y)[source]

Fit the naive baseline for the configured task.

Parameters:
Returns:

The fitted estimator.

Return type:

BaselineModel

Raises:

ValueError – If task is not “regression” or “classification”.

predict(X)[source]

Predict using the naive strategy.

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Returns:

Constant (regression) or majority/sampled (classification) predictions, ignoring the actual feature values.

Return type:

ndarray[tuple[Any, ...], dtype[generic]]

score(X, y, sample_weight=None)[source]

Score the baseline: R^2 for regression, accuracy for classification.

Implemented explicitly rather than inherited, because this estimator switches task at construction time and so cannot carry either RegressorMixin or ClassifierMixin.

Parameters:
Returns:

R^2 when task="regression", accuracy when task="classification". A real model that cannot beat this number has learned nothing from the descriptors.

Return type:

float

predict_proba(X)[source]

Class probabilities for the naive classification baseline.

Parameters:

X (Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], complex, bytes, str, _NestedSequence[complex | bytes | str]])

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Raises:
set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

References