2017-02-03 10 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)) 

: 이 流逝

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

0.45 0.00 0.46

그러나, 예측이 실패 系统 用户 다음 는 오류 메시지입니다. 오류 [.default에서 (객체 $ 테이블은 [[V], 차이) : 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,] 

무엇 내를 샘플 반환 된 (행 indicies) 및 내 테스트 집합 (다시, 행 또는 열을이 시점에서 설정해야합니다) 위로 슬라이스 싶었어, 나는 apply 인수가 필요한 함수를 조정할 것입니다. 그러나 시간을 위해서 , 패스하면 2column에 적용하고 을 전달하면 함수는 각각 row에 주어진다. 다시 말하지만 샘플 (행 또는 열)을 원하는 방식에 따라이 방법을 조정할 수 있습니다.