{"id":3277,"library":"sklearn-compat","title":"sklearn-compat","description":"sklearn-compat is a small Python package designed to help developers write scikit-learn compatible estimators that support multiple scikit-learn versions. It factors out common utilities used by third-party libraries to manage version differences and provide a stable API. As of version 0.1.5, it supports scikit-learn >= 1.2, with recent updates for scikit-learn 1.8 and 1.9. It follows a release cadence tied to new scikit-learn releases, aiming to support scikit-learn versions up to 2 years or about 4 versions.","status":"active","version":"0.1.5","language":"en","source_language":"en","source_url":"https://github.com/sklearn-compat/sklearn-compat","tags":["scikit-learn","compatibility","machine-learning","estimator","development"],"install":[{"cmd":"pip install sklearn-compat","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency; the library provides compatibility layers for scikit-learn versions >= 1.2, up to 1.9.","package":"scikit-learn","optional":false}],"imports":[{"note":"Use this decorator on the `fit` method of custom estimators to handle parameter validation consistently across scikit-learn versions (especially from 1.2+).","symbol":"_fit_context","correct":"from sklearn_compat.base import _fit_context"},{"note":"Use this decorator for functions that require parameter validation consistent with scikit-learn's conventions across different versions.","symbol":"validate_params","correct":"from sklearn_compat.utils import validate_params"},{"note":"Back-ported utility to check if an estimator is a clusterer, ensuring consistent behavior across scikit-learn versions.","symbol":"is_clusterer","correct":"from sklearn_compat.base import is_clusterer"},{"note":"Utility to ensure consistent handling of targets (`y`) across scikit-learn versions, particularly important for changes introduced in scikit-learn 1.8 where it might output 4 parameters.","symbol":"_check_targets","correct":"from sklearn_compat.utils import _check_targets"}],"quickstart":{"code":"from sklearn.base import BaseEstimator, ClassifierMixin\nfrom sklearn_compat.base import _fit_context\nfrom sklearn.utils.validation import check_is_fitted\nimport numpy as np\n\nclass MyCompatibleClassifier(BaseEstimator, ClassifierMixin):\n    # The _fit_context decorator ensures proper parameter validation\n    # and handling consistent with scikit-learn's internal mechanisms\n    # across different versions (e.g., 1.2+).\n    @_fit_context(prefer_skip_nested_validation=True)\n    def fit(self, X, y):\n        if not isinstance(X, np.ndarray):\n            X = np.asarray(X)\n        if not isinstance(y, np.ndarray):\n            y = np.asarray(y)\n\n        self.classes_ = np.unique(y)\n        self.n_features_in_ = X.shape[1]\n        self.is_fitted_ = True\n        return self\n\n    def predict(self, X):\n        check_is_fitted(self)\n        # A simple prediction logic for demonstration\n        return np.full(X.shape[0], self.classes_[0])\n\n# Example usage of the compatible classifier\nX_train = np.array([[1, 2], [3, 4], [5, 6]])\ny_train = np.array([0, 1, 0])\n\nclf = MyCompatibleClassifier()\nclf.fit(X_train, y_train)\nprint(f\"Fitted classes: {clf.classes_}\")\nprint(f\"Predicted for [[7, 8]]: {clf.predict(np.array([[7, 8]]))}\")\n","lang":"python","description":"This quickstart demonstrates how to create a scikit-learn compatible estimator using `sklearn-compat`'s `_fit_context` decorator. This decorator helps developers ensure their custom estimators correctly handle parameter validation and `fit` method behavior across different scikit-learn versions, particularly those after 1.2."},"warnings":[{"fix":"Always import compatibility utilities like `_fit_context` or `validate_params` from `sklearn_compat.base` or `sklearn_compat.utils` instead of directly from `sklearn`'s internal modules.","message":"Directly importing internal utilities from `sklearn` for multi-version support can lead to breaking changes as scikit-learn's internal API is not stable. `sklearn-compat` exists to provide stable compatibility layers.","severity":"breaking","affected_versions":"All scikit-learn versions (when trying to support multiple versions without sklearn-compat)"},{"fix":"Decorate the `fit` method of your custom `BaseEstimator` with `from sklearn_compat.base import _fit_context`. For functions requiring validation, use `from sklearn_compat.utils import validate_params`.","message":"When trying to support `scikit-learn >= 1.2`, parameter validation for estimators and functions changed. Not using `_fit_context` on estimator's `fit` methods or `validate_params` on functions can lead to inconsistent behavior or failures.","severity":"gotcha","affected_versions":"scikit-learn >= 1.2"},{"fix":"Use the `_check_targets` utility from `sklearn_compat.utils` to ensure consistent target checking that adapts to different scikit-learn versions.","message":"Scikit-learn 1.8 introduced changes to internal utilities like `_check_targets`, which now outputs 4 parameters. If your custom code expects a different signature, it will break.","severity":"gotcha","affected_versions":"scikit-learn 1.8+"},{"fix":"`sklearn-compat` provides a compatibility layer, so always use the `sklearn_compat` provided paths for these utilities to avoid needing version checks or conditional imports.","message":"In scikit-learn 1.5, many developer utilities were moved to dedicated modules. Importing them directly by their old paths will fail in newer versions.","severity":"gotcha","affected_versions":"scikit-learn 1.5+"},{"fix":"Decide on a single approach: either install `sklearn-compat` via pip and depend on it, or explicitly vendor a specific version. Do not mix both, and ensure vendored code is kept up-to-date with `sklearn-compat` releases.","message":"The `sklearn-compat` library offers a 'vendored' version in `src/sklearn_compat/_sklearn_compat.py` for those who prefer not to add a direct package dependency. Mixing the vendored version with an installed `sklearn-compat` package or using outdated vendored code can lead to conflicts or missed updates.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}