如何在关于泰坦尼克号的Kaggle比赛中使用Rs neuralnet包

时间:2015-05-21 23:32:20

标签: r machine-learning neural-network kaggle

我正在尝试为关于Titanic的Kaggle比赛运行此代码进行练习。它的forfree和初学者案例。我在这个包中使用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)

1 个答案:

答案 0 :(得分:3)

尝试使用它来预测:

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

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

似乎发生了什么:model.matrix创建了其他列((Intercept)),这些列不是用于构建神经网络的数据的一部分,如compute中所示。功能它不知道用它做什么。因此,解决方案是明确选择在计算函数中使用的列。这是因为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)

超参数的情节如下:

enter image description here

您可以使用confusionMatrix包中的caret来衡量效果:

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