Intel® Extension for Scikit-learn Ridge Regression for Airlines DepDelay dataset
[1]:
from timeit import default_timer as timer
from sklearn import metrics
from sklearn.model_selection import train_test_split
import warnings
from sklearn.datasets import fetch_openml
from sklearn.preprocessing import LabelEncoder
from IPython.display import HTML
warnings.filterwarnings("ignore")
Download the data
[2]:
x, y = fetch_openml(name="Airlines_DepDelay_10M", return_X_y=True)
Preprocessing
Let’s encode categorical features with LabelEncoder
[3]:
for col in ["UniqueCarrier", "Origin", "Dest"]:
le = LabelEncoder().fit(x[col])
x[col] = le.transform(x[col])
Split the data into train and test sets
[4]:
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=0)
x_train.shape, x_test.shape, y_train.shape, y_test.shape
[4]:
((9000000, 9), (1000000, 9), (9000000,), (1000000,))
Normalize the data
[5]:
from sklearn.preprocessing import MinMaxScaler, StandardScaler
scaler_x = MinMaxScaler()
scaler_y = StandardScaler()
[6]:
y_train = y_train.to_numpy().reshape(-1, 1)
y_test = y_test.to_numpy().reshape(-1, 1)
scaler_x.fit(x_train)
x_train = scaler_x.transform(x_train)
x_test = scaler_x.transform(x_test)
scaler_y.fit(y_train)
y_train = scaler_y.transform(y_train).ravel()
y_test = scaler_y.transform(y_test).ravel()
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:
[7]:
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 Ridge Regression algorithm with Intel® Extension for Scikit-learn for Airlines DepDelay dataset
[8]:
from sklearn.linear_model import Ridge
params = {
"alpha": 0.3,
"fit_intercept": False,
"random_state": 0,
"copy_X": False,
}
start = timer()
model = Ridge(random_state=0).fit(x_train, y_train)
train_patched = timer() - start
f"Intel® extension for Scikit-learn time: {train_patched:.2f} s"
[8]:
'Intel® extension for Scikit-learn time: 0.06 s'
Predict and get a result of the Ridge Regression algorithm with Intel® Extension for Scikit-learn
[9]:
y_predict = model.predict(x_test)
mse_metric_opt = metrics.mean_squared_error(y_test, y_predict)
f"Patched Scikit-learn MSE: {mse_metric_opt}"
[9]:
'Patched Scikit-learn MSE: 1.0014288520708046'
Train the same algorithm with original Scikit-learn
In order to cancel optimizations, we use unpatch_sklearn and reimport the class Ridge
[10]:
from sklearnex import unpatch_sklearn
unpatch_sklearn()
Training of the Ridge Regression algorithm with original Scikit-learn library for Airlines DepDelay dataset
[11]:
from sklearn.linear_model import Ridge
start = timer()
model = Ridge(random_state=0).fit(x_train, y_train)
train_unpatched = timer() - start
f"Original Scikit-learn time: {train_unpatched:.2f} s"
[11]:
'Original Scikit-learn time: 0.70 s'
Predict and get a result of the Ridge Regression algorithm with original Scikit-learn
[12]:
y_predict = model.predict(x_test)
mse_metric_original = metrics.mean_squared_error(y_test, y_predict)
f"Original Scikit-learn MSE: {mse_metric_original}"
[12]:
'Original Scikit-learn MSE: 1.0014288520708057'
[13]:
HTML(
f"<h3>Compare MSE metric of patched Scikit-learn and original</h3>"
f"MSE metric of patched Scikit-learn: {mse_metric_opt} <br>"
f"MSE metric of unpatched Scikit-learn: {mse_metric_original} <br>"
f"Metrics ratio: {mse_metric_opt/mse_metric_original} <br>"
f"<h3>With Scikit-learn-intelex patching you can:</h3>"
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>"
)
[13]:
Compare MSE metric of patched Scikit-learn and original
MSE metric of patched Scikit-learn: 1.0014288520708046MSE metric of unpatched Scikit-learn: 1.0014288520708057
Metrics ratio: 0.9999999999999989
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 10.9 times.