繁体   English   中英

apply(log(sapply(seq_along(attribs),function(v){:dim(X)必须为正长度)中的错误

[英]Error in apply(log(sapply(seq_along(attribs), function(v) { : dim(X) must have a positive length

我写了这样的代码:

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)

运行代码时,在此行中出现错误:

预测=预测(classifier,mat [11:20,]); apply(log(sapply(seq_along(attribs),function(v){:dim(X)必须具有正长度)

我没有使用apply函数,为什么会出现此错误? 如果必须使用,如何在此处使用Apply功能? 有人可以帮忙吗? 其他人也可以帮忙吗?

您的问题的答案:“我没有使用apply函数,为什么会出现此错误?”

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

运行时:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM