2016-04-30 20 views
0

私は、(Document Term Matrixを構築して)テキストデータから予測を行うためにNaive BayesをRで実行しようとしています。Naive Bayesの問題

私は訓練とテストセットの両方で欠けている可能性のある用語についていくつかの記事を読んでいました。そのため、1つのデータフレームで作業し、後で分割することにしました。私が使用しているコードはこれです:

   actual 
predicted 1 2 3 
     1 60 833 107 
     2 0 0 0 
     3 0 0 0 

なぜこれが起こっているの任意のアイデア:

data <- read.csv(file="path",header=TRUE) 

########## NAIVE BAYES 
library(e1071) 
library(SparseM) 
library(tm) 

# CREATE DATA FRAME AND TRAINING AND 
# TEST INCLUDING 'Text' AND 'InfoType' (columns 8 and 27) 
traindata <- as.data.frame(data[13000:13999,c(8,27)]) 
testdata <- as.data.frame(data[14000:14999,c(8,27)]) 
complete <- as.data.frame(data[13000:14999,c(8,27)]) 

# SEPARATE TEXT VECTOR TO CREATE Source(), 
# Corpus() CONSTRUCTOR FOR DOCUMENT TERM 
# MATRIX TAKES Source() 
completevector <- as.vector(complete$Text) 

# CREATE SOURCE FOR VECTORS 
completesource <- VectorSource(completevector) 

# CREATE CORPUS FOR DATA 
completecorpus <- Corpus(completesource) 

# STEM WORDS, REMOVE STOPWORDS, TRIM WHITESPACE 
completecorpus <- tm_map(completecorpus,tolower) 
     completecorpus <- tm_map(completecorpus,PlainTextDocument) 
     completecorpus <- tm_map(completecorpus, stemDocument) 
completecorpus <- tm_map(completecorpus, removeWords,stopwords("english")) 
     completecorpus <- tm_map(completecorpus,removePunctuation) 
     completecorpus <- tm_map(completecorpus,removeNumbers) 
     completecorpus <- tm_map(completecorpus,stripWhitespace) 

# CREATE DOCUMENT TERM MATRIX 
completematrix<-DocumentTermMatrix(completecorpus) 
trainmatrix <- completematrix[1:1000,] 
testmatrix <- completematrix[1001:2000,] 

# TRAIN NAIVE BAYES MODEL USING trainmatrix DATA AND traindata$InfoType CLASS VECTOR 
model <- naiveBayes(as.matrix(trainmatrix),as.factor(traindata$InfoType),laplace=1) 

# PREDICTION 
results <- predict(model,as.matrix(testmatrix)) 
conf.matrix<-table(results, testdata$InfoType,dnn=list('predicted','actual')) 

conf.matrix 

の問題は、私はこのような奇妙な結果を得ているということでしょうか?

生データは次のようになります。

head(complete) 

     Text 
13000 Milkshakes, milkshakes, whats not to love? Really like the durability and weight of the cup. Something about it sure makes good milkshakes.Works beautifully with the Cuisinart smart stick. 
13001 excellent. shipped on time, is excellent for protein shakes with a cuisine art mixer. easy to clean and the mixer fits in perfectly 
13002 Great cup. Simple and stainless steel great size cup for use with my cuisinart mixer. I can do milkshakes really easy and fast. Recommended. No problems with the shipping. 
13003 Wife Loves This. Stainless steel....attractive and the best part is---it won't break. We are considering purchasing another one because they are really nice. 
13004 Great! Stainless steel cup is great for smoothies, milkshakes and even chopping small amounts of vegetables for salads!Wish it had a top but still love it! 
13005 Great with my. Stick mixer...the plastic mixing container cracked and became unusable as a result....the only downside is you can't see if the stuff you are mixing is mixed well 

     InfoType 
13000  2 
13001  2 
13002  2 
13003  3 
13004  2 
13005  2 
+0

データなしでデバッグするのは難しいです。列車を分割して特定の行でテストしています。これらの行にすべてのクラスが含まれていない可能性があります。あなたはテスト/トレインスプリットのためにランダムにサンプリングする方が良いです。 – Gopala

+0

いいえ、それは動作しませんでした。私はランダムに行を分割しようとしたが、私はまったく同じ結果を得た。 – JorgeF

+0

あなたの混乱マトリクス(予測されたv実際)は、すべての実際のアイテムがクラス1であることを示しています。 – patrick

答えて

0

一見問題は、TDMはそんなにスパース性を取り除くために必要であるということです。そこで、私は次のように追加しました:

completematrix<-removeSparseTerms(completematrix, 0.95) 

そしてそれは働き始めました!

   actual 
predicted 1 2 3 
     1 60 511 6 
     2 0 86 2 
     3 0 236 99 

(Chelseyヒルありがとう!!)あなたのアイデアありがとうございまし

関連する問題