Patching

patching

To patch scikit-learn with Intel® Extension for Scikit-learn* is to replace stock scikit-learn algorithms with their optimized versions provided by the extension. You can always undo the patch.

There are different ways to patch scikit-learn:

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 sklearnex.glob patch_sklearn
    

See also

Quick Start

global pathcing

One of the patching options available in Intel® Extension for Scikit-learn*. With global patching, you can patch all scikit-learn applications at once:

python sklearnex.glob patch_sklearn

See also

Global Patching

unpatching

To undo the patch is to return to the use of original scikit-learn implementation and replace patched algorithms with the stock scikit-learn algorithms. Unpatching requires scikit-learn to be re-imported again:

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

How it works

The extension replaces the original estimators in scikit-learn modules with the optimized ones. If the desired algorithm parameters are not supported by the Intel® Extension for Scikit-learn*, then the result of the original scikit-learn is returned.

See also

Verbose Mode