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.
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,BaseEstimatorUnified,
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.XGBRegressorif installed, otherwise the"gbm"fallback with a warning (Chen & Guestrin 2016)."lightgbm":lightgbm.LGBMRegressorif 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 whennameis 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}forname="rf"). Whennameis an instance, these are applied withset_params.model_args (
Optional[Sequence[Any]]) – Positional arguments for the constructor. Only meaningful whennameis 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’sfit. Some libraries only accept certain options there rather than in the constructor – XGBoost’seval_set, LightGBM’scallbacks, CatBoost’sverbose, or asample_weightarray.predict_params (
Optional[Dict[str,Any]]) – Extra keyword arguments passed to the estimator’spredict.
- 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,)
namealso 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_paramsreaches arguments the constructor does not take – the same mechanism servessample_weighthere andeval_setfor 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
Breiman, L. (2001). “Random Forests.” Machine Learning, 45(1), 5-32. https://doi.org/10.1023/A:1010933404324
Cortes, C. & Vapnik, V. (1995). “Support-Vector Networks.” Machine Learning, 20(3), 273-297. https://doi.org/10.1007/BF00994018
Chen, T. & Guestrin, C. (2016). “XGBoost: A Scalable Tree Boosting System.” KDD 2016. https://doi.org/10.1145/2939672.2939785
Ke, G. et al. (2017). “LightGBM: A Highly Efficient Gradient Boosting Decision Tree.” NeurIPS 2017.
Wold, S., Sjostrom, M. & Eriksson, L. (2001). Chemometrics and Intelligent Laboratory Systems, 58(2), 109-130. https://doi.org/10.1016/S0169-7439(01)00155-1
Hoerl, A. E. & Kennard, R. W. (1970). “Ridge Regression: Biased Estimation for Nonorthogonal Problems.” Technometrics, 12(1), 55-67. https://doi.org/10.1080/00401706.1970.10488634
Tibshirani, R. (1996). “Regression Shrinkage and Selection via the Lasso.” J. R. Stat. Soc. B, 58(1), 267-288. https://doi.org/10.1111/j.2517-6161.1996.tb02080.x
Zou, H. & Hastie, T. (2005). “Regularization and Variable Selection via the Elastic Net.” J. R. Stat. Soc. B, 67(2), 301-320. https://doi.org/10.1111/j.1467-9868.2005.00503.x
Winkler, D. A. (2004). “Neural Networks as Robust Tools in Drug Design and Analysis.” Mol. Biotechnol., 27(2), 139-167. https://doi.org/10.1385/MB:27:2:139
Ralaivola, L. et al. (2005). “Graph Kernels for Chemical Informatics.” Neural Networks, 18(8), 1093-1110. https://doi.org/10.1016/j.neunet.2005.07.009
- estimator_: BaseEstimator¶
- fit(X, y)[source]¶
Build (from
name) and fit the underlying regressor.- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]])y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]])
- Returns:
The fitted estimator.
- Return type:
- Raises:
ValueError – If
nameis not a recognized algorithm name.
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- 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,BaseEstimatorUnified,
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 singlenamestring.- 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.XGBClassifierif installed, otherwise the"gbm"fallback with a warning (Chen & Guestrin 2016)."lightgbm":lightgbm.LGBMClassifierif 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 aroundPLSRegressor(Barker & Rayens 2003)."ridge":RidgeClassifier— fast linear baseline; has no nativepredict_proba(calling it raisesAttributeError)."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":GaussianProcessClassifierwith aTanimotoKernel— probabilistic fingerprint-similarity classifier (Ralaivola et al. 2005).
random_state (
Optional[int]) – Seed forwarded to the underlying estimator, where applicable. Ignored whennameis 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}forname="rf"). Whennameis an instance, these are applied withset_params.model_args (
Optional[Sequence[Any]]) – Positional arguments for the constructor. Only meaningful whennameis 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’sfit. Some libraries only accept certain options there rather than in the constructor – XGBoost’seval_set, LightGBM’scallbacks, CatBoost’sverbose, or asample_weightarray.predict_params (
Optional[Dict[str,Any]]) – Extra keyword arguments passed to the estimator’spredict.predict_proba_params (
Optional[Dict[str,Any]]) – Extra keyword arguments passed to the estimator’spredict_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,nameaccepts 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 duringfit, so a wrapper that omits the attribute still works:>>> model.classes_.tolist() [0, 1]
References
Breiman, L. (2001). Machine Learning, 45(1), 5-32. https://doi.org/10.1023/A:1010933404324
Cortes, C. & Vapnik, V. (1995). Machine Learning, 20(3), 273-297. https://doi.org/10.1007/BF00994018
Chen, T. & Guestrin, C. (2016). KDD 2016. https://doi.org/10.1145/2939672.2939785
Ke, G. et al. (2017). NeurIPS 2017.
Barker, M. & Rayens, W. (2003). J. Chemometrics, 17(3), 166-173. https://doi.org/10.1002/cem.785
Hoerl, A. E. & Kennard, R. W. (1970). Technometrics, 12(1), 55-67. https://doi.org/10.1080/00401706.1970.10488634
Tibshirani, R. (1996). J. R. Stat. Soc. B, 58(1), 267-288. https://doi.org/10.1111/j.2517-6161.1996.tb02080.x
Zou, H. & Hastie, T. (2005). J. R. Stat. Soc. B, 67(2), 301-320. https://doi.org/10.1111/j.1467-9868.2005.00503.x
Winkler, D. A. (2004). Mol. Biotechnol., 27(2), 139-167. https://doi.org/10.1385/MB:27:2:139
Ralaivola, L. et al. (2005). Neural Networks, 18(8), 1093-1110. https://doi.org/10.1016/j.neunet.2005.07.009
- estimator_: BaseEstimator¶
- fit(X, y)[source]¶
Build (from
name) and fit the underlying classifier.- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]])y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – Class labels.
- Returns:
The fitted estimator.
- Return type:
- Raises:
ValueError – If
nameis not a recognized algorithm name.
- 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:
- Raises:
ModelNotFittedError – If called before
fit().AttributeError – If the underlying estimator has no
predict_proba(e.g.name="ridge", whoseRidgeClassifierbackend has no native probability estimates).
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- class qsarkit.models.PLSRegressor(n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)[source]¶
Bases:
PLSRegressionPLS regressor that additionally reports VIP variable-importance scores.
A thin subclass of
sklearn.cross_decomposition.PLSRegressionthat, 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)Xandybefore 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 copyXandyinfit()before scaling.
- Variables:
vip_scores (
ndarrayofshape (n_features,)) – Variable Importance in Projection score for each input feature, computed afterfit(). 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
- fit(X, y)[source]¶
Fit the PLS model and compute VIP scores.
- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – Training descriptors/fingerprints.y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – Training target(s).
- Returns:
The fitted estimator, with
vip_scores_populated.- Return type:
- set_predict_request(*, copy='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
predictmethod.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(seesklearn.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 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.
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- set_transform_request(*, copy='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
transformmethod.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(seesklearn.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 totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.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.
- class qsarkit.models.ConsensusModel(estimators, task='regression', method='averaging', weights=None, final_estimator=None, cv=5)[source]¶
Bases:
BaseEstimatorConsensus 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") andStackingRegressor/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 whenmethod="averaging".final_estimator (
Optional[BaseEstimator]) – Meta-estimator used only whenmethod="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, whenmethod="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
Wolpert, D. H. (1992). “Stacked Generalization.” Neural Networks, 5(2), 241-259. https://doi.org/10.1016/S0893-6080(05)80023-1
Dietterich, T. G. (2000). “Ensemble Methods in Machine Learning.” In: Multiple Classifier Systems (MCS 2000), LNCS 1857, 1-15. https://doi.org/10.1007/3-540-45014-9_1
- estimator_: BaseEstimator¶
- fit(X, y)[source]¶
Build and fit the internal voting/stacking ensemble.
- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]])y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]])
- Returns:
The fitted estimator.
- Return type:
- Raises:
ValueError – If
task/methodare not among the allowed literals, orestimatorsis empty.
- 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
RegressorMixinorClassifierMixin.- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]])y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – True values.sample_weight (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str],None])
- Return type:
- 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:
- Raises:
ModelNotFittedError – If called before
fit().AttributeError – If the internal ensemble does not support
predict_proba(e.g. stacking classification with afinal_estimatorthat itself has nopredict_proba).
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- class qsarkit.models.TanimotoKernel(sigma_0=1.0, sigma_0_bounds=(1e-05, 100000.0))[source]¶
Bases:
KernelTanimoto (Jaccard) kernel for fingerprint-valued Gaussian processes.
Implements
k(x, y) = sigma_0^2 * T(x, y), whereTis the Tanimoto coefficient|x AND y| / |x OR y|computed byqsarkit.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**2for a binary fingerprintx(self-similarity is always 1 under Tanimoto).sigma_0_bounds (
Union[Tuple[float,float],str]) – Lower and upper bound onsigma_0used by the GP’s hyperparameter optimizer, or the string"fixed"to keepsigma_0constant 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
Ralaivola, L., Swamidass, S. J., Saigo, H. & Baldi, P. (2005). “Graph Kernels for Chemical Informatics.” Neural Networks, 18(8), 1093-1110. https://doi.org/10.1016/j.neunet.2005.07.009
Rasmussen, C. E. & Williams, C. K. I. (2006). “Gaussian Processes for Machine Learning.” MIT Press. ISBN 0-262-18253-X. Freely available at http://gaussianprocess.org/gpml/
scikit-learn custom kernel documentation: https://scikit-learn.org/stable/modules/gaussian_process.html#kernels-for-gaussian-processes
- property hyperparameter_sigma_0: Hyperparameter¶
The
sigma_0hyperparameter descriptor scikit-learn introspects.
- 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:
GaussianProcessRegressorGaussian-process regressor defaulting to a Tanimoto fingerprint kernel.
A thin subclass of
sklearn.gaussian_process.GaussianProcessRegressorwhose only behavioural change is the default kernel: whenkernel=None(the constructor default, exactly as in the parent class),fit()builds aTanimotoKernelrather than scikit-learn’s own RBF default — mirroring the parent class’s own convention of resolvingkernel=Noneto a concrete kernel lazily insidefit(), so__init__keeps storing exactly what was passed to it (sklearn’sget_params/clonecontract).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), aTanimotoKernelis constructed insidefit().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’s1e-10to 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
Ralaivola, L., Swamidass, S. J., Saigo, H. & Baldi, P. (2005). “Graph Kernels for Chemical Informatics.” Neural Networks, 18(8), 1093-1110. https://doi.org/10.1016/j.neunet.2005.07.009
Rasmussen, C. E. & Williams, C. K. I. (2006). “Gaussian Processes for Machine Learning.” MIT Press. ISBN 0-262-18253-X. Freely available at http://gaussianprocess.org/gpml/
- fit(X, y)[source]¶
Fit the Gaussian process, defaulting to a Tanimoto kernel.
- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – Training fingerprints or descriptors.y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – Training target values.
- Returns:
The fitted estimator.
- Return type:
- set_predict_request(*, return_cov='$UNCHANGED$', return_std='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
predictmethod.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(seesklearn.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 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.
- Parameters:
- Returns:
self – The updated object.
- Return type:
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- 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:
RandomForestRegressorRandom-forest regressor tuned with QSAR-sane defaults.
A thin subclass of
sklearn.ensemble.RandomForestRegressorthat 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 toRandomForestRegressor.
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
fitmethod.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(seesklearn.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 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.
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- 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:
SVRSupport-vector regressor tuned with QSAR-sane defaults.
A thin subclass of
sklearn.svm.SVRthat keeps the parent’s full parameter set but defaults to an RBF kernel withgamma="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
Cortes, C. & Vapnik, V. (1995). “Support-Vector Networks.” Machine Learning, 20(3), 273-297. https://doi.org/10.1007/BF00994018
Burbidge, R., Trotter, M., Buxton, B. & Holden, S. (2001). “Drug Design by Machine Learning: Support Vector Machines for Pharmaceutical Data Analysis.” Comput. Chem., 26(1), 5-14. https://doi.org/10.1016/S0097-8485(01)00094-8
- set_fit_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
fitmethod.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(seesklearn.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 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.
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- 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:
MLPRegressorFeed-forward neural network regressor tuned with QSAR-sane defaults.
A thin subclass of
sklearn.neural_network.MLPRegressorthat 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
Winkler, D. A. (2004). “Neural Networks as Robust Tools in Drug Design and Analysis.” Mol. Biotechnol., 27(2), 139-167. https://doi.org/10.1385/MB:27:2:139
- set_fit_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
fitmethod.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(seesklearn.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 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.
- set_partial_fit_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
partial_fitmethod.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(seesklearn.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 topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_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.
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
- class qsarkit.models.BaselineModel(task='regression', strategy=None)[source]¶
Bases:
BaseEstimatorNaive 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.
BaselineModelis 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. Fortask="regression"one of"mean","median","quantile","constant"(default"mean"). Fortask="classification"one of"most_frequent","stratified","uniform","prior"(default"most_frequent").
- Variables:
dummy (
DummyRegressororDummyClassifier) – 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
OECD (2007). “Guidance Document on the Validation of (Quantitative) Structure-Activity Relationship [(Q)SAR] Models.” OECD Series on Testing and Assessment No. 69, ENV/JM/MONO(2007)2, Principle 4 (“a model should be associated with… appropriate measures of goodness-of-fit”). https://doi.org/10.1787/9789264085442-en
scikit-learn
DummyRegressor/DummyClassifierdocumentation: https://scikit-learn.org/stable/modules/model_evaluation.html#dummy-estimators
- dummy_: DummyRegressor | DummyClassifier¶
- fit(X, y)[source]¶
Fit the naive baseline for the configured task.
- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – Ignored except for shape/length checks (the whole point of a baseline is that it does not use the features).y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – Target values or class labels.
- Returns:
The fitted estimator.
- Return type:
- Raises:
ValueError – If
taskis not “regression” or “classification”.
- predict(X)[source]¶
Predict using the naive strategy.
- 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
RegressorMixinorClassifierMixin.- Parameters:
X (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]])y (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – True values.sample_weight (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str],None])
- Returns:
R^2 when
task="regression", accuracy whentask="classification". A real model that cannot beat this number has learned nothing from the descriptors.- Return type:
- 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:
- Raises:
ModelNotFittedError – If called before
fit().AttributeError – If
task="regression", which has no notion of class probabilities.
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 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.
References¶
Breiman, L. (2001). “Random Forests.” Machine Learning, 45(1), 5-32. doi:10.1023/A:1010933404324
Wold, S., Sjostrom, M. & Eriksson, L. (2001). “PLS-Regression.” Chemom. Intell. Lab. Syst., 58(2), 109-130. doi:10.1016/S0169-7439(01)00155-1
Chong, I.-G. & Jun, C.-H. (2005). “Performance of Some Variable Selection Methods When Multicollinearity Is Present.” Chemom. Intell. Lab. Syst., 78(1-2), 103-112. doi:10.1016/j.chemolab.2004.12.011
Ralaivola, L. et al. (2005). “Graph Kernels for Chemical Informatics.” Neural Netw., 18(8), 1093-1110. doi:10.1016/j.neunet.2005.07.009