nannyml.drift.model_outputs.univariate.statistical.calculator module
Calculates drift for model predictions and model outputs using statistical tests.
- class nannyml.drift.model_outputs.univariate.statistical.calculator.StatisticalOutputDriftCalculator(y_pred: str, timestamp_column_name: str, problem_type: Union[str, ProblemType], y_pred_proba: Optional[Union[str, Dict[str, str]]] = None, 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 model predictions and model outputs using statistical tests.
Creates a new StatisticalOutputDriftCalculator.
- Parameters:
y_pred_proba (ModelOutputsType) – Name(s) of the column(s) containing your model output. Pass a single string when there is only a single model output column, e.g. in binary classification cases. Pass a dictionary when working with multiple output columns, e.g. in multiclass classification cases. The dictionary maps a class/label string to the column name containing model outputs for that class/label.
y_pred (str) – The name of the column containing your model predictions.
timestamp_column_name (str) – The name of the column containing the timestamp of the model prediction.
chunk_size (int, default=None) – 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, default=None) – Splits the data into chunk_number pieces. Only one of chunk_size, chunk_number or chunk_period should be given.
chunk_period (str, default=None) – Splits the data according to the given period. Only one of chunk_size, chunk_number or chunk_period should be given.
chunker (Chunker, default=None) – 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() >>> >>> calc = nml.StatisticalOutputDriftCalculator( >>> y_pred_proba='y_pred_proba', >>> y_pred='y_pred', >>> timestamp_column_name='timestamp' >>> ) >>> calc.fit(reference_df) >>> results = calc.calculate(analysis_df) >>> >>> print(results.data) # check the numbers key start_index ... y_pred_proba_alert y_pred_proba_threshold 0 [0:4999] 0 ... True 0.05 1 [5000:9999] 5000 ... False 0.05 2 [10000:14999] 10000 ... False 0.05 3 [15000:19999] 15000 ... False 0.05 4 [20000:24999] 20000 ... False 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 >>> >>> results.plot(kind='score_drift', metric='p_value', plot_reference=True).show() >>> results.plot(kind='score_distribution', plot_reference=True).show() >>> results.plot(kind='prediction_drift', plot_reference=True).show() >>> results.plot(kind='prediction_distribution', plot_reference=True).show()