我写了这样的代码:
library(RTextTools)
library(e1071)
library(SparseM)
pos_feeds = rbind(
c('ICICI Bank stocks soars','positive'),
c('Thomas Cook India stocks increase by 4.92%','positive'),
c('Sensex surges over 1000 pts','positive'),
c('Oil stocks up','positive'),
c('PSU bank stocks rise','positive'),
c('GDP nos are good','positive'),
c('State Bank of India stocks experience jump','positive'),
c('CPI nos are good','positive'),
c('Stock market is positive','positive'),
c('Talwalkar stocks experience sudden increase','positive')
)
neg_feeds = rbind(
c('Tata Nano stocks fall','negative'),
c('Nifty drops below 10000','negative'),
c('IOC stocks fall 3% below normal','negative'),
c('PNB stocks plunge below 40000 mark','negative'),
c('Markets crash a day after demonitization','negative'),
c('Banking stocks plunge','negative'),
c('Sensex drops by a big margin','negatve'),
c('Stocks tumble to new low','negative'),
c('Tata Steel stocks lower than normal','negative'),
c('Bank of India stocks worse than normal','negative')
)
test_feeds = rbind(
c('Citi Bank stocks soars','positive'),
c('Nifty drops below normal to finish at 15000','negative'),
c('Thomas Cook India stocks increase by a big margin','positive'),
c('Sensex surges to finish around the 50000 mark','positive'),
c('Facebook Co stocks fall 10% below usual','negative'),
c('Indian Oil Corp stocks up by 10%','positive'),
c('PNB housing stocks plunge below 50000 mark','negative'),
c('State Bank of India stocks rise','positive'),
c('Markets crash','negative'),
c('Axis Bank stocks tumble to record new low','negative')
)
feeds = rbind(pos_feeds,neg_feeds,test_feeds)
matrix = create_matrix(feeds[,1],language = "english",removeStopwords = FALSE,removeNumbers = TRUE,stemWords = FALSE)
mat = as.matrix(matrix)
classifier = naiveBayes(mat[1:10,], as.factor(feeds[1:10,2]) )
predicted = predict(classifier, mat[11:20,]); predicted
table(feeds[11:20,2], predicted)
recall_accuracy(feeds[11:20,2], predicted)
运行代码时,我在此行中收到错误:
预测=预测(分类器,mat [11:20,]);预测错误 apply(log(sapply(seq_along(attribs),function(v){:dim(X)必须有 正长度
我没有使用apply函数,为什么我会收到此错误? 如果必须使用,如何在这里使用apply函数? 有人可以帮忙吗? 其他人也可以帮忙吗?
答案 0 :(得分:0)
回答你的问题:“我没有使用apply函数,为什么会出现此错误?”
许多函数调用其他函数。如果您没有调用的函数出错,那么您应该首先运行traceback()
。这只有在你收到错误后首先做的事情才有效。在这里,您将获得一个回溯,向您显示predict
调用的堆栈中发生的错误。
当你跑步时:
predicted = predict(classifier, mat[11:20,])
predict
调用predict.naiveBayes(classifier, mat[11:20,])
,然后运行该特定方法。这是您遇到问题的地方。阅读predict.naiveBayes
的文档,特别是查看示例。在每种情况下,传递给naiveBayes
的{{1}}对象都有一个predict
元素,其中apriori
为2.您的对象的dim
为1。这可能是你的问题所在(这意味着它是你如何构建dim
的问题。