【问题标题】:invalid 'type' (character) of argument参数的无效“类型”(字符)
【发布时间】:2016-08-07 00:51:46
【问题描述】:

这是关于该主题的错误消息。当我尝试运行 naive.bayes 分类器时出现此错误。这是我的火车数据的摘要:

'data.frame':   7269 obs. of  193 variables:
 $ pid       : int  2 4 5 7 10 11 14 18 25 31 ...
 $ acquir    : int  0 0 0 0 1 1 0 0 0 0 ...
 $ addit     : int  0 0 0 0 2 2 0 0 0 0 ...
 $ agre      : int  0 0 0 0 0 0 0 0 0 0 ...
 $ agreement : int  0 0 0 0 0 0 0 0 0 0 ...
 $ also      : int  1 0 0 0 2 2 0 0 0 0 ...
 $ american  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ announc   : int  0 0 0 0 0 0 0 0 0 0 ...
 $ annual    : int  0 0 0 0 0 0 0 0 2 0 ...
 $ approv    : int  0 3 0 0 0 0 0 0 0 0 ...
 $ april     : int  0 0 0 0 0 0 0 0 1 0 ...
 $ bank      : int  0 7 0 0 0 0 0 0 0 0 ...
 $ base      : int  0 0 0 0 0 0 0 0 0 0 ...
 .
 .
 $... all of them are integer, except the class column
 .
 .
 $ class     : Factor w/ 10 levels "acq","corn","crude",..: 1 1 4 4 9 1 4 3 1 4 ...

这是naive.bayes() 行:

model <- naiveBayes(as.factor(class) ~ ., data = as.matrix(train), laplace = 3)

谁能告诉我为什么会这样?:

Error in sum(x) : invalid 'type' (character) of argument

【问题讨论】:

  • 由于as.matrix(train),最终您的数据将转换为字符。尝试model &lt;- naiveBayes(class ~ ., data=train, laplace = 3) 或最终model &lt;- naiveBayes(train$class ~ ., data=train[, -c("class")], laplace = 3)
  • @jogo 成功了!谢谢!我想接受你的回答,但如何?
  • 我现在做了一个答案。第一个变体也起作用还是只有第二个?
  • 我刚试过第一个。它奏效了。其他的没试过。

标签: r classification text-classification naivebayes document-classification


【解决方案1】:

由于as.matrix(train),您的数据最终会转换为字符。尝试

model <- naiveBayes(class ~ ., data=train, laplace = 3)

或最终

model <- naiveBayes(train$class ~ ., data=train[, -c("class")], laplace = 3)

第二个变体或多或少与第一个变体相同。公式右轴中的. 扩展为“所有其他变量”;所以它排除了 LHS 上提到的列class。 (更多信息在formula的文档中)

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2016-03-01
    • 2021-05-04
    • 2021-10-28
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多