skoot.model_validation.CustomValidator

class skoot.model_validation.CustomValidator(cols=None, as_df=True, func=None, action='warn')[source][source]

Validate test features given custom functions.

Apply test set validator behavior over custom functions. This can be especially useful in cases where a feature should never exhibit values within a certain range (i.e., sensor data).

Parameters:

cols : array-like, shape=(n_features,)

The names of the columns on which to apply the transformation. If cols is None, will apply to the entire dataframe.

as_df : bool, optional (default=True)

Whether to return a Pandas DataFrame in the transform method. If False, will return a Numpy ndarray instead. Since most skoot transformers depend on explicitly-named DataFrame features, the as_df parameter is True by default.

func : callable or iterable, optional (default=None)

The function used to validate the feature. Can be as complex or as simple as needed, but must adhere to the following criteria:

  • The signature must accept a single vector
  • The output must be a boolean

Note also that providing a lambda expression as a function can prove to be problematic when it comes time to serialize your class, as lambda expressions cannot be serialized via pickle. It’s best to provide a def-style function or closure.

action : str or unicode, optional (default=”warn”)

The default action for handling validation mismatches. Options include “warn”, “raise” or “ignore”. If action is “raise”, will raise a ValueError if mismatched.

Attributes

func_dict_ (dict) A dictionary mapping the column names to their respective validation function.
fit_cols_ (list) The list of column names on which the transformer was fit. This is used to validate the presence of the features in the test set during the transform stage.

Methods

fit(X[, y]) Fit the transformer.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
transform(X) Validate the features in the test dataframe.
__init__(cols=None, as_df=True, func=None, action='warn')[source][source]

Initialize self. See help(type(self)) for accurate signature.

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

Fit the transformer.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to fit. The frame will only be fit on the prescribed cols (see __init__) or all if cols is None.

y : array-like or None, shape=(n_samples,), optional (default=None)

Pass-through for sklearn.pipeline.Pipeline.

fit_transform(X, y=None, **fit_params)[source]

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:

X : numpy array of shape [n_samples, n_features]

Training set.

y : numpy array of shape [n_samples]

Target values.

Returns:

X_new : numpy array of shape [n_samples, n_features_new]

Transformed array.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:

deep : boolean, optional

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

Returns:

params : mapping of string to any

Parameter names mapped to their values.

set_params(**params)[source]

Set the parameters of this estimator.

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

Returns:self
transform(X)[source]

Validate the features in the test dataframe.

This method will apply the validation test over each prescribed feature, and raise or warn appropriately.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to validate. The operation will be applied to a copy of the input data, and the result will be returned.

Returns:

X : pd.DataFrame or np.ndarray, shape=(n_samples, n_features)

The operation is applied to a copy of X, and the result set is returned.