Rのパッケージh2oは、さまざまなクラスタ環境内のニューラルネットワーク(ディープラーニング)、ランダムフォレスト、勾配ブースティングマシン、一般化線形モデルなどの並列分散機械学習アルゴリズムを計算するビッグデータのためのオープンソースの数学エンジンH2O用のRスクリプト機能である。

ここでは、ubuntu14.04環境下でh2oパッケージのインストールの仕方についてお伝えしよう。

インストール手順

まず最初に、端末(ターミナル)上で次のコマンドを実行する。
これは、h2oパッケージの依存パッケージであるRCurlをインストールする場合に必要となるライブラリである。すでにRCurlパッケージがインストールされている場合は次のコマンドは不要である。


$ sudo apt-get install libcurl4-openssl-dev

次に、H2Oはjava上で動作するため、java環境を準備する必要がある。
ここでは、openjdk-7-jreを使うことにする。
すでに、OracleJavaやJDK7以上がインストールされている場合は次のコマンドは不要だ。
次のコマンドの意味は、openjavaのバージョンが7で、開発環境がない実行環境のみのjavaをインストールするということだ。


$ sudo apt-get install openjdk-7-jre

次にRまたはRStudioを立ち上げて、次のコマンドを実行する。
これでインストールは完了だ。


> install.packages("h2o")

ディープラーニングのデモを試す

h2oのディープラーニングをデモで見てみよう。


> # h2oパッケージをロード
> library(h2o)

> # ディープラーニングのデモを実行
> demo(h2o.deeplearning)

デモの結果は次のようになる。




	demo(h2o.deeplearning)
	---- ~~~~~~~~~~~~~~~~

Type  	 to start : 

> # This is a demo of H2O's Deep Learning function
> # It imports a data set, parses it, and prints a summary
> # Then, it runs Deep Learning on the dataset
> # Note: This demo runs H2O on localhost:54321
> library(h2o)

> localH2O = h2o.init(ip = "localhost", port = 54321, startH2O = TRUE)

H2O is not running yet, starting it now...

Note:  In case of errors look at the following log files:
    /tmp/RtmpHWhpgE/h2o_system_developer_started_from_r.out
    /tmp/RtmpHWhpgE/h2o_system_developer_started_from_r.err

java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

..Successfully connected to http://localhost:54321/ 

R is connected to H2O cluster:
    H2O cluster uptime:         4 seconds 85 milliseconds 
    H2O cluster version:        3.0.0.22 
    H2O cluster name:           H2O_started_from_R_system-developer_uul730 
    H2O cluster total nodes:    1 
    H2O cluster total memory:   0.63 GB 
    H2O cluster total cores:    1 
    H2O cluster allowed cores:  1 
    H2O cluster healthy:        TRUE 

Note:  As started, H2O is limited to the CRAN default of 2 CPUs.
       Shut down and restart H2O as shown below to use all your CPUs.
           > h2o.shutdown(localH2O)
           > localH2O = h2o.init(nthreads = -1)


> prostate.hex = h2o.uploadFile(localH2O, path = system.file("extdata", "prostate.csv", package="h2o"), destination_frame = "prostate.hex")
  |============================================================================================================================================================================| 100%

> summary(prostate.hex)
 ID               CAPSULE          AGE             RACE            DPROS           DCAPS           PSA               VOL             GLEASON        
 Min.   :  1.00   Min.   :0.0000   Min.   :43.00   Min.   :0.000   Min.   :1.000   Min.   :1.000   Min.   :  0.300   Min.   : 0.00   Min.   :0.000  
 1st Qu.: 95.75   1st Qu.:0.0000   1st Qu.:62.00   1st Qu.:1.000   1st Qu.:1.000   1st Qu.:1.000   1st Qu.:  4.900   1st Qu.: 0.00   1st Qu.:6.000  
 Median :190.50   Median :0.0000   Median :67.00   Median :1.000   Median :2.000   Median :1.000   Median :  8.664   Median :14.20   Median :6.000  
 Mean   :190.50   Mean   :0.4026   Mean   :66.04   Mean   :1.087   Mean   :2.271   Mean   :1.108   Mean   : 15.409   Mean   :15.81   Mean   :6.384  
 3rd Qu.:285.25   3rd Qu.:1.0000   3rd Qu.:71.00   3rd Qu.:1.000   3rd Qu.:3.000   3rd Qu.:1.000   3rd Qu.: 17.063   3rd Qu.:26.40   3rd Qu.:7.000  
 Max.   :380.00   Max.   :1.0000   Max.   :79.00   Max.   :2.000   Max.   :4.000   Max.   :2.000   Max.   :139.700   Max.   :97.60   Max.   :9.000  

