Fairlearn

0.13.0 · active · verified Fri Apr 17

Fairlearn is a Python package designed to help data scientists and developers assess and improve the fairness of machine learning models. It provides algorithms for fairness assessment and mitigation, integrating seamlessly with scikit-learn pipelines. The current version is 0.13.0, and it maintains an active release cadence with minor updates and improvements released every few months.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use Fairlearn's `MetricFrame` to assess fairness. It involves creating a simple dataset, training a scikit-learn model, and then evaluating performance metrics across different sensitive feature groups using `MetricFrame`.

import pandas as pd
from fairlearn.metrics import MetricFrame, accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline

# 1. Create dummy data
data = {
    'feature_1': [10, 12, 11, 15, 13, 9, 14, 16, 11, 13],
    'feature_2': [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
    'sensitive_feature': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
    'target': [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]
}
df = pd.DataFrame(data)

X = df[['feature_1', 'feature_2']]
y = df['target']
sensitive_features = df['sensitive_feature']

X_train, X_test, y_train, y_test, sf_train, sf_test = train_test_split(
    X, y, sensitive_features, test_size=0.5, random_state=42
)

# 2. Preprocessing pipeline
preprocessor = ColumnTransformer(
    transformers=[
        ('num', StandardScaler(), ['feature_1']),
        ('cat', OneHotEncoder(handle_unknown='ignore'), ['feature_2'])
    ],
    remainder='passthrough'
)

# 3. Train a model
model = Pipeline(steps=[
    ('preprocessor', preprocessor),
    ('classifier', LogisticRegression(solver='liblinear', random_state=42))
])
model.fit(X_train, y_train)

# 4. Predict and assess fairness using MetricFrame
y_pred = model.predict(X_test)

metric_frame = MetricFrame(
    metrics=accuracy_score,
    y_true=y_test,
    y_pred=y_pred,
    sensitive_features=sf_test
)

print(f"Overall accuracy: {metric_frame.overall}")
print(f"Accuracy by sensitive feature group:\n{metric_frame.by_group}")
print("Fairlearn quickstart executed successfully.")

view raw JSON →