1. k=21 일 때,
result1 <- knn(train=wbcd_train, test=wbcd_test, cl=wbcd_train_label, k=21)
result1
data.frame( result1, wbcd_test_label)
sum( result1 == wbcd_test_label )
x <- data.frame('실제'=wbcd_test_label, '예측'=result1)
table(x)
actual_type <- wbcd_test_label # 테스트 데이터의 라벨
predict_type <- result1 # 테스트 데이터의 예측값
positive_value <- 'Benign' # 관심범주
negative_value <- 'Maliganant' # 관심범주 아닌 것
■ 이원교차표
library(gmodels)
CrossTable(wbcd_test_label, result1)
■ 정확도
acc<- CrossTable(actual_type, predict_type)
acc21 <- sum(acc$prop.tbl*diag(2) )
acc21 # 0.9649123
■ 카파통계량
library(vcd)
table(actual_type, predict_type)
Kappa(table(actual_type, predict_type) ) # 0.9287
■ 민감도
library(caret)
sensitivity(predict_type, actual_type, positive=positive_value) # 0.96875
■ 특이도
specificity(predict_type, actual_type, negative=negative_value ) # 0.96
■ 정밀도
posPredValue(predict_type, actual_type, positive=positive_value) # 0.96875
■ 재현율
sensitivity( predict_type, actual_type, positive=positive_value) #0.96875
2. k=11 일때,
result2 <- knn(train=wbcd_train, test=wbcd_test, cl=wbcd_train_label, k=11)
actual_type <- wbcd_test_label # 테스트 데이터의 라벨
predict_type <- result2 # 테스트 데이터의 예측값
positive_value <- 'Benign' # 관심범주
negative_value <- 'Maliganant' # 관심범주 아닌 것
■ 이원교차표
library(gmodels)
CrossTable(wbcd_test_label, result2)
■ 정확도
acc<- CrossTable(actual_type, predict_type)
acc11 <- sum(acc$prop.tbl*diag(2) )
acc11 # 1
■ 카파통계량
library(vcd)
table(actual_type, predict_type)
Kappa(table(actual_type, predict_type) ) # 1
■ 민감도
library(caret)
sensitivity(predict_type, actual_type, positive=positive_value) # 1
■ 특이도
specificity(predict_type, actual_type, negative=negative_value ) # 1
■ 정밀도
posPredValue(predict_type, actual_type, positive=positive_value) # 1
■ 재현율
sensitivity( predict_type, actual_type, positive=positive_value) #1