R 朴素贝叶斯模型在应用模型时不做任何预测-预测函数返回0因子水平

R 朴素贝叶斯模型在应用模型时不做任何预测-预测函数返回0因子水平,r,text-mining,naivebayes,R,Text Mining,Naivebayes,我的数据集如下所示,我随后开发了用于文本挖掘的朴素贝叶斯模型。然而,即使构建了模型,我也无法预测我的朴素贝叶斯的结果。predict函数返回的因子级别为0。下面是到目前为止我的数据集和代码 **Dataset:** lie sentiment review f n 'Mike\'s Pizza High Point

我的数据集如下所示,我随后开发了用于文本挖掘的
朴素贝叶斯
模型。然而,即使构建了模型,我也无法预测我的
朴素贝叶斯
的结果。
predict
函数返回的因子级别为0。下面是到目前为止我的数据集和代码

**Dataset:**
lie sentiment   review                                                                                  
f   n   'Mike\'s Pizza High Point    NY Service was very slow and the quality was low. You would think they would know at least how to make good pizza   not. Stick to pre-made dishes like stuffed pasta or a salad. You should consider dining else where.'                                                                           
f   n   'i really like this buffet restaurant in Marshall street. they have a lot of selection of american   japanese    and chinese dishes. we also got a free drink and free refill. there are also different kinds of dessert. the staff is very friendly. it is also quite cheap compared with the other restaurant in syracuse area. i will definitely coming back here.'                                                                          
f   n   'After I went shopping with some of my friend    we went to DODO restaurant for dinner. I found worm in one of the dishes .'                                                                                
f   n   'Olive Oil Garden was very disappointing. I expect good food and good service (at least!!) when I go out to eat. The meal was cold when we got it    and the waitor had no manners whatsoever. Don\'t go to the Olive Oil Garden. '                                                                             
f   n   'The Seven Heaven restaurant was never known for a superior service but what we experienced last week was a disaster. The waiter would not notice us until we asked him 4 times to bring us the menu. The food was not exceptional either. It took them though 2 minutes to bring us a check after they spotted we finished eating and are not ordering more. Well   never more. '                                                                              
f   n   'I went to XYZ restaurant and had a terrible experience. I had a YELP Free Appetizer coupon which could be applied upon checking in to the restaurant. The person serving us was very rude and didn\'t acknowledge the coupon. When I asked her about it     she rudely replied back saying she had already applied it. Then I inquired about the free salad that they serve. She rudely said that you have to order the main course to get that. Overall    I had a bad experience as I had taken my family to that restaurant for the first time and I had high hopes from the restaurant which is     otherwise   my favorite place to dine. '                                                                   
f   n   'I went to ABC restaurant two days ago and I hated the food and the service. We were kept waiting for over an hour just to get seated and once we ordered    our food came out cold. I ordered the pasta and it was terrible - completely bland and very unappatizing. I definitely would not recommend going there  especially if you\'re in a hurry!'                                                                         
f   n   'I went to the Chilis on Erie Blvd and had the worst meal of my life. We arrived and waited 5 minutes for a hostess  and then were seated by a waiter who was obviously in a terrible mood. We order drinks and it took them 15 minutes to bring us both the wrong beers which were barely cold. Then we order an appetizer and wait 25 minutes for cold southwest egg rolls     at which point we just paid and left. Don\'t go.'                                                                          
f   n   'OMG. This restaurant is horrible. The receptionist did not greet us     we just stood there and waited for five minutes. The food came late and served not warm. Me and my pet ordered a bowl of salad and a cheese pizza. The salad was not fresh  the crust of a pizza was so hard like plastics. My dog didn\'t even eat that pizza. I hate this place!!!!!!!!!!'   
dput(df)

