from sklearn.base import RegressorMixin, ClassifierMixin
from sklearn.base import check_is_fitted
from ..base import ModelBaseRegressor, ModelBaseClassifier
from ...auth import auth
[docs]
class MoSKLearnRegressor(RegressorMixin, ModelBaseRegressor):
"""
A template wrapper for scikit-learn regressors.
Parameters
----------
estimator : object
The scikit-learn regressor to wrap.
name : str, optional
The name of the model.
"""
def __init__(self, estimator, name: str = None):
auth.run()
self.name = name
self.estimator = estimator
def __getattr__(self, attr):
return getattr(self.estimator, attr)
def __sklearn_is_fitted__(self):
try:
check_is_fitted(self.estimator)
return True
except:
return False
[docs]
def get_params(self, deep=True):
"""
Get parameters for this estimator.
Parameters
----------
deep : bool, default=True
If True, will return the parameters for this estimator and
contained subobjects that are estimators.
Returns
-------
params : dict
Parameter names mapped to their values.
"""
params = {"name": self.name, "estimator": self.estimator}
return params
[docs]
def set_params(self, **params):
"""Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as :class:`~sklearn.pipeline.Pipeline`). The latter have
parameters of the form ``<component>__<parameter>`` so that it's
possible to update each component of a nested object.
Parameters
----------
**params : dict
Estimator parameters.
Returns
-------
self : estimator instance
"""
if "name" in params:
self.name = params.pop("name")
if "estimator" in params:
self.estimator = params.pop("estimator")
self.estimator.set_params(**params)
return self
[docs]
def fit(self, X, y, sample_weight=None, **kwargs):
"""
Fits the estimator to the provided data.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Training data.
y : array-like, shape (n_samples,) or (n_samples, n_outputs)
Target values.
sample_weight : array-like, shape (n_samples,), default=None
Sample weights.
Returns
-------
self : object
Fitted model instance.
"""
try:
self.estimator.fit(X=X, y=y, sample_weight=sample_weight, **kwargs)
except (TypeError, ValueError):
self.estimator.fit(X=X, y=y, **kwargs)
return self
def _predict(self, X):
"""
Makes predictions using the fitted model.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Input data for prediction.
Returns
-------
array-like
The predicted values.
"""
return self.estimator.predict(X)
[docs]
class MoSKLearnClassifier(ClassifierMixin, ModelBaseClassifier):
"""
A template wrapper for scikit-learn classifiers.
Parameters
----------
estimator : object
The scikit-learn classifier to wrap.
name : str, optional
The name of the model.
"""
def __init__(self, estimator, name: str = None):
auth.run()
self.name = name
self.estimator = estimator
def __getattr__(self, attr):
return getattr(self.estimator, attr)
def __sklearn_is_fitted__(self):
try:
check_is_fitted(self.estimator)
return True
except:
return False
[docs]
def get_params(self, deep=True):
"""
Get parameters for this estimator.
Parameters
----------
deep : bool, default=True
If True, will return the parameters for this estimator and
contained subobjects that are estimators.
Returns
-------
params : dict
Parameter names mapped to their values.
"""
params = {"name": self.name, "estimator": self.estimator}
return params
[docs]
def set_params(self, **params):
"""Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as :class:`~sklearn.pipeline.Pipeline`). The latter have
parameters of the form ``<component>__<parameter>`` so that it's
possible to update each component of a nested object.
Parameters
----------
**params : dict
Estimator parameters.
Returns
-------
self : estimator instance
"""
if "name" in params:
self.name = params.pop("name")
if "estimator" in params:
self.estimator = params.pop("estimator")
self.estimator.set_params(**params)
return self
[docs]
def fit(self, X, y, sample_weight=None, **kwargs):
"""
Fits the estimator to the provided data.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Training data.
y : array-like, shape (n_samples,) or (n_samples, n_outputs)
Target values.
sample_weight : array-like, shape (n_samples,), default=None
Sample weights.
Returns
-------
self : object
Fitted model instance.
"""
try:
self.estimator.fit(X=X, y=y, sample_weight=sample_weight, **kwargs)
except (TypeError, ValueError):
self.estimator.fit(X=X, y=y, **kwargs)
return self
def _predict_proba(self, X):
"""
Predicts class probabilities for the given input data.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Input data for prediction.
Returns
-------
proba : array, shape (n_samples, n_classes)
Array of predicted probabilities for each class.
"""
return self.estimator.predict_proba(X)