Model Wrappers

Wrappers that bring external models into Modeva workflows.

class modeva.models.MoRegressor(predict_function, fit_function=None, name=None, **kwargs)[source]

A model wrapper for arbitrary regression models with predict and fit functions.

Parameters:
predict_functioncallable

Callable function for making predictions. It takes 2D numpy array as inputs and outputs 1D numpy array.

fit_functioncallable, default=None

Callable function for fitting the model. It takes X, y, sample_weights, and hyperparameters as inputs and trains the model.

namestr, default=None

Optional name for the model.

**kwargsdict

Hyperparameters to store as attributes for compatibility with scikit-learn hyperparameter tuning.

calibrate_interval(X, y, alpha=0.1, max_depth: int = 5)

Fit a conformal prediction model to the given data.

This method computes the model’s prediction interval calibrated to the given data.

If the model is a regressor, splits the data with 50% for fitting lower (alpha / 5) and upper (1 - alpha / 2) gradient boosting trees-based quantile regression to the model’s residual; and 50% for calibration.

If the model is a binary classifiers, it computes the calibration quantile based on predicted probabilities for the positive class.

Parameters:
XXnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

yarray-like of shape (n_samples, )

Target values.

alphafloat, default=0.1

Expected miscoverage for the conformal prediction.

max_depthint, default=5

Maximum depth of the gradient boosting trees for regression tasks. Only used when task_type is REGRESSION.

Raises:
ValueError: If the model is neither a regressor nor a classifier.
fit(X, y, sample_weight=None)[source]

Fits the model using the provided data.

Parameters:
Xarray-like

Input features.

yarray-like

Target values.

sample_weightarray-like, default=None

Optional weights for the samples.

Returns:
selfobject

Fitted instance of the model.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

load(file_name: str)

Load the model into memory from file system.

Parameters:
file_name: str

The path and name of the file.

Returns:
estimator object
predict(X)

Model predictions, calling the child class’s ‘_predict’ method.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The (calibrated) final prediction
predict_interval(X)

Predict the prediction interval for the given data based on the conformal prediction model.

It splits the data with 50% for fitting lower (alpha / 5) and upper (1 - alpha / 2) gradient boosting trees-based quantile regression to the model’s residual; and 50% for calibration.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The lower and upper bounds of the prediction intervals for each sample

in the format [n_samples, 2] for regressors or a flattened array for classifiers.

Raises:
ValueError: If fit_conformal has not been called to fit the conformal prediction model

before calling this method.

save(file_name: str)

Save the model into file system.

Parameters:
file_name: str

The path and name of the file.

score(X, y, sample_weight=None)

Return the coefficient of determination of the prediction.

The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)** 2).sum() and \(v\) is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

\(R^2\) of self.predict(X) w.r.t. y.

Notes

The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score(). This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoRegressor

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance
set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoRegressor

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

class modeva.models.MoClassifier(predict_proba_function, fit_function=None, name=None, **kwargs)[source]

A model wrapper for arbitrary classification models with predict, predict_proba, and fit functions.

Parameters:
predict_proba_functioncallable

Callable function for predicting probabilities. It takes 2D numpy array as inputs and outputs 2D numpy array.

fit_functioncallable, default=None

Callable function for fitting the model. It takes X, y, sample_weights, and hyperparameters as inputs and trains the model.

namestr, default=None

Optional name for the model.

**kwargsdict

Hyperparameters to store as attributes for compatibility with scikit-learn hyperparameter tuning.

calibrate_interval(X, y, alpha=0.1)

Fit a conformal prediction model to the given data.

This method computes the model’s prediction interval calibrated to the given data.

It computes the calibration quantile based on predicted probabilities for the positive class.

Parameters:
XXnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

yarray-like of shape (n_samples, )

Target values.

alphafloat, default=0.1

Expected miscoverage for the conformal prediction.

Raises:
ValueError: If the model is neither a regressor nor a classifier.
calibrate_proba(X, y, sample_weight=None, method='sigmoid')

Fit the calibration method on the model’s predictions.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

ynp.ndarray of shape (n_samples, )

Ground truth labels.

sample_weightarray-like, shape (n_samples,), default=None

Sample weights.

method{‘sigmoid’, ‘isotonic’}, default=’sigmoid’

The calibration method.

  • ‘sigmoid’: Platt’s method, i.e., fit a logistic regression on predicted probabilities and y

  • ‘isotonic’: Fit an isotonic regression on predicted probabilities and y.

Returns:
self: Calibrated estimator
decision_function(X, calibration: bool = True)

Computes the decision function for the given input data.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns:
logit_predictionarray, shape (n_samples,) or (n_samples, n_classes)

