| seed = 1, k= 11 | seed = 1, k = 21 |
정확도 | 1 | 1 |
카파통계량 | 1 | 1 |
정밀도 | 1 | 1 |
재현율 | 1 | 1 |
민감도 | 1 | 1 |
특이도 | 1 | 1 |
wbcd <- read.csv("wisc_bc_data.csv", header=T, stringsAsFactors=FALSE)
wbcd$diagnosis <- factor( wbcd$diagnosis,
levels= c("B","M"),
labels=c("Benign", "Maliganant") )
str(wbcd)
set.seed(1) # 1부터 10까지의 숫자를 랜덤으로 섞어서 출력하는 코드
wbcd_shuffle <- wbcd[ sample(569), ] # 설명: wbcd[ 행, 열 ]
wbcd_shuffle
wbcd2 <- wbcd_shuffle[ , -1 ]
str(wbcd2)
normalize <- function(x) {
return ( (x-min(x)) / ( max(x) - min(x) ) )
}
wbcd_n <- as.data.frame( lapply( wbcd2[ , 2:31], normalize) )
nrow( wbcd_n ) # 569
train_num <- round( 0.9 * nrow(wbcd_n), 0 )
train_num # 512
wbcd_train <- wbcd_n[ 1:train_num, ]
wbcd_test <- wbcd_n[ (train_num+1) : nrow(wbcd_n), ]
nrow(wbcd_test) # 57
wbcd_train_label <- wbcd2[ 1:train_num, 1 ]
wbcd_test_label <- wbcd2[ (train_num+1) : nrow(wbcd_n), 1 ]
wbcd_test_label
library(class)
#result1 <- knn(train=wbcd_train, test=wbcd_test, cl=wbcd_train_label, k=21)
result1 <- knn(train=wbcd_train, test=wbcd_test, cl=wbcd_train_label, k=11)
result1
data.frame( result1, wbcd_test_label)
sum( result1 == wbcd_test_label )
x <- data.frame('실제'=wbcd_test_label, '예측'=result1)
table(x)
################
positive_value <- 'Maliganant' # 악성
negative_value <- 'Benign' # 양성
#■ 카파통계량
library(vcd)
table( result1, wbcd_test_label)
Kappa( table( result1, wbcd_test_label) )
#■ 정확도
library(gmodels)
g <- CrossTable( result1, wbcd_test_label )
x <- sum(g$prop.tbl *diag(2)) # 정확도 확인하는 코드
x
#■ 민감도
#install.packages("caret")
library(caret)
sensitivity( result1, wbcd_test_label,
positive=positive_value)
#■ 특이도
specificity( result1, wbcd_test_label,
negative=negative_value)
#■ 정밀도
posPredValue( result1, wbcd_test_label,
positive=positive_value)
#■ 재현율
sensitivity( result1, wbcd_test_label,
positive=positive_value)