apply(log(sapply(seq_along(attribs), function(v) { : dim(X)



我写了这样的代码:

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) 必须有 正长度

我没有使用应用功能,为什么会收到此错误? 如果我必须使用,如何使用此处的应用功能? 有人可以帮忙吗? 其他人也可以帮忙吗?

回答您的问题:"我没有使用应用功能,为什么会出现此错误?

许多函数调用其他函数。如果您没有调用的函数出错,您应该做的第一件事就是运行traceback()。 仅当这是您收到错误后做的第一件事时,这才有效。在这里,您将获得一个回溯,显示错误发生在predict调用的堆栈中。

运行时:

predicted = predict(classifier, mat[11:20,]) 

predict调用predict.naiveBayes(classifier, mat[11:20,]),然后通过该特定方法运行。这就是您遇到问题的地方。阅读文档以了解predict.naiveBayes,特别是查看示例。在上述每种情况下,传递给predictnaiveBayes对象都有一个dim为 2 的apriori元素。对象的dim为 1。 这可能是你的问题所在(这意味着它是如何构建classifier的问题。

最新更新