nannyml.drift.model_inputs.univariate.statistical.calculator module

Calculates drift for individual features using the Kolmogorov-Smirnov and chi2-contingency statistical tests.

class nannyml.drift.model_inputs.univariate.statistical.calculator.UnivariateStatisticalDriftCalculator(feature_column_names: List[str], timestamp_column_name: str, chunk_size: Optional[int] = None, chunk_number: Optional[int] = None, chunk_period: Optional[str] = None, chunker: Optional[Chunker] = None)[source]

Bases: AbstractCalculator

Calculates drift for individual features using statistical tests.

Creates a new UnivariateStatisticalDriftCalculator instance.

Parameters:
  • feature_column_names (List[str]) – A list containing the names of features in the provided data set. A drift score will be calculated for each entry in this list.

  • timestamp_column_name (str) – The name of the column containing the timestamp of the model prediction.

  • chunk_size (int) – Splits the data into chunks containing chunks_size observations. Only one of chunk_size, chunk_number or chunk_period should be given.

  • chunk_number (int) – Splits the data into chunk_number pieces. Only one of chunk_size, chunk_number or chunk_period should be given.

  • chunk_period (str) – Splits the data according to the given period. Only one of chunk_size, chunk_number or chunk_period should be given.

  • chunker (Chunker) – The Chunker used to split the data sets into a lists of chunks.

Examples

>>> import nannyml as nml
>>>
>>> reference_df, analysis_df, _ = nml.load_synthetic_binary_classification_dataset()
>>>
>>> feature_column_names = [col for col in reference_df.columns
>>>                         if col not in ['y_pred', 'y_pred_proba', 'work_home_actual', 'timestamp']]
>>> calc = nml.UnivariateStatisticalDriftCalculator(
>>>     feature_column_names=feature_column_names,
>>>     timestamp_column_name='timestamp'
>>> )
>>> calc.fit(reference_df)
>>> results = calc.calculate(analysis_df)
>>> print(results.data)  # check the numbers
             key  start_index  ...  identifier_alert identifier_threshold
0       [0:4999]            0  ...              True                 0.05
1    [5000:9999]         5000  ...              True                 0.05
2  [10000:14999]        10000  ...              True                 0.05
3  [15000:19999]        15000  ...              True                 0.05
4  [20000:24999]        20000  ...              True                 0.05
5  [25000:29999]        25000  ...              True                 0.05
6  [30000:34999]        30000  ...              True                 0.05
7  [35000:39999]        35000  ...              True                 0.05
8  [40000:44999]        40000  ...              True                 0.05
9  [45000:49999]        45000  ...              True                 0.05
>>> fig = results.plot(kind='feature_drift', plot_reference=True, feature_column_name='distance_from_office')
>>> fig.show()