我在使用朴素贝叶斯设置文本分类时遇到问题。首先,我有3个文本文件,两个带有好/坏字的模板,一个测试文件。根据我以前的评级模板,我的TermDocumentMatrix已创建,我也有一个评级向量:
TDM word1 word2 word3 word4 ... rating
doc1 1 1 1 good
doc2 1 1 1 bad
doc3 ...
该向量未添加到TDM,因为我认为qazxswpo将值转换为cbind
。所以我将矩阵分成两部分:
character
其中template_train <- complete_TDM[1:(x+y),]
text_test <- data.matrix(complete_TDM[((x+y+1):nrow(complete_TDM)),])
是好评级模板的行数,而qazxswpo是坏的。
x
然后我创建了一个naiveBayes模型:
y
想要预测
random <- sample(x+y)
template_train <- data.matrix(template_train[random,]) ###shuffle
rating_vector <- as.factor(rating[random]) ###vector containing rating, shuffled the same way
但在最后一步,我收到一个错误:
naive_model <- naiveBayes(rating_vector~., x = template_train, y=rating_vector)
提前致谢!
好吧我刚解决了这个问题,我现在使用prediction <- predict(naive_model, text_test)
instead的> prediction <- predict(naive_model, text_test)
Error in log(sapply(seq_along(attribs), function(v) { :
non-numeric argument to mathematical function
和data.matrix
for我的评级向量,但现在我有问题,一切都很好被评为坏,反之亦然。
as.matrix
你可以使用
as.factor