certifai.model.sdk.pandas_adapter module

Provides an abstraction for packaging a model that expects as input a pandas.DataFrame, as a flask application.

class certifai.model.sdk.pandas_adapter.PandasModelWrapper(pandas_kwargs: Optional[dict] = None, **kwargs)

Provides a Flask app that runs a single model. It is optimized for models that accept as input a pandas.DataFrame of instances from the dataset, and returns an array-like object of predictions. The expected output of the model can be any type of Iterable, such as a list, numpy array, pandas DataFrame, or pandas Series.

If an encoder is set, then it will also receive as input a pandas.DataFrame.

Parameters for creating the `pandas.DataFrame` can be specified in the `pandas_kwargs` dictionary. Refer to the pandas documentation for available keyword arguments. For example,

m = PandasModelWrapper(model=model, pandas_kwargs={'columns': ['a', 'b', 'c', 'd']})
Parameters
  • pandas_kwargs – Dictionary with keyword arguments to provided to the pandas.DataFrame constructor, such as: columns, dtype, copy, or index.

  • kwargs – Keyword arguments for configuring the prediction service. Refer to the parameters of the SimpleModelWrapper.

predict_raw(instances: List) certifai.model.sdk.simple_wrapper.PredictResponse

Override this method if the model doesn’t use pandas DataFrame’s for prediction input.

Parameters

instances (List) – {array-like, list} of data instances of shape (n_samples, n_features)

Returns

NamedTuple (PredictResponse) of model predictions, scores, labels and threshold

Return type

PredictResponse

NamedTuple(predictions: np.ndarray
           scores:      Optional[np.ndarray]
           labels:      Optional[list]
           threshold:   Optional[float]
           )

predict(df: pandas.core.frame.DataFrame) numpy.ndarray

Override this method to change the way the model is called. The default implementation calls model.predict(df).

Parameters

dfDataFrame of shape (n_samples, n_features) to predict on

Returns

array-like collection of model predictions of shape (n_samples,).

Return type

np.ndarray

soft_predict(df: pandas.core.frame.DataFrame) numpy.ndarray

Computes soft scores along with ordered list of score labels if supports_soft_scores is enabled. Override this method to change to how soft scores are computed. The default implementation calls model.predict_proba(df).

Parameters

dfDataFrame of shape (n_samples, n_features) to predict on.

Returns

model predict scores in an array-like collection of shape (n_samples,n_classes)

Return type

np.ndarray

certifai.model.sdk.pandas_adapter.to_numpy(result: Union[numpy.ndarray, pandas.core.frame.DataFrame, pandas.core.series.Series, Iterable]) numpy.ndarray

Utility for converting array-like types to a numpy array.