Array of (calibrated) logit predictions.

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

Fits the model using the provided data.

Parameters:
Xarray-like

Input features.

yarray-like

Target values.

sample_weightarray-like, default=None

Optional weights for the samples.

Returns:
selfobject

Fitted instance of the model.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

load(file_name: str)

Load the model into memory from file system.

Parameters:
file_name: str

The path and name of the file.

Returns:
estimator object
predict(X, calibration: bool = True)

Model predictions, calling the child class’s ‘_predict’ method.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns:
np.ndarray: The (calibrated) final prediction
predict_interval(X)

Predict the prediction set for the given data based on the conformal prediction model.

This method computes the model prediction interval (regression) or prediction sets (classification) using conformal prediction.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The lower and upper bounds of the prediction intervals for each sample

in the format [n_samples, 2] for regressors or a flattened array for classifiers.

Raises:
ValueError: If fit_conformal has not been called to fit the conformal prediction model

before calling this method.

predict_proba(X, calibration: bool = True)

Predict (calibrated) probabilities for X.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will return calibrated probability if calibration is done. Otherwise, will return raw probability.

Returns:
np.ndarray: The (calibrated) predicted probabilities
save(file_name: str)

Save the model into file system.

Parameters:
file_name: str

The path and name of the file.

score(X, y, sample_weight=None)

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

set_decision_function_request(*, calibration: bool | None | str = '$UNCHANGED$') MoClassifier

Request metadata passed to the decision_function method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

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

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in decision_function.

Returns:
selfobject

The updated object.

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoClassifier

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance
set_predict_proba_request(*, calibration: bool | None | str = '$UNCHANGED$') MoClassifier

Request metadata passed to the predict_proba method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

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

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in predict_proba.

Returns:
selfobject

The updated object.

set_predict_request(*, calibration: bool | None | str = '$UNCHANGED$') MoClassifier

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoClassifier

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

class modeva.models.MoSKLearnRegressor(estimator, name: str = None)[source]

A template wrapper for scikit-learn regressors.

Parameters:
estimatorobject

The scikit-learn regressor to wrap.

namestr, optional

The name of the model.

calibrate_interval(X, y, alpha=0.1, max_depth: int = 5)

Fit a conformal prediction model to the given data.

This method computes the model’s prediction interval calibrated to the given data.

If the model is a regressor, splits the data with 50% for fitting lower (alpha / 5) and upper (1 - alpha / 2) gradient boosting trees-based quantile regression to the model’s residual; and 50% for calibration.

If the model is a binary classifiers, it computes the calibration quantile based on predicted probabilities for the positive class.

Parameters:
XXnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

yarray-like of shape (n_samples, )

Target values.

alphafloat, default=0.1

Expected miscoverage for the conformal prediction.

max_depthint, default=5

Maximum depth of the gradient boosting trees for regression tasks. Only used when task_type is REGRESSION.

Raises:
ValueError: If the model is neither a regressor nor a classifier.
fit(X, y, sample_weight=None, **kwargs)[source]

Fits the estimator to the provided data.

Parameters:
Xarray-like, shape (n_samples, n_features)

Training data.

yarray-like, shape (n_samples,) or (n_samples, n_outputs)

Target values.

sample_weightarray-like, shape (n_samples,), default=None

Sample weights.

Returns:
selfobject

Fitted model instance.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

load(file_name: str)

Load the model into memory from file system.

Parameters:
file_name: str

The path and name of the file.

Returns:
estimator object
predict(X)

Model predictions, calling the child class’s ‘_predict’ method.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The (calibrated) final prediction
predict_interval(X)

Predict the prediction interval for the given data based on the conformal prediction model.

It splits the data with 50% for fitting lower (alpha / 5) and upper (1 - alpha / 2) gradient boosting trees-based quantile regression to the model’s residual; and 50% for calibration.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The lower and upper bounds of the prediction intervals for each sample

in the format [n_samples, 2] for regressors or a flattened array for classifiers.

Raises:
ValueError: If fit_conformal has not been called to fit the conformal prediction model

before calling this method.

save(file_name: str)

Save the model into file system.

Parameters:
file_name: str

The path and name of the file.

score(X, y, sample_weight=None)

Return the coefficient of determination of the prediction.

The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)** 2).sum() and \(v\) is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

\(R^2\) of self.predict(X) w.r.t. y.

Notes

The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score(). This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoSKLearnRegressor

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance
set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoSKLearnRegressor

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

class modeva.models.MoSKLearnClassifier(estimator, name: str = None)[source]

