J'essaie de classer 94 textes de discours. Depuis naiveBayes ne peut pas bien fonctionner si les catégories de rames n'existent pas dans les catégories de testset, j'ai randomisé et confirmé. Aucun problème avec les catégories. Mais le classificateur ne fonctionnait pas avec testset. Ce qui suit est un message d'erreur:erreur bayes naïves dans R: indice hors limites
Df.dtm<-cbind(Df.dtm, category)
dim(Df.dtm)
Df.dtm[1:10, 530:532]
# Randomize and Split data by rownumber
train <- sample(nrow(Df.dtm), ceiling(nrow(Df.dtm) * .50))
test <- (1:nrow(Df.dtm))[- train]
# Isolate classifier
cl <- Df.dtm[, "category"]
> summary(cl[train])
dip eds ind pols
23 8 3 13
# Create model data and remove "category"
modeldata <- Df.dtm[,!colnames(Df.dtm) %in% "category"]
#Boolean feature Multinomial Naive Bayes
#Function to convert the word frequencies to yes and no labels
convert_count <- function(x) {
y <- ifelse(x > 0, 1,0)
y <- factor(y, levels=c(0,1), labels=c("No", "Yes"))
y
}
#Apply the convert_count function to get final training and testing DTMs
train.cc <- apply(modeldata[train, ], 2, convert_count)
test.cc <- apply(modeldata[test, ], 2, convert_count)
#Training the Naive Bayes Model
#Train the classifier
system.time(classifier <- naiveBayes(train.cc, cl[train], laplace = 1))
Ce classificateur a bien fonctionné: 用户 系统 流逝 0,45 0,00 0,46
#Use the classifier we built to make predictions on the test set.
system.time(pred <- predict(classifier, newdata=test.cc))
Cependant, la prévision a échoué. Erreur dans [.default
(objet $ tables [[v]],, nd): 下 标出 界 timing arrêté à: 0,2 0 0,2