Intel® Extension for Scikit-learn SVC for Adult dataset

[1]:
from timeit import default_timer as timer
from IPython.display import HTML
from sklearn import metrics
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split

Download the data

[2]:
x, y = fetch_openml(name='a9a', return_X_y=True)

Split the data into train and test sets

[3]:
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

Patch original Scikit-learn with Intel® Extension for Scikit-learn

Intel® Extension for Scikit-learn (previously known as daal4py) contains drop-in replacement functionality for the stock Scikit-learn package. You can take advantage of the performance optimizations of Intel® Extension for Scikit-learn by adding just two lines of code before the usual Scikit-learn imports:

[4]:
from sklearnex import patch_sklearn
patch_sklearn()
Intel(R) Extension for Scikit-learn* enabled (https://github.com/intel/scikit-learn-intelex)

Intel® Extension for Scikit-learn patching affects performance of specific Scikit-learn functionality. Refer to the list of supported algorithms and parameters for details. In cases when unsupported parameters are used, the package fallbacks into original Scikit-learn. If the patching does not cover your scenarios, submit an issue on GitHub.

Training of the SVC algorithm with Intel® Extension for Scikit-learn for Adult dataset

[5]:
from sklearn.svm import SVC

params = {
    'C': 100.0,
    'kernel': 'rbf',
    'gamma': 'scale'
}
start = timer()
classifier = SVC(**params).fit(x_train, y_train)
train_patched = timer() - start
f"Intel® extension for Scikit-learn time: {train_patched:.2f} s"
[5]:
'Intel® extension for Scikit-learn time: 14.08 s'

Predict and get a result of the SVC algorithm with Intel® Extension for Scikit-learn

[6]:
predicted = classifier.predict(x_test)
report = metrics.classification_report(y_test, predicted)
print(f"Classification report for Intel® extension for Scikit-learn SVC:\n{report}\n")
Classification report for Intel® extension for Scikit-learn SVC:
              precision    recall  f1-score   support

        -1.0       0.87      0.90      0.88      7414
         1.0       0.64      0.58      0.61      2355

    accuracy                           0.82      9769
   macro avg       0.76      0.74      0.75      9769
weighted avg       0.82      0.82      0.82      9769


The first column of the classification report above is the class labels.

Train the same algorithm with original Scikit-learn

In order to cancel optimizations, we use unpatch_sklearn and reimport the class SVC.

[7]:
from sklearnex import unpatch_sklearn
unpatch_sklearn()

Training of the SVC algorithm with original Scikit-learn library for Adult dataset

[8]:
from sklearn.svm import SVC

start = timer()
classifier = SVC(**params).fit(x_train, y_train)
train_unpatched = timer() - start
f"Original Scikit-learn time: {train_unpatched:.2f} s"
[8]:
'Original Scikit-learn time: 803.06 s'

Predict and get a result of the SVC algorithm with original Scikit-learn

[9]:
predicted = classifier.predict(x_test)
report = metrics.classification_report(y_test, predicted)
print(f"Classification report for original Scikit-learn SVC:\n{report}\n")
Classification report for original Scikit-learn SVC:
              precision    recall  f1-score   support

        -1.0       0.87      0.90      0.88      7414
         1.0       0.64      0.58      0.61      2355

    accuracy                           0.82      9769
   macro avg       0.76      0.74      0.75      9769
weighted avg       0.82      0.82      0.82      9769


[10]:
HTML(f'<h2>With scikit-learn-intelex patching you can:</h2>'
     f"<ul>"
     f"<li>Use your Scikit-learn code for training and prediction with minimal changes (a couple of lines of code);</li>"
     f"<li>Fast execution training and prediction of Scikit-learn models;</li>"
     f"<li>Get the similar quality</li>"
     f"<li>Get speedup in <strong>{(train_unpatched/train_patched):.1f}</strong> times.</li>"
     f"</ul>")
[10]:

With scikit-learn-intelex patching you can:

  • Use your Scikit-learn code for training and prediction with minimal changes (a couple of lines of code);
  • Fast execution training and prediction of Scikit-learn models;
  • Get the similar quality
  • Get speedup in 57.0 times.