【问题标题】:R: Kaggle Titanic Dataset Random Forest NAs introduced by coercionR:强制引入的 Kaggle Titanic 数据集随机森林 NA
【发布时间】:2015-07-21 00:46:38
【问题描述】:

我目前正在使用 Titanic 数据集在 Kaggle 上练习 R 我正在使用随机森林算法

下面是代码

fit <- randomForest(as.factor(Survived) ~ Pclass + Sex + Age_Bucket + Embarked
                + Age_Bucket + Fare_Bucket + F_Name + Title + FamilySize + FamilyID, 
                data=train, importance=TRUE, ntree=5000)

我收到以下错误

Error in randomForest.default(m, y, ...) : 
  NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning messages:
1: In data.matrix(x) : NAs introduced by coercion
2: In data.matrix(x) : NAs introduced by coercion
3: In data.matrix(x) : NAs introduced by coercion
4: In data.matrix(x) : NAs introduced by coercion

我的数据如下所示

$ Survived   : int  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_Bucket : chr  "20-25" "30-40" "25-30" "30-40" ...
$ Fare_Bucket: chr  "<10" "30+" "<10" "30+" ...
$ Title      : Factor w/ 11 levels "Col","Dr","Lady",..: 7 8 5 8 7 7 7 4 8 8 ...
$ F_Name     : chr  "Braund" "Cumings" "Heikkinen" "Futrelle" ...
$ FamilySize : num  2 2 1 2 1 1 1 5 3 2 ...
$ Embarked   : Factor w/ 3 levels "C","Q","S": 3 1 3 3 3 2 3 3 3 1 ...
$ FamilyID   : chr  "Small" "Small" "Alone" "Small" ...

如果我只输入以下内容,我没有强制问题,据我所知,这是唯一发生强制以创建 NA 值的地方

as.factor(Survived)

谁能看到问题

感谢您的宝贵时间

【问题讨论】:

    标签: r random-forest coercion kaggle


    【解决方案1】:

    您需要将char 列转换为因子。因子在内部被视为整数,而字符字段则不是。请看下面的小示范:

    数据:

    df <- data.frame(y = sample(0:1, 26, rep=T), x1=runif(26), x2=letters, stringsAsFactors=F)
    
    df$y <- as.factor(df$y)
    
    > str(df)
    'data.frame':   26 obs. of  3 variables:
     $ y : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 2 1 ...
     $ x1: num  0.457 0.296 0.517 0.478 0.764 ...
     $ x2: chr  "a" "b" "c" "d" ...
    

    现在,如果我运行 randomForest 函数:

    > randomForest(y ~ x1 + x2, data=df)
    Error in randomForest.default(m, y, ...) : 
      NA/NaN/Inf in foreign function call (arg 1)
    In addition: Warning message:
    In data.matrix(x) : NAs introduced by coercion
    

    我遇到了同样的错误。

    而如果我将char 列转换为factor

    df$x2 <- as.factor(df$x2)
    
    > randomForest(y ~ x1 + x2, data=df)
    
    Call:
     randomForest(formula = y ~ x1 + x2, data = df) 
                   Type of random forest: classification
                         Number of trees: 500
    No. of variables tried at each split: 1
    
            OOB estimate of  error rate: 61.54%
    Confusion matrix:
      0  1 class.error
    0 0 16           1
    1 0 10           0
    

    效果很好!

    【讨论】:

    • 嗨,对不起,我应该更清楚。我自己运行了“as.factor(Survived)”这一行,它将一切都很好地转换为一个因素,因为这就是我最初认为的问题所在。当我在随机森林代码中运行它时,它给了我关于强制的错误
    • 可以dput数据吗?
    • 我找到了它坏掉的原因!您的代码中有+ FamilyID,但此列不在您的数据集中。
    • 哦哦哦哦哦。你在那里有char 列。 randomForest 函数内的 matrix 创建失败。你能把这些转换成因子再试一次吗? Age_bucket 例如是 char,当创建矩阵时,所有内容都被强制转换为 NA。
    • 看起来就是这样:).....谢谢。可悲的是,看起来我有太多因素无法运行它,所以我将尝试使用推理树。 - 无法处理超过 53 个类别的分类预测变量。
    猜你喜欢
    • 2012-04-19
    • 2017-01-12
    • 1970-01-01
    • 2019-05-04
    • 2014-05-01
    • 2020-10-20
    • 1970-01-01
    • 2018-03-11
    • 2017-07-23
    相关资源
    最近更新 更多