【问题标题】:R: why does gbm give NA values on Titanic data?R:为什么 gbm 在泰坦尼克号数据上给出 NA 值?
【发布时间】:2021-10-18 23:26:45
【问题描述】:

我有经典的泰坦尼克号数据。以下是清理后数据的说明。

> str(titanic)
'data.frame':   887 obs. of  7 variables:
 $ Survived               : Factor w/ 2 levels "No","Yes": 1 2 2 2 1 1 1 1 2 2 ...
 $ Pclass                 : int  3 1 3 1 3 3 1 3 3 2 ...
 $ Sex                    : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
 $ Age                    : num  22 38 26 35 35 27 54 2 27 14 ...
 $ Siblings.Spouses.Aboard: int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parents.Children.Aboard: int  0 0 0 0 0 0 0 1 2 0 ...
 $ Fare                   : num  7.25 71.28 7.92 53.1 8.05 ...

我先拆分数据。

set.seed(123)
train_ind <- sample(seq_len(nrow(titanic)), size = smp_size)
train <- titanic[train_ind, ]
test <- titanic[-train_ind, ]

然后我将 Survived 列更改为 0 和 1。

train$Survived <- as.factor(ifelse(train$Survived == 'Yes', 1, 0))
test$Survived <- as.factor(ifelse(test$Survived == 'Yes', 1, 0))

最后,我运行了梯度提升算法。

dt_gb <- gbm(Survived ~ ., data = train)

这是结果。

> print(dt_gb)
gbm(formula = Survived ~ ., data = train)
A gradient boosted model with bernoulli loss function.
100 iterations were performed.
There were 6 predictors of which 0 had non-zero influence.

由于有 0 个预测变量具有非零影响,因此预测结果为 NA。我想知道为什么会这样?我的代码有什么问题吗?

【问题讨论】:

    标签: r gbm


    【解决方案1】:

    避免在训练和测试数据中将 Survival 转换为 0/1 因子。而是将Survival 列更改为具有numeric 类型的0/1 向量。

    # e.g. like this
    titanic$Survival <- as.numeric(titantic$Survival) - 1
    
    # data should look like this
    > str(titanic)
    'data.frame':   887 obs. of  7 variables:
    $ Survived               : num  0 1 1 1 0 0 0 0 1 1 ...
    $ Pclass                 : int  3 1 3 1 3 3 1 3 3 2 ...
    $ Sex                    : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
    $ Age                    : num  22 38 26 35 35 27 54 2 27 14 ...
    $ Siblings.Spouses.Aboard: int  1 1 0 1 0 0 0 3 0 1 ...
    $ Parents.Children.Aboard: int  0 0 0 0 0 0 0 1 2 0 ...
    $ Fare                   : num  7.25 71.28 7.92 53.1 8.05 ...
    

    然后用伯努利损失拟合模型。

    dt_gb <- gbm::gbm(formula = Survived ~ ., data = titanic, 
                      distribution = "bernoulli")
    
    > print(dt_gb)
    gbm::gbm(formula = Survived ~ ., distribution = "bernoulli", 
        data = titanic)
    A gradient boosted model with bernoulli loss function.
    100 iterations were performed.
    There were 6 predictors of which 6 had non-zero influence.
    

    获取前几名乘客的预测生存概率:

    >head(predict(dt_gb, type = "response"))
    [1] 0.1200703 0.9024225 0.5875393 0.9271306 0.1200703 0.1200703
    

    【讨论】:

    • 知道了。这是否意味着 gbm 只接受数值变量?如果我在数据中有因子/分类变量怎么办?我可以将它们更改为 1、2、3 等...但将它们用作数值变量是没有意义的。
    • factor 回归器是可以的。我对我拥有的数据集的版本感到困惑(其中Sexcharacter)我已经编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-03-01
    • 2017-06-27
    • 2016-06-19
    • 2022-01-03
    • 2021-07-29
    • 2020-10-25
    • 2022-07-25
    • 2017-03-12
    相关资源
    最近更新 更多