如何在关于泰坦尼克号的 Kaggle 竞赛中使用 Rs 神经网络包

How to use Rs neuralnet package in a Kaggle competition about Titanic

我正在尝试 运行 此代码用于关于 Titanic 的 Kaggle 竞赛练习。它是免费的,是一个初学者案例。我在这个包中使用 R 中的 neuralnet 包。

这是来自网站的列车数据:

train <- read.csv("train.csv")
m <- model.matrix(  ~ Survived + Pclass + Sex + Age + SibSp, data =train )
head(m)

我在这里训练神经网络,这取决于谁幸存下来。我想 看看我能否预测谁幸存:

library(neuralnet)

r <- neuralnet( Survived ~ Pclass + Sexmale + Age + SibSp, 
data=m, hidden=10, threshold=0.01,rep=100)

网络训练好了。我加载测试数据并准备测试。

test=read.csv("test.csv")

m2 <- model.matrix(  ~  Pclass + Sex + Age + SibSp, data = test )

预测的最终测试:

res= compute(r, m2)

首先,我不知道我应该采取多少隐藏的神经元。有时需要很长时间,当我成功时,我无法用测试数据进行测试,因为出现错误,说两个数据集不兼容:

res= compute(r, m2)

Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

我做错了什么?

全部代码:

train <- read.csv("train.csv")
m <- model.matrix(  ~ Survived + Pclass + Sex + Age + SibSp, data =train )
head(m)

library(neuralnet)

r <- neuralnet( Survived ~ Pclass + Sexmale + Age + SibSp, 
data=m, hidden=10, threshold=0.01,rep=100)

test=read.csv("test.csv")

m2 <- model.matrix(  ~  Pclass + Sex + Age + SibSp, data = test )

res= compute(r, m2)

试试用这个来预测:

res = compute(r, m2[,c("Pclass", "Sexmale", "Age", "SibSp")])

这对我有用,你应该得到一些输出。

似乎发生了什么:model.matrix 创建了额外的列 ((Intercept)),这不是用于构建神经网络的数据的一部分,例如在 compute 功能它不知道用它做什么。所以解决方案是 select 显式地列出需要在计算函数中使用的列。这是因为 neuralnet 试图进行某种矩阵乘法,但矩阵的大小不正确。


对于多少个神经元,或优化超参数,您可以使用交叉验证和所有其他方法。如果使用不同的包 (nnet) 没问题,那么您可以使用 caret 包来确定最适合您的参数。它看起来像这样:

library(caret)
nnet.model <- train(Survived ~ Pclass + Sex + Age + SibSp, 
                    data=train, method="nnet")
plot(nnet.model)
res2 = predict(nnet.model, newdata=test)

超参数图是这样的:


您可以使用 caret 包中的 confusionMatrix 来衡量性能:

library(neuralnet)
library(caret)
library(dplyr)
train <- read.csv("train.csv")
m <- model.matrix(  ~ Survived + Pclass + Sex + Age + SibSp, data =train )

r <- neuralnet( Survived ~ Pclass + Sexmale + Age + SibSp, 
                data=m, rep=20)

res = neuralnet::compute(r, m[,c("Pclass", "Sexmale", "Age", "SibSp")])
pred_train = round(res$net.result)

# filter only with the ones with a survival prediction, not all records
# were predicted for some reason;
pred_rowid <- as.numeric(row.names(pred_train))
train_survived <- train %>% filter(row_number(Survived) %in% pred_rowid) %>% select(Survived)
confusionMatrix(as.factor(train_survived$Survived), as.factor(pred_train))

输出:

Confusion Matrix and Statistics

          Reference
Prediction   0   1
         0 308 128
         1 164 114

               Accuracy : 0.5910364             
                 95% CI : (0.5539594, 0.6273581)
    No Information Rate : 0.6610644             
    P-Value [Acc > NIR] : 0.99995895            

                  Kappa : 0.119293              
 Mcnemar's Test P-Value : 0.04053844            

            Sensitivity : 0.6525424             
            Specificity : 0.4710744             
         Pos Pred Value : 0.7064220             
         Neg Pred Value : 0.4100719             
             Prevalence : 0.6610644             
         Detection Rate : 0.4313725             
   Detection Prevalence : 0.6106443             
      Balanced Accuracy : 0.5618084             

       'Positive' Class : 0