> # Set the CAPSULE column to be a factor column then build model.
> prostate.hex$CAPSULE = as.factor(prostate.hex$CAPSULE)

> model = h2o.deeplearning(x = setdiff(colnames(prostate.hex), c("ID","CAPSULE")), y = "CAPSULE", training_frame = prostate.hex, activation = "Tanh", hidden = c(10, 10, 10), epochs = 10000)
  |============================================================================================================================================================================| 100%

> print(model@model$model_summary)
Status of Neuron Layers:
     layer     units    type dropout       l1       l2 mean_rate rate_RMS momentum mean_weight weight_RMS mean_bias bias_RMS
1 1.000000  7.000000   Input  0.00 %                                                                                        
2 2.000000 10.000000    Tanh  0.00 % 0.000000 0.000000  0.006025 0.010133 0.000000   -0.068544   0.975921 -0.188694 0.486558
3 3.000000 10.000000    Tanh  0.00 % 0.000000 0.000000  0.015935 0.016414 0.000000    0.290550   1.529273  0.073799 0.793197
4 4.000000 10.000000    Tanh  0.00 % 0.000000 0.000000  0.018007 0.025610 0.000000   -0.146090   1.740220 -0.196112 1.467025
5 5.000000  2.000000 Softmax         0.000000 0.000000  0.009652 0.004613 0.000000    0.688943   4.818391  0.082278 1.215682

> # Make predictions with the trained model with training data.
> predictions = predict(object = model, newdata = prostate.hex)

> # Export predictions from H2O Cluster as R dataframe.
> predictions.R = as.data.frame(predictions)

> head(predictions.R)
  predict           p0           p1
1       0 1.000000e+00 1.768709e-11
2       0 9.991143e-01 8.856702e-04
3       0 1.000000e+00 2.899589e-12
4       0 9.995110e-01 4.890519e-04
5       0 1.000000e+00 1.784630e-10
6       1 2.741250e-25 1.000000e+00

> tail(predictions.R)
    predict          p0           p1
375       0 1.000000000 7.365224e-14
376       0 0.995499909 4.500029e-03
377       0 1.000000000 5.529978e-19
378       1 0.002203206 9.977968e-01
379       0 1.000000000 1.167493e-20
380       0 0.999924660 7.536520e-05

> # Check performance of classification model.
> performance = h2o.performance(model = model)

> print(performance)
H2OBinomialMetrics: deeplearning
** Reported on training data. **
Description: Metrics reported on temporary (load-balanced) training frame

MSE:  0.006801479
R^2:  0.9717961
LogLoss:  0.02699277
AUC:  0.9995105
Gini:  0.999021

Confusion Matrix for F1-optimal threshold:
         0   1    Error    Rate
0      225   2 0.008811  =2/227
1        1 152 0.006536  =1/153
Totals 226 154 0.007895  =3/380

Maximum Metrics:
                      metric threshold    value       idx
1                     max f1  0.104665 0.990228 94.000000
2                     max f2  0.104665 0.992167 94.000000
3               max f0point5  0.879355 0.996016 90.000000
4               max accuracy  0.879355 0.992105 90.000000
5              max precision  1.000000 1.000000  0.000000
6           max absolute_MCC  0.879355 0.983669 90.000000
7 max min_per_class_accuracy  0.104665 0.991189 94.000000

h2oでの使用が完了したら、次のコマンドでシャットダウンしておく。
シャットダウンしたら、h2o関連のオブジェクトの中身が見れなくなるので注意が必要だ。


> h2o.shutdown(localH2O)
Are you sure you want to shutdown the H2O instance running at http://localhost:54321/ (Y/N)? y
[1] TRUE
Ubuntu,R h2oパッケージのインストールの方法