【发布时间】: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 好的,很高兴知道。再次感谢。