Created
July 29, 2026 19:24
-
-
Save rehan243/eb283d07d96753611881ad86fbe23cde to your computer and use it in GitHub Desktop.
Data Drift Detector using KS-Test — Production Pattern (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Author: Rehan Malik | Senior AI/ML Engineer | |
| This script implements a Data Drift Detector using the Kolmogorov-Smirnov (KS) Test. | |
| It compares two datasets (reference and current) to identify distribution shifts | |
| and outputs the drift results for each feature. | |
| Usage: | |
| - Replace or extend the demo in the `__main__` block with your datasets. | |
| """ | |
| from typing import List, Tuple, Dict | |
| from scipy.stats import ks_2samp | |
| from dataclasses import dataclass | |
| import pandas as pd | |
| import numpy as np | |
| @dataclass | |
| class DriftResult: | |
| feature: str | |
| p_value: float | |
| statistic: float | |
| drift_detected: bool | |
| class DataDriftDetector: | |
| def __init__(self, threshold: float = 0.05): | |
| """ | |
| Initialize the Data Drift Detector. | |
| :param threshold: p-value threshold for drift detection. Defaults to 0.05. | |
| """ | |
| self.threshold = threshold | |
| def detect_drift( | |
| self, reference_data: pd.DataFrame, current_data: pd.DataFrame | |
| ) -> List[DriftResult]: | |
| """ | |
| Detects data drift between reference and current datasets. | |
| :param reference_data: DataFrame containing reference data. | |
| :param current_data: DataFrame containing current data. | |
| :return: List of DriftResult objects for each feature. | |
| """ | |
| if set(reference_data.columns) != set(current_data.columns): | |
| raise ValueError("Both datasets must have the same columns.") | |
| results = [] | |
| for column in reference_data.columns: | |
| ref_col = reference_data[column].dropna() | |
| curr_col = current_data[column].dropna() | |
| if ref_col.empty or curr_col.empty: | |
| continue | |
| # Perform KS-Test | |
| stat, p_value = ks_2samp(ref_col, curr_col) | |
| drift_detected = p_value < self.threshold | |
| results.append( | |
| DriftResult( | |
| feature=column, | |
| p_value=p_value, | |
| statistic=stat, | |
| drift_detected=drift_detected | |
| ) | |
| ) | |
| return results | |
| def summarize_results(self, results: List[DriftResult]) -> Dict[str, int]: | |
| """ | |
| Summarize drift detection results. | |
| :param results: List of DriftResult objects. | |
| :return: Summary of drift results as a dictionary. | |
| """ | |
| drifted_features = sum(result.drift_detected for result in results) | |
| return { | |
| "total_features": len(results), | |
| "drifted_features": drifted_features, | |
| } | |
| if __name__ == "__main__": | |
| # Generate sample data for demonstration | |
| np.random.seed(42) | |
| ref_data = pd.DataFrame({ | |
| "feature1": np.random.normal(0, 1, 1000), | |
| "feature2": np.random.normal(5, 2, 1000), | |
| }) | |
| curr_data = pd.DataFrame({ | |
| "feature1": np.random.normal(0, 1, 1000), # No drift | |
| "feature2": np.random.normal(6, 2, 1000), # Drift | |
| }) | |
| # Initialize and run detector | |
| detector = DataDriftDetector(threshold=0.05) | |
| drift_results = detector.detect_drift(ref_data, curr_data) | |
| # Print detailed drift results | |
| for result in drift_results: | |
| print(f"Feature: {result.feature}, p-value: {result.p_value:.5f}, " | |
| f"statistic: {result.statistic:.5f}, Drift Detected: {result.drift_detected}") | |
| # Print summary | |
| summary = detector.summarize_results(drift_results) | |
| print("\nDrift Detection Summary:", summary) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment