forked from h2oai/data-science-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.html
599 lines (518 loc) · 18 KB
/
examples.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html lang="en">
<head>
<title>Data Science Examples</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="static/jquery/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" href="static/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="static/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="static/highlight/styles/default.css">
<script src="static/highlight/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<div class="container">
<div class="jumbotron text-center">
<h1>Data Science Examples</h1>
<em>A collection of data science examples implemented across a variety of languages and libraries.</em>
<hr>
<h3>
<a href="https://github.com/h2oai/data-science-examples/blob/gh-pages/README.md">
[How to contribute]</a>
<a href="https://github.com/h2oai/data-science-examples/graphs/contributors">
[List of contributors]</a>
<a href="report_an_issue.html">
[Report an issue]</a>
</h3>
</div>
<div class="bg-primary">
<h1>Table of Contents</h1>
</div>
<h2>Section 1. Parsing and Ingesting Data</h2>
<ul>
<li><a href="#how_do_i_import_data_">1.1 How Do I Import Data?</a></li>
<li><a href="#how_do_i_upload_data_">1.2 How Do I Upload Data?</a></li>
</ul>
<h2>Section 2. Munging Data</h2>
<ul>
<li><a href="#how_do_i_slice_columns_from_a_frame_">2.1 How do I Slice Columns From A Frame?</a></li>
<li><a href="#how_do_i_slice_rows_from_a_frame_">2.2 How Do I Slice Rows From A Frame?</a></li>
<li><a href="#how_do_i_replace_values_in_a_frame_">2.3 How Do I Replace Values In A Frame?</a></li>
</ul>
<h2>Section 3. Training Models</h2>
<ul>
<li><a href="#how_do_i_view_trees_from_a_random_forest_model_">3.1 How Do I View Trees From A Random Forest Model?</a></li>
</ul>
<h2>Section 4. Making Predictions</h2>
<ul>
</ul>
<div class="bg-primary">
<h1>Tags</h1>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>Tag</th>
<th>Related Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td>classifier</td>
<td>
<a href=#how_do_i_view_trees_from_a_random_forest_model_>How Do I View Trees From A Random Forest Model?</a>
</td>
</tr>
<tr>
<td>csv</td>
<td>
<a href=#how_do_i_import_data_>How Do I Import Data?,</a>
<a href=#how_do_i_upload_data_>How Do I Upload Data?</a>
</td>
</tr>
<tr>
<td>import</td>
<td>
<a href=#how_do_i_import_data_>How Do I Import Data?</a>
</td>
</tr>
<tr>
<td>missing</td>
<td>
<a href=#how_do_i_replace_values_in_a_frame_>How Do I Replace Values In A Frame?</a>
</td>
</tr>
<tr>
<td>munge</td>
<td>
<a href=#how_do_i_slice_columns_from_a_frame_>How do I Slice Columns From A Frame?,</a>
<a href=#how_do_i_slice_rows_from_a_frame_>How Do I Slice Rows From A Frame?,</a>
<a href=#how_do_i_replace_values_in_a_frame_>How Do I Replace Values In A Frame?</a>
</td>
</tr>
<tr>
<td>parse</td>
<td>
<a href=#how_do_i_import_data_>How Do I Import Data?,</a>
<a href=#how_do_i_upload_data_>How Do I Upload Data?</a>
</td>
</tr>
<tr>
<td>pojo</td>
<td>
<a href=#how_do_i_view_trees_from_a_random_forest_model_>How Do I View Trees From A Random Forest Model?</a>
</td>
</tr>
<tr>
<td>pull</td>
<td>
<a href=#how_do_i_import_data_>How Do I Import Data?</a>
</td>
</tr>
<tr>
<td>push</td>
<td>
<a href=#how_do_i_upload_data_>How Do I Upload Data?</a>
</td>
</tr>
<tr>
<td>randomforest</td>
<td>
<a href=#how_do_i_view_trees_from_a_random_forest_model_>How Do I View Trees From A Random Forest Model?</a>
</td>
</tr>
<tr>
<td>replace</td>
<td>
<a href=#how_do_i_replace_values_in_a_frame_>How Do I Replace Values In A Frame?</a>
</td>
</tr>
<tr>
<td>slice</td>
<td>
<a href=#how_do_i_slice_columns_from_a_frame_>How do I Slice Columns From A Frame?,</a>
<a href=#how_do_i_slice_rows_from_a_frame_>How Do I Slice Rows From A Frame?</a>
</td>
</tr>
<tr>
<td>trees</td>
<td>
<a href=#how_do_i_view_trees_from_a_random_forest_model_>How Do I View Trees From A Random Forest Model?</a>
</td>
</tr>
<tr>
<td>upload</td>
<td>
<a href=#how_do_i_upload_data_>How Do I Upload Data?</a>
</td>
</tr>
</tbody>
</table>
<div class="bg-primary">
<h1>The Examples</h1>
</div>
<div id="how_do_i_import_data_">
<h3>1.1. Parsing and Ingesting Data / How Do I Import Data?<a href="#how_do_i_import_data_"> <em>[link]</em></a></h3>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#1_1_description">Description</a></li>
<li><a data-toggle="tab" href="#1_1_R">R</a></li>
<li><a data-toggle="tab" href="#1_1_h2o-R">h2o-R</a></li>
<li><a data-toggle="tab" href="#1_1_h2o-python">h2o-python</a></li>
</ul>
<div class="tab-content">
<div id="1_1_description" class="tab-pane fade in active">
<div class="well">
<p>This example shows a very simple example of how to parse a small CSV file.</p>
<p>The well-known iris dataset is used.</p>
<p>The H2O examples use the h2o.importFile method, which is a "pull-into-h2o" operation.</p>
<p>This is a fast, scalable, highly optimized way to read data. H2O pulls the data from a data store. H2O initiates the data transfer as a read operation.</p>
</div>
</div>
<div id="1_1_R" class="tab-pane fade">
<pre>
<code class="lang-r">
library(ex)
path = ex.locate("data/iris/iris_wheader.csv")
df = read.csv(path)
stopifnot(nrow(df) == 150)
</code>
</pre>
</div>
<div id="1_1_h2o-R" class="tab-pane fade">
<pre>
<code class="lang-r">
library(h2o)
library(ex)
path = ex.locate("data/iris/iris_wheader.csv")
h2o.init()
h2odf = h2o.importFile(path)
stopifnot(nrow(h2odf) == 150)
</code>
</pre>
</div>
<div id="1_1_h2o-python" class="tab-pane fade">
<pre>
<code class="lang-python">
import h2o
h2o.init()
path = "data/iris/iris_wheader.csv"
df = h2o.import_file(path=path)
assert df.nrow == 150
</code>
</pre>
</div>
</div>
</div>
<div id="how_do_i_upload_data_">
<h3>1.2. Parsing and Ingesting Data / How Do I Upload Data?<a href="#how_do_i_upload_data_"> <em>[link]</em></a></h3>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#1_2_description">Description</a></li>
<li><a data-toggle="tab" href="#1_2_h2o-R">h2o-R</a></li>
</ul>
<div class="tab-content">
<div id="1_2_description" class="tab-pane fade in active">
<div class="well">
<p>This example shows a very simple example of how to parse a small CSV file.</p>
<p>The well-known iris dataset is used.</p>
<p>The H2O examples use the h2o.uploadFile method, which is a "push-to-h2o" operation.</p>
<p>This is not scalable, and only intended for smaller data sizes. The client pushes the data from a local filesystem (for example on your laptop where R is running) to H2O.</p>
<p>For big-data operations, you don't want the data stored on, or flowing through, the client.</p>
</div>
</div>
<div id="1_2_h2o-R" class="tab-pane fade">
<pre>
<code class="lang-r">
library(h2o)
library(ex)
path = ex.locate("data/iris/iris_wheader.csv")
h2o.init()
h2odf = h2o.uploadFile(path)
stopifnot(nrow(h2odf) == 150)
</code>
</pre>
</div>
</div>
</div>
<div id="how_do_i_slice_columns_from_a_frame_">
<h3>2.1. Munging Data / How do I Slice Columns From A Frame?<a href="#how_do_i_slice_columns_from_a_frame_"> <em>[link]</em></a></h3>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#2_1_description">Description</a></li>
<li><a data-toggle="tab" href="#2_1_R">R</a></li>
<li><a data-toggle="tab" href="#2_1_h2o-R">h2o-R</a></li>
<li><a data-toggle="tab" href="#2_1_h2o-python">h2o-python</a></li>
</ul>
<div class="tab-content">
<div id="2_1_description" class="tab-pane fade in active">
<div class="well">
<p>This example shows how to slice columns from a frame of data.</p>
<p>H2O lazily slices out columns of data, and will only materialize a shared copy upon some type of triggering IO.</p>
</div>
</div>
<div id="2_1_R" class="tab-pane fade">
<pre>
<code class="lang-r">
path <- "data/iris/iris_wheader.csv"
df <- read.csv(path)
# slice 1 column by index
c1 <- df[,1]
# slice 1 column by name
c1_1 <- df[, "sepal_len"]
# slice cols by vector of indexes
cols <- df[, 1:4]
# slice cols by vector of names
cols_1 <- df[, c("sepal_len", "sepal_wid", "petal_len", "petal_wid")]
</code>
</pre>
</div>
<div id="2_1_h2o-R" class="tab-pane fade">
<pre>
<code class="lang-r">
library(h2o)
path <- "data/iris/iris_wheader.csv"
h2o.init()
df <- h2o.importFile(path)
# slice 1 column by index
c1 <- df[,1]
# slice 1 column by name
c1_1 <- df[, "sepal_len"]
# slice cols by vector of indexes
cols <- df[, 1:4]
# slice cols by vector of names
cols_1 <- df[, c("sepal_len", "sepal_wid", "petal_len", "petal_wid")]
</code>
</pre>
</div>
<div id="2_1_h2o-python" class="tab-pane fade">
<pre>
<code class="lang-python">
import h2o
h2o.init()
path = "data/iris/iris_wheader.csv"
df = h2o.import_file(path=path)
# slice 1 column by index
c1 = df[:,0]
# slice 1 column by name
c1_1 = df[:, "sepal_len"]
# slice cols by list of indexes
cols = df[:, range(4)]
# slice cols by a list of names
cols_1 = df[:, ["sepal_len", "sepal_wid", "petal_len", "petal_wid"]]
</code>
</pre>
</div>
</div>
</div>
<div id="how_do_i_slice_rows_from_a_frame_">
<h3>2.2. Munging Data / How Do I Slice Rows From A Frame?<a href="#how_do_i_slice_rows_from_a_frame_"> <em>[link]</em></a></h3>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#2_2_description">Description</a></li>
<li><a data-toggle="tab" href="#2_2_R">R</a></li>
<li><a data-toggle="tab" href="#2_2_h2o-R">h2o-R</a></li>
<li><a data-toggle="tab" href="#2_2_h2o-python">h2o-python</a></li>
</ul>
<div class="tab-content">
<div id="2_2_description" class="tab-pane fade in active">
<div class="well">
<p>This example shows how to slice rows columns from a frame of data.</p>
<p>H2O lazily slices out rows of data, and will only materialize a shared copy upon IO.</p>
</div>
</div>
<div id="2_2_R" class="tab-pane fade">
<pre>
<code class="lang-r">
path <- "data/iris/iris_wheader.csv"
df <- read.csv(path)
# slice 1 row by index
c1 <- df[15,]
# slice a range of rows
c1_1 <- df[25:49,]
# slice with a boolean mask
mask <- df[,"sepal_len"] < 4.4
cols <- df[mask,]
# filter out missing values
mask <- is.na(df[,"sepal_len"])
cols <- df[!mask,]
</code>
</pre>
</div>
<div id="2_2_h2o-R" class="tab-pane fade">
<pre>
<code class="lang-r">
library(h2o)
path <- "data/iris/iris_wheader.csv"
h2o.init()
df <- h2o.importFile(path)
# slice 1 row by index
c1 <- df[15,]
# slice a range of rows
c1_1 <- df[25:49,]
# slice with a boolean mask
mask <- df[,"sepal_len"] < 4.4
cols <- df[mask,]
# filter out missing values
mask <- is.na(df[,"sepal_len"])
cols <- df[!mask,]
</code>
</pre>
</div>
<div id="2_2_h2o-python" class="tab-pane fade">
<pre>
<code class="lang-python">
import h2o
h2o.init()
path = "data/iris/iris_wheader.csv"
df = h2o.import_file(path=path)
# slice 1 row by index
c1 = df[15,:]
# slice a ramge of rows
c1_1 = df[range(25,50,1), :]
# slice with a boolean mask
mask = df["sepal_len"] < 4.4
cols = df[mask,:]
# filter out missing values
mask = df["sepal_len"].isna()
cols = df[~mask,:] # note how to perform a logical not with the '~'
</code>
</pre>
</div>
</div>
</div>
<div id="how_do_i_replace_values_in_a_frame_">
<h3>2.3. Munging Data / How Do I Replace Values In A Frame?<a href="#how_do_i_replace_values_in_a_frame_"> <em>[link]</em></a></h3>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#2_3_description">Description</a></li>
<li><a data-toggle="tab" href="#2_3_R">R</a></li>
<li><a data-toggle="tab" href="#2_3_h2o-R">h2o-R</a></li>
<li><a data-toggle="tab" href="#2_3_h2o-python">h2o-python</a></li>
</ul>
<div class="tab-content">
<div id="2_3_description" class="tab-pane fade in active">
<div class="well">
<p>This example shows how to replace values in a frame of data.</p>
</div>
</div>
<div id="2_3_R" class="tab-pane fade">
<pre>
<code class="lang-r">
path <- "data/iris/iris_wheader.csv"
df <- read.csv(path)
# replace a single numerical datum
df[15,3] <- 2
# replace a single categorical datum
df[15,5] <- "versicolor"
# replace a whole column
df[,1] <- 3*df[,1]
# replace by row mask
df[df[,"sepal_len"] < 4.4, "sepal_len"] <- 22
# replacement with ifelse
df[,"sepal_len"] <- ifelse(df[,"sepal_len"] < 4.4, 22, df[,"sepal_len"])
# replace missing values with 0
df[is.na(df[,"sepal_len"]), "sepal_len"] <- 0
# alternative with ifelse
df[,"sepal_len"] <- ifelse(is.na(df[,"sepal_len"]), 0, df[,"sepal_len"])
</code>
</pre>
</div>
<div id="2_3_h2o-R" class="tab-pane fade">
<pre>
<code class="lang-r">
library(h2o)
path <- "data/iris/iris_wheader.csv"
h2o.init()
df <- h2o.importFile(path)
# replace a single numerical datum
df[15,3] <- 2
# replace a single categorical datum
# unimplemented as of 3.6.0.8 (tibshirani)
# replace a whole column
df[,1] <- 3*df[,1]
# replace by row mask
df[df[,"sepal_len"] < 4.4, "sepal_len"] <- 22 # BUG: https://0xdata.atlassian.net/browse/PUBDEV-2520
# replacement with ifelse
df[,"sepal_len"] <- h2o.ifelse(df[,"sepal_len"] < 4.4, 22, df[,"sepal_len"])
# replace missing values with 0
df[is.na(df[,"sepal_len"]), "sepal_len"] <- 0
# alternative with ifelse
df[,"sepal_len"] <- h2o.ifelse(is.na(df[,"sepal_len"]), 0, df[,"sepal_len"])
</code>
</pre>
</div>
<div id="2_3_h2o-python" class="tab-pane fade">
<pre>
<code class="lang-python">
import h2o
h2o.init()
path = "data/iris/iris_wheader.csv"
df = h2o.import_file(path=path)
# replace a single numerical datum
df[14,2] = 2
# replace a single categorical datum
# unimplemented as of 3.6.0.8 (tibshirani)
# replace a whole column
df[0] = 3*df[0]
# replace by row mask
df[df["sepal_len"] < 4.4, "sepal_len"] = 22 # BUG: https://0xdata.atlassian.net/browse/PUBDEV-2520
# replacement with ifelse
df["sepal_len"] = (df["sepal_len"] < 4.4).ifelse(22, df["sepal_len"])
# replace missing values with 0
df[df["sepal_len"].isna(), "sepal_len"] <- 0
# alternative with ifelse
df["sepal_len"] <- (df["sepal_len"].isna()).ifelse(0, df["sepal_len"]) # note the parantheses!
</code>
</pre>
</div>
</div>
</div>
<div id="how_do_i_view_trees_from_a_random_forest_model_">
<h3>3.1. Training Models / How Do I View Trees From A Random Forest Model?<a href="#how_do_i_view_trees_from_a_random_forest_model_"> <em>[link]</em></a></h3>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#3_1_description">Description</a></li>
<li><a data-toggle="tab" href="#3_1_h2o-R">h2o-R</a></li>
<li><a data-toggle="tab" href="#3_1_h2o-python">h2o-python</a></li>
</ul>
<div class="tab-content">
<div id="3_1_description" class="tab-pane fade in active">
<div class="well">
<p>This example shows how to build a simple classifier and view/save its POJO.</p>
</div>
</div>
<div id="3_1_h2o-R" class="tab-pane fade">
<pre>
<code class="lang-r">
library(h2o)
path <- "data/iris/iris_wheader.csv"
h2o.init()
df <- h2o.importFile(path)
# build a simple random forest classifier
model <- h2o.randomForest(x=1:4, y=5, training_frame=df)
# print the pojo to screen
h2o.download_pojo(model)
# save it to your favorite local path
h2o.download_pojo(model, modelSavePath)
</code>
</pre>
</div>
<div id="3_1_h2o-python" class="tab-pane fade">
<pre>
<code class="lang-python">
import h2o
h2o.init()
path = "data/iris/iris_wheader.csv"
df = h2o.import_file(path=path)
# build a simple random forest classifier
from h2o.estimators.random_forest import H2ORandomForestEstimator
model = H2ORandomForestEstimator()
model.train(x=range(4), y=4, training_frame=df)
# print the pojo to screen
h2o.download_pojo(model)
# save it to your favorite local path
h2o.download_pojo(model, modelSavePath)
</code>
</pre>
</div>
</div>
</div>
</div>
</body>
</html>