微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R语言朴素贝叶斯分类器的predict()下标类型'list'无效

如何解决R语言朴素贝叶斯分类器的predict()下标类型'list'无效

我的课程要求我使用 Udacity 的 enron 财务数据在 R 中制作财务欺诈检测模型。

我写了一个计算函数(split_train_set 只是将数据分成 1 70-30 个训练和测试集。

library(e1071)
library(caret)

nb_runner <- function(dataset,rm.na=FALSE) {
  split_df <- split_train_set(dataset,rm.na)
  nb <- naiveBayes(x=split_df$x_train_set,y=split_df$y_train_set$poi)
  nb_predict <- predict(nb,newdata=split_df$x_test_set,type='class')
  cm <- confusionMatrix(nb_predict,split_df$y_test_set$poi,positive='True')
  return(cm)
}

一开始效果很好。 但是,在我尝试通过以下代码删除超过 15 个 NA 的行来清理数据后,并重新运行相同的 nb_runner()

remove_high_na <- function(dataset,threshold = 0.7) {
  # The range of NA in rows is 2 to 17
  # Since we have only 22 features in the dataset,high level of NA makes the col useless
  # Hence,we will remove rows with high level of NA,and we will set the threshold as 0.7.
  # The row with NA higher than 0.7 (> 15.6) will be removed. 
  threshold_cols <- floor(ncol(dataset) * threshold)
  df <- subset(dataset,rowSums(is.na(dataset)) <= threshold_cols)
  # df <- dataset[-which(rowSums(is.na(dataset)) > threshold_cols),]
  return(df)
}
Error in object$levels[apply(L,2,which.max)] : 
  invalid subscript type 'list' 
The code Failed and the traceback is as follows:
4.
factor(object$levels[apply(L,which.max)],levels = object$levels) 
3.
predict.naiveBayes(nb,newdata = split_df$x_test_set,type = "class") 
2.
predict(nb,type = "class") at POI_helpers.R#38
1.
nb_runner(df_1) 

我不太确定我做错了什么,因为相同的数据集在其他分类器中运行良好。 预先感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。