簡體   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