A template wrapper for scikit-learn classifiers.

Parameters:
estimatorobject

The scikit-learn classifier to wrap.

namestr, optional

The name of the model.

calibrate_interval(X, y, alpha=0.1)

Fit a conformal prediction model to the given data.

This method computes the model’s prediction interval calibrated to the given data.

It computes the calibration quantile based on predicted probabilities for the positive class.

Parameters:
XXnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

yarray-like of shape (n_samples, )

Target values.

alphafloat, default=0.1

Expected miscoverage for the conformal prediction.

Raises:
ValueError: If the model is neither a regressor nor a classifier.
calibrate_proba(X, y, sample_weight=None, method='sigmoid')

Fit the calibration method on the model’s predictions.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

ynp.ndarray of shape (n_samples, )

Ground truth labels.

sample_weightarray-like, shape (n_samples,), default=None

Sample weights.

method{‘sigmoid’, ‘isotonic’}, default=’sigmoid’

The calibration method.

  • ‘sigmoid’: Platt’s method, i.e., fit a logistic regression on predicted probabilities and y

  • ‘isotonic’: Fit an isotonic regression on predicted probabilities and y.

Returns:
self: Calibrated estimator
decision_function(X, calibration: bool = True)

Computes the decision function for the given input data.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns:
logit_predictionarray, shape (n_samples,) or (n_samples, n_classes)

Array of (calibrated) logit predictions.

fit(X, y, sample_weight=None, **kwargs)[source]

Fits the estimator to the provided data.

Parameters:
Xarray-like, shape (n_samples, n_features)

Training data.

yarray-like, shape (n_samples,) or (n_samples, n_outputs)

Target values.

sample_weightarray-like, shape (n_samples,), default=None

Sample weights.

Returns:
selfobject

Fitted model instance.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

load(file_name: str)

Load the model into memory from file system.

Parameters:
file_name: str

The path and name of the file.

Returns:
estimator object
predict(X, calibration: bool = True)

Model predictions, calling the child class’s ‘_predict’ method.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns:
np.ndarray: The (calibrated) final prediction
predict_interval(X)

Predict the prediction set for the given data based on the conformal prediction model.

This method computes the model prediction interval (regression) or prediction sets (classification) using conformal prediction.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The lower and upper bounds of the prediction intervals for each sample

in the format [n_samples, 2] for regressors or a flattened array for classifiers.

Raises:
ValueError: If fit_conformal has not been called to fit the conformal prediction model

before calling this method.

predict_proba(X, calibration: bool = True)

Predict (calibrated) probabilities for X.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will return calibrated probability if calibration is done. Otherwise, will return raw probability.

Returns:
np.ndarray: The (calibrated) predicted probabilities
save(file_name: str)

Save the model into file system.

Parameters:
file_name: str

The path and name of the file.

score(X, y, sample_weight=None)

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

set_decision_function_request(*, calibration: bool | None | str = '$UNCHANGED$') MoSKLearnClassifier

Request metadata passed to the decision_function method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

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

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in decision_function.

Returns:
selfobject

The updated object.

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoSKLearnClassifier

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance
set_predict_proba_request(*, calibration: bool | None | str = '$UNCHANGED$') MoSKLearnClassifier

Request metadata passed to the predict_proba method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

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

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in predict_proba.

Returns:
selfobject

The updated object.

set_predict_request(*, calibration: bool | None | str = '$UNCHANGED$') MoSKLearnClassifier

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoSKLearnClassifier

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

class modeva.models.MoScoredRegressor(dataset, prediction_name: str = None, name: str = None)[source]

A wrapper for a scored regression model that provides predictions without holding the model object itself.

Parameters:
datasetobject

The dataset to be used for predictions.

prediction_namestr

The prediction column name in dataset. If None, will use the prediction_proba_name attribute in dataset.

namestr, default=None

The name of the model.

calibrate_interval(X, y, alpha=0.1, max_depth: int = 5)

Fit a conformal prediction model to the given data.

This method computes the model’s prediction interval calibrated to the given data.

If the model is a regressor, splits the data with 50% for fitting lower (alpha / 5) and upper (1 - alpha / 2) gradient boosting trees-based quantile regression to the model’s residual; and 50% for calibration.

If the model is a binary classifiers, it computes the calibration quantile based on predicted probabilities for the positive class.

Parameters:
XXnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

yarray-like of shape (n_samples, )

Target values.

alphafloat, default=0.1

Expected miscoverage for the conformal prediction.

max_depthint, default=5

Maximum depth of the gradient boosting trees for regression tasks. Only used when task_type is REGRESSION.

