{ "cells": [ { "cell_type": "markdown", "id": "f5c4abc0", "metadata": {}, "source": [ "# Intel® Extension for Scikit-learn Random Forest for Yolanda dataset" ] }, { "cell_type": "code", "execution_count": 1, "id": "23512089", "metadata": {}, "outputs": [], "source": [ "from timeit import default_timer as timer\n", "from sklearn import metrics\n", "from IPython.display import HTML\n", "from sklearn.datasets import fetch_openml\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "markdown", "id": "7d0b6bb9", "metadata": {}, "source": [ "### Download the data" ] }, { "cell_type": "code", "execution_count": 2, "id": "27b99b44", "metadata": {}, "outputs": [], "source": [ "x, y = fetch_openml(name=\"Yolanda\", return_X_y=True)" ] }, { "cell_type": "markdown", "id": "5b3a2483", "metadata": {}, "source": [ "Split the data into train and test sets" ] }, { "cell_type": "code", "execution_count": 3, "id": "96e14dd7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((280000, 100), (120000, 100), (280000,), (120000,))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=72)\n", "x_train.shape, x_test.shape, y_train.shape, y_test.shape" ] }, { "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/latest/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 Random Forest algorithm with Intel® Extension for Scikit-learn for Yolanda dataset" ] }, { "cell_type": "code", "execution_count": 5, "id": "8fecbbb1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Intel® extension for Scikit-learn time: 42.56 s'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.ensemble import RandomForestRegressor\n", "\n", "params = {\"n_estimators\": 150, \"random_state\": 44, \"n_jobs\": -1}\n", "start = timer()\n", "rf = RandomForestRegressor(**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": "d9279181", "metadata": {}, "source": [ "Predict and get a result of the Random Forest algorithm with Intel® Extension for Scikit-learn" ] }, { "cell_type": "code", "execution_count": 6, "id": "d05bc57b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Intel® extension for Scikit-learn Mean Squared Error: 83.62232345666878'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred = rf.predict(x_test)\n", "mse_opt = metrics.mean_squared_error(y_test, y_pred)\n", "f\"Intel® extension for Scikit-learn Mean Squared Error: {mse_opt}\"" ] }, { "cell_type": "markdown", "id": "bd8e7b0b", "metadata": {}, "source": [ "### Train the same algorithm with original Scikit-learn\n", "In order to cancel optimizations, we use *unpatch_sklearn* and reimport the class RandomForestRegressor." ] }, { "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 Random Forest algorithm with original Scikit-learn library for Yolanda dataset" ] }, { "cell_type": "code", "execution_count": 8, "id": "76a8d5f1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Original Scikit-learn time: 123.34 s'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.ensemble import RandomForestRegressor\n", "\n", "start = timer()\n", "rf = RandomForestRegressor(**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": "f162fe6b", "metadata": {}, "source": [ "Predict and get a result of the Random Forest algorithm with original Scikit-learn" ] }, { "cell_type": "code", "execution_count": 9, "id": "d5b5e45c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Original Scikit-learn Mean Squared Error: 83.62232345666878'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred = rf.predict(x_test)\n", "mse_original = metrics.mean_squared_error(y_test, y_pred)\n", "f\"Original Scikit-learn Mean Squared Error: {mse_opt}\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "e255e563", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

Compare MSE metric of patched Scikit-learn and original

MSE metric of patched Scikit-learn: 83.62232345666878
MSE metric of unpatched Scikit-learn: 83.80131297814816
Metrics ratio: 0.9978641203208111

With Scikit-learn-intelex patching you can:

" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(\n", " f\"

Compare MSE metric of patched Scikit-learn and original

\"\n", " f\"MSE metric of patched Scikit-learn: {mse_opt}
\"\n", " f\"MSE metric of unpatched Scikit-learn: {mse_original}
\"\n", " f\"Metrics ratio: {mse_opt/mse_original}
\"\n", " f\"

With Scikit-learn-intelex patching you can:

\"\n", " f\"\"\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }