LoginSignup
0
1

More than 3 years have passed since last update.

R: Kaggle Titanic

Last updated at Posted at 2020-08-27

R で Kaggle に Submit してみました。
ほぼ、次の記事と同じです。
【初心者用】kaggleのタイタニック号乗客の生存予測モデルをRでサクッと作る
ただし、Score は違いました。0.70334 でした。
titanic_aug27_aa.png

In [1]:

library('randomForest') 

list.files(path = "../input/titanic")

train_path <- '../input/titanic/train.csv'
test_path <- '../input/titanic/test.csv'
train <- read.csv(train_path, stringsAsFactors = F )
test  <- read.csv(test_path, stringsAsFactors = F )
#

In [2]:

# 乱数種を設定することで、毎回、同じモデルが生成される
set.seed(754)

# Survivedが目的変数(知りたい結果)で、~以降が説明変数
# 与えられた全ての変数を利用するわけではない
rf_model <- randomForest(factor(Survived) ~ Pclass + SibSp + Parch + Parch, data = train)

# OOB estimate of  error rateが30.75%、これは
print(rf_model)

In [3]:

# 作成したモデルを利用して、testファイルのPclass + SibSp + Parch + ParchからSurvivedを計算する
prediction <- predict(rf_model, test)

# 提出用にPassengerIdと予想したpredictionの列を持つdata.frameを作成する
solution <- data.frame(PassengerID = test$PassengerId, Survived = prediction)

# testの結果をcsvファイルに書き込む
write.csv(solution, file = 'r_titanic_aug2701.csv', row.names = F)
0
1
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
0
1