我正在使用Rtexttools软件包进行情感分析。我用训练数据集训练了几个算法,现在想将算法应用于其他测试数据集。测试数据集具有完全相同的结构,只是包含不同的注释以进行分类。我使用完全相同的过程来创建矩阵和容器,但是在算法上会遇到不同的错误。对于SVM,请参见标题,其他如下:
Error in predict.randomForest(model, newdata = as.matrix(container@classification_matrix), :
number of variables in newdata does not match that in the training data
> NNET_CLASSIFY_hqs = classify_model(container_amazon_test_hqs, NNET_amazon_hqs)
Error in data.frame(as.character(nnet_pred), nnet_prob) :
arguments imply differing number of rows: 0, 626
> TREE_CLASSIFY_hqs = classify_model(container_amazon_test_hqs, TREE_amazon_hqs)
> GLMNET_CLASSIFY_hql = classify_model(container_amazon_test, GLMNET_amazon_hql)
Error in cbind2(1, newx) %*% (nbeta[[i]]) :
Cholmod-Fehler 'A and B inner dimensions must match' bei Datei ../MatrixOps/cholmod_ssmult.c, Zeile 82
这是我的代码中最重要的组成部分。
trainingdata_amazon_hqs$sentiment <- factor(trainingdata_amazon_hqs$sentiment, levels=c(0,1),ordered = FALSE)
# TERM-DOCUMENT MATRIX REPRESENTS WORD FREQUENCIES IN EACH DOCUMENT
doc_matrix_amazon_hql <- create_matrix(balanced_sample_amazon_hqs$reviewText, language="english",removeNumbers=TRUE,removePunctuation=TRUE, removeStopwords=TRUE, removeSparseTerms=.998,toLower=TRUE, stemWords=TRUE)
# Creating Container training data set
container_amazon_hql <- create_container(doc_matrix_amazon_hql, balanced_sample_amazon_hqs$sentiment, trainSize=1:2500, virgin=FALSE)
# TRAIN THE ALGORITHMS USING THE CONTAINER
SVM_amazon_hql <- train_model(container_amazon_hql, "SVM")
...
testdata_amazon$sentiment <- factor(testdata_amazon$sentiment, levels=c(0,1),ordered = FALSE)
#Term-Document Matrix Test Data Set
doc_matrix_amazon_test <- create_matrix(testdata_amazon$reviewText, language="english",removeNumbers=TRUE,removePunctuation=TRUE, removeStopwords=TRUE, removeSparseTerms=.998,toLower=TRUE, stemWords=TRUE)
#container test data set
container_amazon_test <-create_container(doc_matrix_amazon_test_hqs, testdata_amazon$sentiment, testSize=1:626, virgin=FALSE)
#classify
SVM_CLASSIFY_hql = classify_model(container_amazon_test, SVM_amazon_hql)
在此先感谢您的帮助!