在R使用H2O應用範例
請先完成在R啟用H2O flow的設定。
以下以簡單的案例在R的環境下執行H2O套件ML的API,雖然H2O可以直接讀取local上面的檔案,但是會有資料上限的問題。 我們建議先把資料put到hdfs的系統上(請參見hdfs相關操作),可以解決資料讀取上限的問題,並且會提高資料讀取的速度。
註:一般使用者在系統下的hdfs絕對路徑是 hdfs://nn/user/user-id/,其中user-id就是我們平台登入的帳號, 在讀取資料的時候必須用這個絕對路徑。
Step 1:Get data,在terminal下載資料檔(allyears2k.csv),這是美國飛航資料的抽樣檔。更完整的資料
$ wget "https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv"
Step 2: 把資料檔put到hdfs的系統上
$ hadoop fs -put allyears2k.csv allyears2k.csv
Step 3: 進入R,並進入h2o cluster
$ R
> library(h2o)
> h2o.init(ip="172.16.1.12", port=54323)
Step 4: 引入資料檔,並執行GLM程序,建議執行程序之前加上執行時間,實際可以了解在h2o cluster上執行工作的效能。
> hdfs.folder="hdfs://nn/user/user-id/allyears2k.csv"
> Sys.time()
> airline.hex = h2o.importFile(path=hdfs.folder, destination_frame = "airline.hex")
> Sys.time()
> names(airline.hex)
建立航班是否取消和出發機場的logistic model
> Sys.time()
> airline.glm<-h2o.glm(y = "Cancelled", x = c("Origin"),
training_frame = airline.hex,
family = "binomial",
nfolds = 10, alpha = 0.5)
> Sys.time()
相關的說明請參考以下連結
測試用大資料集