Quick Start

You may enable patching in different ways:

Important

These patching methods are interchangeable. They support different enabling scenarios while producing the same result.

  • Without editing the code of a scikit-learn application by using the following command line flag:

    python -m sklearnex my_application.py
    
  • Directly from the script:

    from sklearnex import patch_sklearn
    patch_sklearn()
    

Important

You have to import scikit-learn after these lines. Otherwise, the patching will not affect the original scikit-learn estimators.

  • Through importing the desired estimator from the sklearnex module in your script:

    from sklearnex.neighbors import NearestNeighbors
    
  • Through global patching to enable patching for your scikit-learn installation for all further runs:

    python -m sklearnex.glob patch_sklearn
    

Example

import numpy as np
from sklearnex import patch_sklearn
patch_sklearn()

# You need to re-import scikit-learn algorithms after the patch
from sklearn.cluster import KMeans

X = np.array([[1,  2], [1,  4], [1,  0],
              [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
print(f"kmeans.labels_ = {kmeans.labels_}")

In the example above, you can see that the use of the original Scikit-learn has not changed. This behavior is achieved through drop-in patching.

To undo the patch, run:

sklearnex.unpatch_sklearn()
# Re-import scikit-learn algorithms after the unpatch:
from sklearn.cluster import KMeans

You may specify which algorithms to patch:

  • Patching only one algorithm:

    from sklearnex import patch_sklearn
    # The names match scikit-learn estimators
    patch_sklearn("SVC")
    
  • Patching several algorithms:

    from sklearnex import patch_sklearn
    # The names match scikit-learn estimators
    patch_sklearn(["SVC", "DBSCAN"])