Frouros is a specialized Python library designed for drift detection in machine learning systems. In this article, we will guide you through using Frouros to detect both concept and data drift effectively.
Quickstart: Concept Drift Detection
Let’s dive right into an example using the breast cancer dataset to demonstrate concept drift detection using the Drift Detection Method (DDM).
Step-by-Step Guide
- Import necessary libraries.
- Load the dataset and split it into training and testing sets.
- Define and fit your model with a pipeline.
- Configure and initialize the drift detector.
- Simulate a data stream to detect concept drift.
Code Example
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from frouros.detectors.concept_drift import DDM, DDMConfig
from frouros.metrics import PrequentialError
np.random.seed(seed=31)
# Load and split data
X, y = load_breast_cancer(return_X_y=True)
(X_train, X_test, y_train, y_test) = train_test_split(X, y, train_size=0.7, random_state=31)
# Define and fit your model
pipeline = Pipeline([
('scaler', StandardScaler()),
('model', LogisticRegression())
])
pipeline.fit(X=X_train, y=y_train)
# Configure and instantiate the DDM detector
config = DDMConfig(warning_level=2.0, drift_level=3.0, min_num_instances=25)
detector = DDM(config=config)
# Define metric for accuracy
metric = PrequentialError(alpha=1.0)
def stream_test(X_test, y_test, y, metric, detector):
drift_flag = False
for i, (X, y) in enumerate(zip(X_test, y_test)):
y_pred = pipeline.predict(X.reshape(1, -1))
error = 1 - (y_pred.item() == y.item())
metric_error = metric(error_value=error)
_ = detector.update(value=error)
if detector.status['drift'] and not drift_flag:
drift_flag = True
print(f"Concept drift detected at step {i}. Accuracy: {1 - metric_error:.4f}")
print(f"Final accuracy: {1 - metric_error:.4f}")
# Test the stream - initially no concept drift expected
stream_test(X_test=X_test, y_test=y_test, y=y, metric=metric, detector=detector)
Analogy: Detecting Change in a River’s Flow
Imagine you are a river keeper. Every time the river flows, you take note of its clarity and depth. At first, you measure normally. But suddenly, after a rainstorm (akin to a shift in data), you notice the river has changed its clarity or depth. Just like the river, your model may go through similar changes, known as concept drift. By monitoring and adjusting (just like using DDM), you ensure your river remains healthy and your measurement systems stable!
Data Drift Detection
Now let’s see how to detect data drift using the Kolmogorov-Smirnov Test (KSTest) with the iris dataset.
Implementation Steps
- Import necessary libraries.
- Load the iris dataset and split it into training and testing sets.
- Add noise to simulate data drift.
- Initialize and use KSTest to compare data distributions.
Code Example
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from frouros.detectors.data_drift import KSTest
np.random.seed(seed=31)
# Load and split data
X, y = load_iris(return_X_y=True)
(X_train, X_test, y_train, y_test) = train_test_split(X, y, train_size=0.7, random_state=31)
# Add noise to simulate data drift
feature_idx = 0
X_test[:, feature_idx] += np.random.normal(loc=0.0, scale=3.0, size=X_test.shape[0])
# Fit the model
model = DecisionTreeClassifier(random_state=31)
model.fit(X=X_train, y=y_train)
# Initialize KSTest
detector = KSTest()
_ = detector.fit(X=X_train[:, feature_idx])
# Compare the test data against the trained feature
result, _ = detector.compare(X=X_test[:, feature_idx])
if result.p_value < 0.001:
print(f"Data drift detected at feature {feature_idx}")
else:
print(f"No data drift detected at feature {feature_idx}")
Troubleshooting
- Ensure that you have the required datasets loaded properly. Missing data can lead to misleading results.
- If no drift is detected when expected, double-check the configuration parameters in the drift detection algorithms.
- Make sure that your Python environment has all the necessary libraries installed.
- For any code errors, carefully check syntax and library documentation.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
In this guide, you’ve learned how to effectively use Frouros for both concept and data drift detection. This powerful library will assist you in maintaining the integrity of your machine learning models, ensuring they perform accurately even as data streams evolve.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Further Reading
To gather more detailed examples, you can explore the concept drift examples and data drift examples included in the Frouros documentation.

