Skip to content

Commit 73d61ec

Browse files
authored
New examples (#812)
1 parent 737bcd3 commit 73d61ec

File tree

5 files changed

+1872
-0
lines changed

5 files changed

+1872
-0
lines changed
Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "3768ec43",
6+
"metadata": {},
7+
"source": [
8+
"# Intel® Extension for Scikit-learn ElasticNet for Airlines DepDelay dataset"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "b1b922d1",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from timeit import default_timer as timer\n",
19+
"from sklearn import metrics\n",
20+
"from sklearn.model_selection import train_test_split\n",
21+
"import warnings\n",
22+
"from sklearn.datasets import fetch_openml\n",
23+
"from sklearn.preprocessing import LabelEncoder\n",
24+
"from IPython.display import HTML\n",
25+
"warnings.filterwarnings('ignore')"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"id": "34e460a7",
31+
"metadata": {},
32+
"source": [
33+
"### Download the data"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 2,
39+
"id": "00c2277b",
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"x, y = fetch_openml(name='Airlines_DepDelay_10M', return_X_y=True)"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"id": "06d309c0",
49+
"metadata": {},
50+
"source": [
51+
"### Preprocessing\n",
52+
"Let's encode categorical features with LabelEncoder"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 3,
58+
"id": "2ff35bc2",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"for col in ['UniqueCarrier', 'Origin', 'Dest']:\n",
63+
" le = LabelEncoder().fit(x[col])\n",
64+
" x[col] = le.transform(x[col])"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"id": "38637349",
70+
"metadata": {},
71+
"source": [
72+
"Split the data into train and test sets"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 4,
78+
"id": "0d332789",
79+
"metadata": {},
80+
"outputs": [
81+
{
82+
"data": {
83+
"text/plain": [
84+
"((9000000, 9), (1000000, 9), (9000000,), (1000000,))"
85+
]
86+
},
87+
"execution_count": 4,
88+
"metadata": {},
89+
"output_type": "execute_result"
90+
}
91+
],
92+
"source": [
93+
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=0)\n",
94+
"x_train.shape, x_test.shape, y_train.shape, y_test.shape"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"id": "246f819f",
100+
"metadata": {},
101+
"source": [
102+
"Normalize the data"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 5,
108+
"id": "454a341c",
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"from sklearn.preprocessing import StandardScaler\n",
113+
"scaler_y = StandardScaler()"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 6,
119+
"id": "df400504",
120+
"metadata": {},
121+
"outputs": [],
122+
"source": [
123+
"y_train = y_train.to_numpy().reshape(-1, 1)\n",
124+
"y_test = y_test.to_numpy().reshape(-1, 1)\n",
125+
"\n",
126+
"scaler_y.fit(y_train)\n",
127+
"y_train = scaler_y.transform(y_train).ravel()\n",
128+
"y_test = scaler_y.transform(y_test).ravel()"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"id": "fe1d4fac",
134+
"metadata": {},
135+
"source": [
136+
"### Patch original Scikit-learn with Intel® Extension for Scikit-learn\n",
137+
"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:"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 7,
143+
"id": "ef6938df",
144+
"metadata": {},
145+
"outputs": [
146+
{
147+
"name": "stderr",
148+
"output_type": "stream",
149+
"text": [
150+
"Intel(R) Extension for Scikit-learn* enabled (https://github.com/intel/scikit-learn-intelex)\n"
151+
]
152+
}
153+
],
154+
"source": [
155+
"from sklearnex import patch_sklearn\n",
156+
"patch_sklearn()"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"id": "20c5ab48",
162+
"metadata": {},
163+
"source": [
164+
"Intel® Extension for Scikit-learn patching affects performance of specific Scikit-learn functionality. Refer to the [list of supported algorithms and parameters](https://intel.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/intel/scikit-learn-intelex/issues)."
165+
]
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"id": "f80273e7",
170+
"metadata": {},
171+
"source": [
172+
"Training of the ElasticNet algorithm with Intel® Extension for Scikit-learn for Airlines DepDelay dataset"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 8,
178+
"id": "a4dd1c7e",
179+
"metadata": {},
180+
"outputs": [
181+
{
182+
"data": {
183+
"text/plain": [
184+
"'Intel® extension for Scikit-learn time: 0.28 s'"
185+
]
186+
},
187+
"execution_count": 8,
188+
"metadata": {},
189+
"output_type": "execute_result"
190+
}
191+
],
192+
"source": [
193+
"from sklearn.linear_model import ElasticNet\n",
194+
"\n",
195+
"params = {\n",
196+
" \"alpha\": 0.3, \n",
197+
" \"fit_intercept\": False,\n",
198+
" \"l1_ratio\": 0.7,\n",
199+
" \"random_state\": 0,\n",
200+
" \"copy_X\": False,\n",
201+
"}\n",
202+
"start = timer()\n",
203+
"model = ElasticNet(**params).fit(x_train, y_train)\n",
204+
"train_patched = timer() - start\n",
205+
"f\"Intel® extension for Scikit-learn time: {train_patched:.2f} s\""
206+
]
207+
},
208+
{
209+
"cell_type": "markdown",
210+
"id": "f10b51fc",
211+
"metadata": {},
212+
"source": [
213+
"Predict and get a result of the ElasticNet algorithm with Intel® Extension for Scikit-learn"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 9,
219+
"id": "d4295a26",
220+
"metadata": {},
221+
"outputs": [
222+
{
223+
"data": {
224+
"text/plain": [
225+
"'Patched Scikit-learn MSE: 1.0109113399224974'"
226+
]
227+
},
228+
"execution_count": 9,
229+
"metadata": {},
230+
"output_type": "execute_result"
231+
}
232+
],
233+
"source": [
234+
"y_predict = model.predict(x_test)\n",
235+
"mse_metric_opt = metrics.mean_squared_error(y_test, y_predict)\n",
236+
"f'Patched Scikit-learn MSE: {mse_metric_opt}'"
237+
]
238+
},
239+
{
240+
"cell_type": "markdown",
241+
"id": "cbe6db0d",
242+
"metadata": {},
243+
"source": [
244+
"### Train the same algorithm with original Scikit-learn\n",
245+
"In order to cancel optimizations, we use *unpatch_sklearn* and reimport the class ElasticNet"
246+
]
247+
},
248+
{
249+
"cell_type": "code",
250+
"execution_count": 10,
251+
"id": "6f64ba97",
252+
"metadata": {},
253+
"outputs": [],
254+
"source": [
255+
"from sklearnex import unpatch_sklearn\n",
256+
"unpatch_sklearn()"
257+
]
258+
},
259+
{
260+
"cell_type": "markdown",
261+
"id": "f242c6da",
262+
"metadata": {},
263+
"source": [
264+
"Training of the ElasticNet algorithm with original Scikit-learn library for Airlines DepDelay dataset"
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": 11,
270+
"id": "67243849",
271+
"metadata": {},
272+
"outputs": [
273+
{
274+
"data": {
275+
"text/plain": [
276+
"'Original Scikit-learn time: 3.96 s'"
277+
]
278+
},
279+
"execution_count": 11,
280+
"metadata": {},
281+
"output_type": "execute_result"
282+
}
283+
],
284+
"source": [
285+
"from sklearn.linear_model import ElasticNet\n",
286+
"\n",
287+
"start = timer()\n",
288+
"model = ElasticNet(**params).fit(x_train, y_train)\n",
289+
"train_unpatched = timer() - start\n",
290+
"f\"Original Scikit-learn time: {train_unpatched:.2f} s\""
291+
]
292+
},
293+
{
294+
"cell_type": "markdown",
295+
"id": "c85a125c",
296+
"metadata": {},
297+
"source": [
298+
"Predict and get a result of the ElasticNet algorithm with original Scikit-learn"
299+
]
300+
},
301+
{
302+
"cell_type": "code",
303+
"execution_count": 12,
304+
"id": "cd9e726c",
305+
"metadata": {},
306+
"outputs": [
307+
{
308+
"data": {
309+
"text/plain": [
310+
"'Original Scikit-learn MSE: 1.0109113399545733'"
311+
]
312+
},
313+
"execution_count": 12,
314+
"metadata": {},
315+
"output_type": "execute_result"
316+
}
317+
],
318+
"source": [
319+
"y_predict = model.predict(x_test)\n",
320+
"mse_metric_original = metrics.mean_squared_error(y_test, y_predict)\n",
321+
"f'Original Scikit-learn MSE: {mse_metric_original}'"
322+
]
323+
},
324+
{
325+
"cell_type": "code",
326+
"execution_count": 13,
327+
"id": "a2edbb65",
328+
"metadata": {},
329+
"outputs": [
330+
{
331+
"data": {
332+
"text/html": [
333+
"<h3>Compare MSE metric of patched Scikit-learn and original</h3>MSE metric of patched Scikit-learn: 1.0109113399224974 <br>MSE metric of unpatched Scikit-learn: 1.0109113399545733 <br>Metrics ratio: 0.9999999999682703 <br><h3>With Scikit-learn-intelex patching you can:</h3><ul><li>Use your Scikit-learn code for training and prediction with minimal changes (a couple of lines of code);</li><li>Fast execution training and prediction of Scikit-learn models;</li><li>Get the similar quality</li><li>Get speedup in <strong>14.2</strong> times.</li></ul>"
334+
],
335+
"text/plain": [
336+
"<IPython.core.display.HTML object>"
337+
]
338+
},
339+
"execution_count": 13,
340+
"metadata": {},
341+
"output_type": "execute_result"
342+
}
343+
],
344+
"source": [
345+
"HTML(f\"<h3>Compare MSE metric of patched Scikit-learn and original</h3>\"\n",
346+
" f\"MSE metric of patched Scikit-learn: {mse_metric_opt} <br>\"\n",
347+
" f\"MSE metric of unpatched Scikit-learn: {mse_metric_original} <br>\"\n",
348+
" f\"Metrics ratio: {mse_metric_opt/mse_metric_original} <br>\"\n",
349+
" f\"<h3>With Scikit-learn-intelex patching you can:</h3>\"\n",
350+
" f\"<ul>\"\n",
351+
" f\"<li>Use your Scikit-learn code for training and prediction with minimal changes (a couple of lines of code);</li>\"\n",
352+
" f\"<li>Fast execution training and prediction of Scikit-learn models;</li>\"\n",
353+
" f\"<li>Get the similar quality</li>\"\n",
354+
" f\"<li>Get speedup in <strong>{(train_unpatched/train_patched):.1f}</strong> times.</li>\"\n",
355+
" f\"</ul>\")"
356+
]
357+
}
358+
],
359+
"metadata": {
360+
"kernelspec": {
361+
"display_name": "Python 3 (ipykernel)",
362+
"language": "python",
363+
"name": "python3"
364+
},
365+
"language_info": {
366+
"codemirror_mode": {
367+
"name": "ipython",
368+
"version": 3
369+
},
370+
"file_extension": ".py",
371+
"mimetype": "text/x-python",
372+
"name": "python",
373+
"nbconvert_exporter": "python",
374+
"pygments_lexer": "ipython3",
375+
"version": "3.8.12"
376+
}
377+
},
378+
"nbformat": 4,
379+
"nbformat_minor": 5
380+
}

0 commit comments

Comments
 (0)