{ "cells": [ { "cell_type": "markdown", "id": "f5c4abc0", "metadata": {}, "source": [ "# Intel® Extension for Scikit-learn SVC for Adult dataset" ] }, { "cell_type": "code", "execution_count": 1, "id": "23512089", "metadata": {}, "outputs": [], "source": [ "from timeit import default_timer as timer\n", "from IPython.display import HTML\n", "from sklearn import metrics\n", "from sklearn.datasets import fetch_openml\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "markdown", "id": "2cdcbfa6", "metadata": {}, "source": [ "### Download the data" ] }, { "cell_type": "code", "execution_count": 2, "id": "27b99b44", "metadata": {}, "outputs": [], "source": [ "x, y = fetch_openml(name=\"a9a\", return_X_y=True)" ] }, { "cell_type": "markdown", "id": "3a6df301", "metadata": {}, "source": [ "Split the data into train and test sets" ] }, { "cell_type": "code", "execution_count": 3, "id": "96e14dd7", "metadata": {}, "outputs": [], "source": [ "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)" ] }, { "cell_type": "markdown", "id": "0341cac9", "metadata": {}, "source": [ "### Patch original Scikit-learn with Intel® Extension for Scikit-learn\n", "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:" ] }, { "cell_type": "code", "execution_count": 4, "id": "244c5bc9", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Intel(R) Extension for Scikit-learn* enabled (https://github.com/uxlfoundation/scikit-learn-intelex)\n" ] } ], "source": [ "from sklearnex import patch_sklearn\n", "\n", "patch_sklearn()" ] }, { "cell_type": "markdown", "id": "6bb14ac8", "metadata": {}, "source": [ "Intel® Extension for Scikit-learn patching affects performance of specific Scikit-learn functionality. Refer to the [list of supported algorithms and parameters](https://uxlfoundation.github.io/scikit-learn-intelex/algorithms.html) 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](https://github.com/uxlfoundation/scikit-learn-intelex/issues)." ] }, { "cell_type": "markdown", "id": "693b4e26", "metadata": {}, "source": [ "Training of the SVC algorithm with Intel® Extension for Scikit-learn for Adult dataset" ] }, { "cell_type": "code", "execution_count": 5, "id": "e9b8f06b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Intel® extension for Scikit-learn time: 14.08 s'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.svm import SVC\n", "\n", "params = {\"C\": 100.0, \"kernel\": \"rbf\", \"gamma\": \"scale\"}\n", "start = timer()\n", "classifier = SVC(**params).fit(x_train, y_train)\n", "train_patched = timer() - start\n", "f\"Intel® extension for Scikit-learn time: {train_patched:.2f} s\"" ] }, { "cell_type": "markdown", "id": "d01cdabc", "metadata": {}, "source": [ "Predict and get a result of the SVC algorithm with Intel® Extension for Scikit-learn" ] }, { "cell_type": "code", "execution_count": 6, "id": "9ead2a44", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Classification report for Intel® extension for Scikit-learn SVC:\n", " precision recall f1-score support\n", "\n", " -1.0 0.87 0.90 0.88 7414\n", " 1.0 0.64 0.58 0.61 2355\n", "\n", " accuracy 0.82 9769\n", " macro avg 0.76 0.74 0.75 9769\n", "weighted avg 0.82 0.82 0.82 9769\n", "\n", "\n" ] } ], "source": [ "predicted = classifier.predict(x_test)\n", "report = metrics.classification_report(y_test, predicted)\n", "print(f\"Classification report for Intel® extension for Scikit-learn SVC:\\n{report}\\n\")" ] }, { "cell_type": "markdown", "id": "bd8e7b0b", "metadata": {}, "source": [ "*The first column of the classification report above is the class labels.* \n", " \n", "### Train the same algorithm with original Scikit-learn\n", "In order to cancel optimizations, we use *unpatch_sklearn* and reimport the class SVC." ] }, { "cell_type": "code", "execution_count": 7, "id": "5bb884d5", "metadata": {}, "outputs": [], "source": [ "from sklearnex import unpatch_sklearn\n", "\n", "unpatch_sklearn()" ] }, { "cell_type": "markdown", "id": "8cfa0dba", "metadata": {}, "source": [ "Training of the SVC algorithm with original Scikit-learn library for Adult dataset" ] }, { "cell_type": "code", "execution_count": 8, "id": "ae421d8e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Original Scikit-learn time: 803.06 s'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.svm import SVC\n", "\n", "start = timer()\n", "classifier = SVC(**params).fit(x_train, y_train)\n", "train_unpatched = timer() - start\n", "f\"Original Scikit-learn time: {train_unpatched:.2f} s\"" ] }, { "cell_type": "markdown", "id": "c0a7a747", "metadata": {}, "source": [ "Predict and get a result of the SVC algorithm with original Scikit-learn" ] }, { "cell_type": "code", "execution_count": 9, "id": "7644999d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Classification report for original Scikit-learn SVC:\n", " precision recall f1-score support\n", "\n", " -1.0 0.87 0.90 0.88 7414\n", " 1.0 0.64 0.58 0.61 2355\n", "\n", " accuracy 0.82 9769\n", " macro avg 0.76 0.74 0.75 9769\n", "weighted avg 0.82 0.82 0.82 9769\n", "\n", "\n" ] } ], "source": [ "predicted = classifier.predict(x_test)\n", "report = metrics.classification_report(y_test, predicted)\n", "print(f\"Classification report for original Scikit-learn SVC:\\n{report}\\n\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "fc992182", "metadata": {}, "outputs": [ { "data": { "text/html": [ "