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

Use global patching to patch all your scikit-learn applications without any additional actions.

Prerequisites

  • Intel® Extension for Scikit-learn*

  • Scikit-learn

  • read and write permissions to Scikit-learn files

To patch all supported algorithms, run:

python -m sklearnex.glob patch_sklearn

If you want to patch only some algorithms, use --algorithm or -a keys with a list of algorithms to patch.

For example, to patch only SVC and RandomForestClassifier estimators, run:

python -m sklearnex.glob patch_sklearn -a svc random_forest_classifier

If you do not want to receive patching notifications, then use --no-verbose or -nv keys:

python -m sklearnex.glob patch_sklearn -a svc random_forest_classifier -nv

Note

If you run the global patching command several times with different parameters, then only the last configuration will be applied.

To disable global patching, use the following command:

python -m sklearnex.glob unpatch_sklearn

You can also enable global patching in your code. To do this, use the patch_sklearn function with the global_patch argument:

from sklearnex import patch_sklearn
patch_sklearn(global_patch=True)
import sklearn

After that, Scikit-learn patches will be enabled in the current application and in all others that use the same environment.

To disable global patching via code, use the global_patch argument in the unpatch_sklearn function:

from sklearnex import unpatch_sklearn
unpatch_sklearn(global_patch=True)

Note

If you clone an environment with enabled global patching, it will already be applied in the new environment.

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