【问题标题】:Problem when training Naive Bayes model in R在 R 中训练朴素贝叶斯模型时出现问题
【发布时间】:2020-08-03 14:42:07
【问题描述】:

我正在使用 to Caret 包(没有太多使用 Caret 的经验)来使用 Naive Bayes 训练我的数据,如下面的 R 代码中所述。我在执行“nb_model”时遇到了包含句子的问题,因为它会产生一系列错误消息,它们是:

1: predictions failed for Fold1: usekernel= TRUE, fL=0, adjust=1 Error in 
predict.NaiveBayes(modelFit, newdata) : 
Not all variable names used in object found in newdata

2: model fit failed for Fold1: usekernel=FALSE, fL=0, adjust=1 Error in 
NaiveBayes.default(x, y, usekernel = FALSE, fL = param$fL, ...) : 

请您就如何修改下面的 R 代码来解决该问题提出建议吗?

Dataset used in the R code below

数据集外观的快速示例(10 个变量):

  Over arrested at in | Negative | Negative | Neutral | Neutral | Neutral | Negative |
  Positive | Neutral | Negative
library(caret)

# Loading dataset
setwd("directory/path")
TrainSet = read.csv("textsent.csv", header = FALSE)

# Specifying an 80-20 train-test split
# Creating the training and testing sets
train = TrainSet[1:1200, ]
test = TrainSet[1201:1500, ]

# Declaring the trainControl function
train_ctrl = trainControl(
  method  = "cv", #Specifying Cross validation
  number  = 3, # Specifying 3-fold
)

nb_model = train(
  V10 ~., # Specifying the response variable and the feature variables
  method = "nb", # Specifying the model to use
  data = train, 
  trControl = train_ctrl,
)

# Get the predictions of your model in the test set
predictions = predict(nb_model, newdata = test)

# See the confusion matrix of your model in the test set
confusionMatrix(predictions, test$V10)

【问题讨论】:

  • 这看起来是一个很好的问题,但请记住,我们不会在 Stack Overflow 上使用个人云链接,因为它们会导致 virii 和断开的链接。如果数据可从软件包或主要网站获得,则使用该数据,否则请编造伪数据以用于您的示例。话虽如此,这个错误 - Not all variable names used in object found in newdata - 就是它所说的。新数据中缺少训练数据中的数据。我认为如果您不小心将因变量包含在训练数据中作为预测变量,这通常会发生。
  • @Hack-R 我将确保将来提供伪数据。我已经尝试了几种方法来解决这个问题,但我没有找到解决这个问题的方法。请帮忙看一下上面的 R 代码。
  • @Hack-R 我已将链接更改为 GitHub,因此将数据集导入 R 应该更容易,并在上面提供了数据集外观的示例。
  • @Hack-R 好的,非常感谢您的帮助。
  • @Hack-R 好的,很高兴知道。再次感谢。

标签: r r-caret


【解决方案1】:

数据集都是字符数据。在该数据中,有易于编码的单词 (V2 - V10) 和句子的组合,您可以对其进行任意数量的特征工程并生成任意数量的特征。

要了解文本挖掘,请查看tm 包、其文档或hack-r.com 等博客以获取实际示例。这是链接文章中的一些Github code

好的,所以首先我设置了stringsAsFactors = F,因为您的V1 有大量独特的句子

TrainSet <- read.csv(url("https://raw.githubusercontent.com/jcool12/dataset/master/textsentiment.csv?token=AA4LAP5VXI6I7FRKMT6HDPK6U5XBY"),
                     header = F,
                     stringsAsFactors = F)

library(caret)

然后我做了特征工程

## Feature Engineering
# V2 - V10
TrainSet[TrainSet=="Negative"] <- 0
TrainSet[TrainSet=="Positive"] <- 1

# V1 - not sure what you wanted to do with this
#     but here's a simple example of what 
#     you could do
TrainSet$V1 <- grepl("london", TrainSet$V1) # tests if london is in the string

然后它起作用了,尽管您需要改进 V1 的工程(或放弃它)以获得更好的结果。

# In reality you could probably generate 20+ decent features from this text
#  word count, tons of stuff... see the tm package

# Specifying an 80-20 train-test split
# Creating the training and testing sets
train = TrainSet[1:1200, ]
test = TrainSet[1201:1500, ]

# Declaring the trainControl function
train_ctrl = trainControl(
  method  = "cv", # Specifying Cross validation
  number  = 3,    # Specifying 3-fold
)

nb_model = train(
  V10 ~., # Specifying the response variable and the feature variables
  method = "nb", # Specifying the model to use
  data = train, 
  trControl = train_ctrl,
)

# Resampling: Cross-Validated (3 fold) 
# Summary of sample sizes: 799, 800, 801 
# Resampling results across tuning parameters:
#   
#   usekernel  Accuracy   Kappa    
# FALSE      0.6533444  0.4422346
# TRUE      0.6633569  0.4185751

您会在这个基本示例中得到一些可忽略的警告,因为V1 中只有很少的句子包含“london”一词。我建议将该列用于情绪分析、词频/逆文档频率等。

【讨论】:

  • 感谢您的详细回答,这对了解我接下来需要做什么非常有帮助。第一列中的句子已被一系列情感词典识别为正面、负面或中性。我希望将手动分类 (V10) 与词典词典结果按正面、负面和中性进行比较。我知道在进行比较时我需要包含这些句子。你认为我以正确的方式处理这件事吗?希望这是有道理的。
  • @jr134 当然。那么,您可能想要完全删除原来的V1 列?
  • 那么如果我理解正确的话,我根本不需要用句子来运行V2-V10吗?我想如果我不包含这些句子,那么这会产生一个不准确的图片,还是我没有正确理解这个?
猜你喜欢
  • 2020-06-28
  • 2019-04-07
  • 2017-08-30
  • 1970-01-01
  • 2014-02-21
  • 2015-06-25
  • 2013-09-09
  • 2014-12-21
  • 2020-09-13
相关资源
最近更新 更多