2017-02-03 5 views
0

私は94のテキストを分類しようとしています。 trainsetのカテゴリがtestsetのカテゴリに存在しない場合、naiveBayesはうまく動作しないため、私はランダム化して確認しました。 カテゴリに問題はありませんでした。 しかし、クラシファイアはtestsetで動作しませんでした。続き はエラーメッセージです:Rの素朴なベイズエラー:下付き文字の範囲外

Df.dtm<-cbind(Df.dtm, category) 
dim(Df.dtm) 
Df.dtm[1:10, 530:532] 

# Randomize and Split data by rownumber 
train <- sample(nrow(Df.dtm), ceiling(nrow(Df.dtm) * .50)) 
test <- (1:nrow(Df.dtm))[- train] 

# Isolate classifier 
cl <- Df.dtm[, "category"] 
> summary(cl[train]) 
    dip eds ind pols 
    23 8 3 13 

# Create model data and remove "category" 
modeldata <- Df.dtm[,!colnames(Df.dtm) %in% "category"] 

#Boolean feature Multinomial Naive Bayes 
#Function to convert the word frequencies to yes and no labels 
convert_count <- function(x) { 
    y <- ifelse(x > 0, 1,0) 
    y <- factor(y, levels=c(0,1), labels=c("No", "Yes")) 
    y 
} 

#Apply the convert_count function to get final training and testing DTMs 
train.cc <- apply(modeldata[train, ], 2, convert_count) 
test.cc <- apply(modeldata[test, ], 2, convert_count) 

#Training the Naive Bayes Model 
#Train the classifier 
system.time(classifier <- naiveBayes(train.cc, cl[train], laplace = 1)) 

この分類器はうまく: 流逝系统用户0.45 0.00 0.46

#Use the classifier we built to make predictions on the test set. 
system.time(pred <- predict(classifier, newdata=test.cc)) 

をしかし、予測に失敗しました。 [.default(オブジェクト$表は[[V]]、ND)で エラー:下标出界 タイミングで停止:0.2 0 0.2

答えて

0

は、次のことを考えてみましょう:

# Indicies of training observations as observations. 
train <- sample(nrow(Df.dtm), ceiling(nrow(Df.dtm) * .50)) 

# Indicies of whatever is left over from the previous sample, again, also observations are being returned. 
#that still remains inside of Df.dtm, notation as follows: 
test <- Df.dtm[-train,] 

何私をクリアした後私はテストセットをスライスしたいのですが(この時点で行や列を設定する必要があります)、apply関数を引数のあるhere is a link of how the apply function worksと微調整しますが、時間のために、あなたがそれを渡す場合2あなたはそれぞれcolumnに適用し、それを渡す場合1それは適用されます関数はそれぞれrowに与えられます。繰り返しますが、サンプル(行または列)をどのようにしたいかに応じて、これをいずれかの方法で調整できます。

関連する問題