2016-10-17 8 views
1

R에서 KNN 텍스트 분류를위한 :KFold 교차 검증 내가 사용 그런 내가 분류를 수행 한 <pre><code> Comment Category Good Service provided Service Excellent Communication Communication </code></pre> <p></p> 등 다양한 범주로 의견을 분류 텍스트 분류 만든

knn(modeldata[train, ], modeldata[test,] , cl[train], k =2, use.all = TRUE) 

지금 내가 원하는을 K-Fold Cross Validation을 사용하여이 모델을 평가하십시오. 난 내가 모델 overfitting 또는 내가

knn.cv(modeldata[train, ], cl[train], k =2, use.all = TRUE) 

을 사용했다 그러나이 명령의 도움이 모델은 혼동하는 경우가 NA를 반환 할 것이라고 말했습니다 등

를 단순성과 있는지 알고하는 데 사용할 수있는 번호를 기대하고있다. 안내해주세요

답변

1

knn에 어떤 패키지를 사용하고 있습니까? 다음 (홍채 데이터 세트와 예)처럼 CV에 대한 caret를 사용할 수 있습니다

training <- iris 
ctrl <- trainControl(method="repeatedcv",repeats = 3) 
knnFit <- train(Species ~ ., data = training, method = "knn", 
       trControl = ctrl, preProcess = c("center","scale")) 
knnFit 

를 출력

k-Nearest Neighbors 

150 samples 
    4 predictor 
    3 classes: 'setosa', 'versicolor', 'virginica' 

Pre-processing: centered (4), scaled (4) 
Resampling: Cross-Validated (10 fold, repeated 3 times) 
Summary of sample sizes: 135, 135, 135, 135, 135, 135, ... 
Resampling results across tuning parameters: 

    k Accuracy Kappa  
    5 0.9511111 0.9266667 
    7 0.9577778 0.9366667 
    9 0.9533333 0.9300000 

Accuracy was used to select the optimal model using the largest value. 
The final value used for the model was k = 7. 
+0

내가 KNN은 "클래스"패키지를 사용하고와. 홍채 데이터 세트의 열 개수로 인해 위의 코드가 홍채 데이터 세트에서 작동하지만 내 데이터 세트 (두 개의 열만 있음)에서 작동하지 않는지 확실하지 않습니다. 위의 명령을 실행하면 메시지 아래에 나타납니다. thresh = 0.95, k = 5, method = c ("center", : ) 이러한 변수의 분산은 0입니다. – Sourabh

+0

다음 문장을 시도했지만 오류 메시지 : knControl = trainControl (method = "cv", number = 5, .classProbs = FALSE) knnFit1 <- train (Category ~ Text, data = x, method = "knn", preProcess = NULL, 오류 메시지 : 결과에있는 하나 이상의 요인 수준에는 데이터가 없습니다. 모든 요인을 살펴 보았지만 공백/빈 수준을 찾지 못했습니다. – Sourabh