>dput(头部(侧面))
结构(列表)(lie=c(“f”,“f”,“f”,“f”,“f”,“f”),情绪=c(“n”),
“n”,“n”,“n”,“n”,“n”,复习=C(“迈克的比萨高点,NY服务很慢,质量很低。你会认为他们至少知道如何做好比萨饼,不是。坚持预制馅饼或沙拉之类的菜肴。你应该考虑就餐在哪里。”
“我非常喜欢马歇尔街的这家自助餐厅。他们有很多美式、日式和中式菜肴可供选择。我们还提供免费饮料和免费续杯。还有各种甜点。员工非常友好。与锡拉丘兹地区的其他餐厅相比,这家餐厅也相当便宜。我一定会回来的。”在这里。”,
“我和我的一些朋友去购物后,我们去渡渡鸟餐厅吃饭。我在一道菜里发现了虫子。”,
“橄榄油花园非常令人失望。当我出去吃饭时,我希望有好的食物和好的服务(至少!!)。我们拿到的饭很冷,服务员一点礼貌都没有。不要去橄榄油花园。”,
“七天餐厅的服务从来都不是一流的,但我们上周经历的是一场灾难。服务员没有注意到我们,直到我们四次请他给我们拿菜单。食物也不例外。他们发现我们吃完了,没有点更多的菜后,花了2分钟才给我们送来一张支票。好吧,再也不会了。”,
“我去了XYZ餐厅,经历了一次可怕的经历。我有一张免费开胃券,可以在登记入住时使用。为我们服务的人非常粗鲁,没有承认优惠券。当我问她这件事时,她粗鲁地回答道,她已经申请了。然后我询问了免费沙拉的情况。”她粗鲁地说,你必须点主菜才能吃到。总的来说,我有一段不愉快的经历,因为我第一次带家人去那家餐厅,我对那家餐厅寄予厚望,否则,那家餐厅是我最喜欢的用餐场所。”
)),.Names=c(“谎言”、“情绪”、“评论”),class=c(“数据表”,
“data.frame”),row.names=c(NA,-6L),.internal.selfref=)
R代码:

library(gmodels)

lie<- fread('deception.csv',header = T,fill = T,quote = "\'")
str(lie)
lie
#Corpus Building
words.vec<- VectorSource(lie$review)
words.corpus<- Corpus(words.vec)
words.corpus<-tm_map(words.corpus,content_transformer(tolower)) #lower case
words.corpus<-tm_map(words.corpus,removePunctuation) # remove punctuation
words.corpus<-tm_map(words.corpus,removeNumbers) # remove numbers
words.corpus<-tm_map(words.corpus,removeWords,stopwords('english')) # remove stopwords
words.corpus<-tm_map(words.corpus,stripWhitespace) # remove unnecessary whitespace

#==========================================================================
#Document term Matrix
dtm<-DocumentTermMatrix(words.corpus)
dtm
class(dtm)

#dtm_df<-as.data.frame(as.matrix(dtm))
#class(dtm_df)

freq <- colSums(as.matrix(dtm))
length(freq)
ord <- order(freq,decreasing=TRUE)
freq[head(ord)]
freq[tail(ord)]

#===========================================================================
#Data frame partition
#Splitting DTM

dtm_train <- dtm[1:61, ]
dtm_test <- dtm[62:92, ]

train_labels <- lie[1:61, ]$lie
test_labels <-lie[62:92, ]$lie

str(train_labels)
str(test_labels)

prop.table(table(train_labels))
prop.table(table(test_labels))


freq_words <- findFreqTerms(dtm_train, 10)
freq_words
dtm_freq_train<- dtm_train[ , freq_words]
dtm_freq_test <- dtm_test[ , freq_words]
dtm_freq_test


convert_counts <- function(x) {
  x <- ifelse(x > 0, 'yes','No')
}

train <- apply(dtm_freq_train, MARGIN = 2, convert_counts)
test <- apply(dtm_freq_test, MARGIN = 2, convert_counts)
str(test)


nb_classifier<-naiveBayes(train,train_labels)
nb_classifier

test_pred<-predict(nb_classifier,test)
库(gmodels)
lieNaive Bayes要求响应变量作为分类类变量:
将
lie
数据帧的
lie
列转换为
factor
并重新运行分析:

lie$lie <- as.factor(lie$lie)
lie$lie
lie$lie <- as.factor(lie$lie)