LoginSignup
3
2

More than 5 years have passed since last update.

H2O AutoMLでR上でタイタニックの生存予測をしてみる

Posted at

はじめに

SIGNATEの練習問題であるタイタニックの生存予測をRとH2Oでしてみます。ついでに、H2OのAutoMLを試してみてどんなモデルができるかを見てみます。

実行したコード

なにはともあれ、実行したコードを示します。

require(h2o)

# H2Oを起動
local.H2O <- h2o.init()

# データをロードする
train.df <- read.table("train.tsv", sep = "\t", header = T)
test.df <- read.table("test.tsv", sep = "\t", header = T)

# 欠損値を埋める
train.df$age <- ifelse(is.na(train.df$age), mean(train.df$age, na.rm = T), train.df$age)
test.df$age <- ifelse(is.na(test.df$age), mean(test.df$age, na.rm = T), test.df$age)

# H2Oのオブジェクトに変換する
train.hex <- as.h2o(train.df)
test.hex <- as.h2o(test.df)

# survivedを変換する
train.hex$survived <- as.factor(train.hex$survived)

# AutoMLを実行
automl.hex <- h2o.automl(x = c(3,4,5,6,7,8,9), y = 2, training_frame = train.hex)

# 予測を行う
predict.hex <- h2o.predict(automl.hex, test.hex)

# 予測結果を整える
predict.df <- as.data.frame(predict.hex)
submit.df <- data.frame(id = test.df$id, p = predict.df$p1)

# H2O を終了
h2o.shutdown(prompt = FALSE)

# 予測結果を保存
write.table(submit.df, file = "submit.tsv", sep = "\t", row.names = F, col.names = F)

コードそのものはいたってシンプルで、欠損値の見られる年齢を平均値で欠損を埋めて、H2OのAutoMLで2値予測をするコードです。

どんなモデルができたか

テスト実行したときの、実行結果のモデルを確認したところ、GBMとDeepLearningからなるStacked Ensembleなモデルができたようです。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2