Model Wrappers

Wrappers that bring external models into Modeva workflows.

MoRegressor

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

Parameters

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

fit_function : callable, default=None
Callable function for fitting the model. It takes X, y, sample_weights, and hyperparameters as inputs and trains the model.

name : str, default=None
Optional name for the model.

**kwargs : dict
Hyperparameters to store as attributes for compatibility with scikit-learn hyperparameter tuning.

__init__(predict_function, fit_function=None, name=None, **kwargs)

get_params(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.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as ~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

fit(X, y, sample_weight=None)

Fits the model using the provided data.

Parameters

X : array-like
Input features.

y : array-like
Target values.

sample_weight : array-like, default=None
Optional weights for the samples.

Returns

self : object
Fitted instance of the model.

reset_calibrate_interval()

reset_calibrate_proba()

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

X : X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : array-like of shape (n_samples, )
Target values.

alpha : float, default=0.1
Expected miscoverage for the conformal prediction.

max_depth : int, 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.

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

X : np.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(X)

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

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

Returns

np.ndarray: The (calibrated) final prediction

name()

version()

save(file_name: str)

Save the model into file system.

Parameters

file_name: str
The path and name of the file.

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

MoClassifier

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

Parameters

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

fit_function : callable, default=None
Callable function for fitting the model. It takes X, y, sample_weights, and hyperparameters as inputs and trains the model.

name : str, default=None
Optional name for the model.

**kwargs : dict
Hyperparameters to store as attributes for compatibility with scikit-learn hyperparameter tuning.

__init__(predict_proba_function, fit_function=None, name=None, **kwargs)

get_params(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.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as ~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

fit(X, y, sample_weight=None)

Fits the model using the provided data.

Parameters

X : array-like
Input features.

y : array-like
Target values.

sample_weight : array-like, default=None
Optional weights for the samples.

Returns

self : object
Fitted instance of the model.

reset_calibrate_interval()

reset_calibrate_proba()

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

X : X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : array-like of shape (n_samples, )
Target values.

alpha : float, default=0.1
Expected miscoverage for the conformal prediction.

Raises

ValueError: If the model is neither a regressor nor a classifier.

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

X : np.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.

calibrate_proba(X, y, sample_weight=None, method='sigmoid')

Fit the calibration method on the model’s predictions.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : np.ndarray of shape (n_samples, )
Ground truth labels.

sample_weight : array-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

predict_proba(X, calibration: bool=True)

Predict (calibrated) probabilities for X.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will return calibrated probability if calibration is done. Otherwise, will return raw probability.

Returns

np.ndarray: The (calibrated) predicted probabilities

predict(X, calibration: bool=True)

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

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns

np.ndarray: The (calibrated) final prediction

decision_function(X, calibration: bool=True)

Computes the decision function for the given input data.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns

logit_prediction : array, shape (n_samples,) or (n_samples, n_classes)
Array of (calibrated) logit predictions.

name()

version()

save(file_name: str)

Save the model into file system.

Parameters

file_name: str
The path and name of the file.

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

MoSKLearnRegressor

A template wrapper for scikit-learn regressors.

Parameters

estimator : object
The scikit-learn regressor to wrap.

name : str, optional
The name of the model.

__init__(estimator, name: str=None)

get_params(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.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as ~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

fit(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.

reset_calibrate_interval()

reset_calibrate_proba()

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

X : X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : array-like of shape (n_samples, )
Target values.

alpha : float, default=0.1
Expected miscoverage for the conformal prediction.

max_depth : int, 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.

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

X : np.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(X)

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

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

Returns

np.ndarray: The (calibrated) final prediction

name()

version()

save(file_name: str)

Save the model into file system.

Parameters

file_name: str
The path and name of the file.

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

MoSKLearnClassifier

A template wrapper for scikit-learn classifiers.

Parameters

estimator : object
The scikit-learn classifier to wrap.

name : str, optional
The name of the model.

__init__(estimator, name: str=None)

get_params(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.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as ~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

fit(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.

reset_calibrate_interval()

reset_calibrate_proba()

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

X : X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : array-like of shape (n_samples, )
Target values.

alpha : float, default=0.1
Expected miscoverage for the conformal prediction.

Raises

ValueError: If the model is neither a regressor nor a classifier.

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

X : np.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.

calibrate_proba(X, y, sample_weight=None, method='sigmoid')

Fit the calibration method on the model’s predictions.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : np.ndarray of shape (n_samples, )
Ground truth labels.

sample_weight : array-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

predict_proba(X, calibration: bool=True)

Predict (calibrated) probabilities for X.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will return calibrated probability if calibration is done. Otherwise, will return raw probability.

Returns

np.ndarray: The (calibrated) predicted probabilities

predict(X, calibration: bool=True)

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

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns

np.ndarray: The (calibrated) final prediction

decision_function(X, calibration: bool=True)

Computes the decision function for the given input data.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns

logit_prediction : array, shape (n_samples,) or (n_samples, n_classes)
Array of (calibrated) logit predictions.

name()

version()

save(file_name: str)

Save the model into file system.

Parameters

file_name: str
The path and name of the file.

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

MoScoredRegressor

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

Parameters

dataset : object
The dataset to be used for predictions.

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

name : str, default=None
The name of the model.

__init__(dataset, prediction_name: str=None, name: str=None)

reset_calibrate_interval()

reset_calibrate_proba()

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

X : X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : array-like of shape (n_samples, )
Target values.

alpha : float, default=0.1
Expected miscoverage for the conformal prediction.

max_depth : int, 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.

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

X : np.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(X)

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

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

Returns

np.ndarray: The (calibrated) final prediction

name()

version()

save(file_name: str)

Save the model into file system.

Parameters

file_name: str
The path and name of the file.

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

MoScoredClassifier

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

Parameters

dataset : object
The dataset to be used for predictions.

prediction_proba_name : str, default=None
The prediction_proba column name in dataset. If None, will use the prediction_proba_name attribute in dataset.

name : str, default=None
The name of the model.

__init__(dataset, prediction_proba_name: str=None, name: str=None)

reset_calibrate_interval()

reset_calibrate_proba()

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

X : X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : array-like of shape (n_samples, )
Target values.

alpha : float, default=0.1
Expected miscoverage for the conformal prediction.

Raises

ValueError: If the model is neither a regressor nor a classifier.

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

X : np.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.

calibrate_proba(X, y, sample_weight=None, method='sigmoid')

Fit the calibration method on the model’s predictions.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

y : np.ndarray of shape (n_samples, )
Ground truth labels.

sample_weight : array-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

predict_proba(X, calibration: bool=True)

Predict (calibrated) probabilities for X.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will return calibrated probability if calibration is done. Otherwise, will return raw probability.

Returns

np.ndarray: The (calibrated) predicted probabilities

predict(X, calibration: bool=True)

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

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns

np.ndarray: The (calibrated) final prediction

decision_function(X, calibration: bool=True)

Computes the decision function for the given input data.

Parameters

X : np.ndarray of shape (n_samples, n_features)
Feature matrix for prediction.

calibration : bool, default=True
If True, will use calibrated probability if calibration is done. Otherwise, will use raw probability.

Returns

logit_prediction : array, shape (n_samples,) or (n_samples, n_classes)
Array of (calibrated) logit predictions.

name()

version()

save(file_name: str)

Save the model into file system.

Parameters

file_name: str
The path and name of the file.

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