{"id":9732,"library":"fairlearn","title":"Fairlearn","description":"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.","status":"active","version":"0.13.0","language":"en","source_language":"en","source_url":"https://github.com/fairlearn/fairlearn","tags":["machine learning","fairness","responsible AI","ethics","scikit-learn","model assessment","bias mitigation"],"install":[{"cmd":"pip install fairlearn","lang":"bash","label":"Install Fairlearn"}],"dependencies":[{"reason":"Core dependency for model training, assessment, and mitigation. Fairlearn APIs are designed to integrate with scikit-learn estimators.","package":"scikit-learn","optional":false},{"reason":"Required for certain data handling and functionality, particularly from v0.11.0 onwards.","package":"pyarrow","optional":false}],"imports":[{"note":"MetricFrame is used for fairness assessment and is located in fairlearn.metrics.","wrong":"from fairlearn.postprocessing import MetricFrame","symbol":"MetricFrame","correct":"from fairlearn.metrics import MetricFrame"},{"note":"GridSearch is one of the reduction-based mitigation algorithms, found in fairlearn.reductions.","wrong":"from fairlearn.mitigation import GridSearch","symbol":"GridSearch","correct":"from fairlearn.reductions import GridSearch"},{"note":"ThresholdOptimizer is a post-processing mitigation technique.","symbol":"ThresholdOptimizer","correct":"from fairlearn.postprocessing import ThresholdOptimizer"},{"note":"CorrelationRemover is a preprocessing mitigation technique.","symbol":"CorrelationRemover","correct":"from fairlearn.preprocessing import CorrelationRemover"},{"note":"Fairlearn provides access to common fairness datasets for examples and testing.","symbol":"fetch_adult","correct":"from fairlearn.datasets import fetch_adult"}],"quickstart":{"code":"import pandas as pd\nfrom fairlearn.metrics import MetricFrame, accuracy_score\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\n\n# 1. Create dummy data\ndata = {\n    'feature_1': [10, 12, 11, 15, 13, 9, 14, 16, 11, 13],\n    'feature_2': [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],\n    'sensitive_feature': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],\n    'target': [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]\n}\ndf = pd.DataFrame(data)\n\nX = df[['feature_1', 'feature_2']]\ny = df['target']\nsensitive_features = df['sensitive_feature']\n\nX_train, X_test, y_train, y_test, sf_train, sf_test = train_test_split(\n    X, y, sensitive_features, test_size=0.5, random_state=42\n)\n\n# 2. Preprocessing pipeline\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', StandardScaler(), ['feature_1']),\n        ('cat', OneHotEncoder(handle_unknown='ignore'), ['feature_2'])\n    ],\n    remainder='passthrough'\n)\n\n# 3. Train a model\nmodel = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('classifier', LogisticRegression(solver='liblinear', random_state=42))\n])\nmodel.fit(X_train, y_train)\n\n# 4. Predict and assess fairness using MetricFrame\ny_pred = model.predict(X_test)\n\nmetric_frame = MetricFrame(\n    metrics=accuracy_score,\n    y_true=y_test,\n    y_pred=y_pred,\n    sensitive_features=sf_test\n)\n\nprint(f\"Overall accuracy: {metric_frame.overall}\")\nprint(f\"Accuracy by sensitive feature group:\\n{metric_frame.by_group}\")\nprint(\"Fairlearn quickstart executed successfully.\")","lang":"python","description":"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`."},"warnings":[{"fix":"Update `MetricFrame` calls to use `metrics=` for a single metric or a dictionary of metrics, and pass all arguments (e.g., `y_true=`, `y_pred=`, `sensitive_features=`) as keyword arguments.","message":"The `MetricFrame` constructor API changed significantly in v0.7.0, with the `metric` argument being renamed to `metrics` and all arguments becoming keyword-only. The old syntax issued a deprecation warning until v0.10.0, after which it became a breaking change.","severity":"breaking","affected_versions":"0.7.0 and later (deprecated until 0.9.0, breaking from 0.10.0)"},{"fix":"Upgrade your Python environment to 3.9 or newer. The current recommended minimum is Python 3.9 as of Fairlearn v0.13.0.","message":"Fairlearn v0.8.0 dropped support for Python 3.6 and 3.7. Attempting to install or run on these Python versions will lead to dependency resolution errors or runtime issues.","severity":"breaking","affected_versions":"0.8.0 and later"},{"fix":"Always consult Fairlearn's `pyproject.toml` or `setup.py` on its GitHub repository for the exact `scikit-learn` version constraints. Install `scikit-learn` within the specified range (e.g., `pip install 'scikit-learn>=1.0,<1.7'`).","message":"Fairlearn has tight dependencies on `scikit-learn`. Mismatched scikit-learn versions can lead to `AttributeError`, `TypeError`, or unexpected behavior during model training or fairness assessment. For example, v0.12.0 added specific compatibility for scikit-learn 1.6.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade Fairlearn to v0.8.0 or later. If upgrading is not possible, avoid passing custom `grid` objects to `GridSearch` in older versions.","message":"Prior to v0.8.0, passing a custom `grid` object to a `GridSearch` reduction could result in a `KeyError` due to an internal bug.","severity":"gotcha","affected_versions":"Prior to 0.8.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install fairlearn` to install the library.","cause":"The Fairlearn library is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'fairlearn'"},{"fix":"Change `metric=` to `metrics=` in your `MetricFrame` constructor call, and ensure all arguments are passed as keyword arguments (e.g., `y_true=y_test`, `sensitive_features=sf_test`).","cause":"You are attempting to use the old `metric` argument for `MetricFrame` after Fairlearn v0.10.0, which expects `metrics` and keyword-only arguments.","error":"TypeError: MetricFrame.__init__() got an unexpected keyword argument 'metric'"},{"fix":"Uninstall your current `scikit-learn` (`pip uninstall scikit-learn`) and then install a compatible version. For example, if Fairlearn requires `>=1.0,<1.7`, run `pip install 'scikit-learn>=1.0,<1.7'`.","cause":"Your installed `scikit-learn` version is outside the range required by your `fairlearn` version, causing dependency conflicts.","error":"fairlearn requires scikit-learn>=X.Y,<A.B but you have scikit-learn C.D"},{"fix":"Ensure `sensitive_features` is passed as a `pandas.Series`, `numpy.ndarray`, or list-like object to the relevant Fairlearn component.","cause":"You did not provide the `sensitive_features` argument to a Fairlearn assessment or mitigation function (e.g., `MetricFrame`, `GridSearch`), where it is a required input.","error":"ValueError: sensitive_features cannot be None"}]}