Raises:
ValueError: If the model is neither a regressor nor a classifier.
get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

load(file_name: str)

Load the model into memory from file system.

Parameters:
file_name: str

The path and name of the file.

Returns:
estimator object
predict(X)

Model predictions, calling the child class’s ‘_predict’ method.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The (calibrated) final prediction
predict_interval(X)

Predict the prediction interval for the given data based on the conformal prediction model.

It splits the data with 50% for fitting lower (alpha / 5) and upper (1 - alpha / 2) gradient boosting trees-based quantile regression to the model’s residual; and 50% for calibration.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The lower and upper bounds of the prediction intervals for each sample

in the format [n_samples, 2] for regressors or a flattened array for classifiers.

Raises:
ValueError: If fit_conformal has not been called to fit the conformal prediction model

before calling this method.

save(file_name: str)

Save the model into file system.

Parameters:
file_name: str

The path and name of the file.

score(X, y, sample_weight=None)

Return the coefficient of determination of the prediction.

The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)** 2).sum() and \(v\) is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

\(R^2\) of self.predict(X) w.r.t. y.

Notes

The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score(). This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoScoredRegressor

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

class modeva.models.MoScoredClassifier(dataset, prediction_proba_name: str = None, name: str = None)[source]

A wrapper for a scored classification model that provides predictions and probability estimates without holding the model object itself.

Parameters:
datasetobject

The dataset to be used for predictions.

prediction_proba_namestr, default=None

The prediction_proba column name in dataset. If None, will use the prediction_proba_name attribute in dataset.

namestr, default=None

The name of the model.

calibrate_interval(X, y, alpha=0.1)

Fit a conformal prediction model to the given data.

This method computes the model’s prediction interval calibrated to the given data.

It computes the calibration quantile based on predicted probabilities for the positive class.

Parameters:
XXnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

yarray-like of shape (n_samples, )

Target values.

alphafloat, default=0.1

Expected miscoverage for the conformal prediction.

Raises:
ValueError: If the model is neither a regressor nor a classifier.
calibrate_proba(X, y, sample_weight=None, method='sigmoid')

Fit the calibration method on the model’s predictions.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

ynp.ndarray of shape (n_samples, )

Ground truth labels.

sample_weightarray-like, shape (n_samples,), default=None

Sample weights.

method{‘sigmoid’, ‘isotonic’}, default=’sigmoid’

The calibration method.

  • ‘sigmoid’: Platt’s method, i.e., fit a logistic regression on predicted probabilities and y

  • ‘isotonic’: Fit an isotonic regression on predicted probabilities and y.

Returns:
self: Calibrated estimator
decision_function(X, calibration: bool = True)

Computes the decision function for the given input data.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns:
logit_predictionarray, shape (n_samples,) or (n_samples, n_classes)

Array of (calibrated) logit predictions.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

load(file_name: str)

Load the model into memory from file system.

Parameters:
file_name: str

The path and name of the file.

Returns:
estimator object
predict(X, calibration: bool = True)

Model predictions, calling the child class’s ‘_predict’ method.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns:
np.ndarray: The (calibrated) final prediction
predict_interval(X)

Predict the prediction set for the given data based on the conformal prediction model.

This method computes the model prediction interval (regression) or prediction sets (classification) using conformal prediction.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

Returns:
np.ndarray: The lower and upper bounds of the prediction intervals for each sample

in the format [n_samples, 2] for regressors or a flattened array for classifiers.

Raises:
ValueError: If fit_conformal has not been called to fit the conformal prediction model

before calling this method.

predict_proba(X, calibration: bool = True)

Predict (calibrated) probabilities for X.

Parameters:
Xnp.ndarray of shape (n_samples, n_features)

Feature matrix for prediction.

calibrationbool, default=True

If True, will return calibrated probability if calibration is done. Otherwise, will return raw probability.

Returns:
np.ndarray: The (calibrated) predicted probabilities
save(file_name: str)

Save the model into file system.

Parameters:
file_name: str

The path and name of the file.

score(X, y, sample_weight=None)

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

set_decision_function_request(*, calibration: bool | None | str = '$UNCHANGED$') MoScoredClassifier

Request metadata passed to the decision_function method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

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

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in decision_function.

Returns:
selfobject

The updated object.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_predict_proba_request(*, calibration: bool | None | str = '$UNCHANGED$') MoScoredClassifier

Request metadata passed to the predict_proba method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

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

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in predict_proba.

Returns:
selfobject

The updated object.

set_predict_request(*, calibration: bool | None | str = '$UNCHANGED$') MoScoredClassifier

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
calibrationstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for calibration parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MoScoredClassifier

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.