자료 분할 :: 예측 분석 - mindscale
Skip to content

자료 분할

단순한 방법

set.seed(1234) # 랜덤하게 자료를 뽑을 때 결과가 동일하게 나오도록 설정
length_data <- dim(dat)[1]
TrainingIndex <- sample(1:length_data, round(length_data*0.8))

createDataPartition

  • sample 함수와 비슷함
  • caret에서 제공하는 방법 중 제일 간단한 방법
  • 중복을 허용하지 않고 자료의 일부분을 학습 자료로 나눌 수 있음
set.seed(1234)
TrainingIndex <- createDataPartition(y = dat$Class, p = 0.8, times = 1, list = F)
# y는 예측하고자 하는 예측 변수
# p는 사용하고자 하는 데이터의 비율
# times는 만들 샘플의 개수
# list는 list로 받을 것인지의 여부

TrainingIndex
# 144개의 데이터가 반환

Train_dat <- dat[TrainingIndex,]
Test_dat <- dat[-TrainingIndex,]

maxdissim

  • 최대한 다양한 자료를 포함하도록 하는 방법
  • 시간이 오래 걸림
  • 자료가 적은 경우 유용함
  • 내가 가지고 있는 것과 가장 유사하지 않은 자료를 하나씩 뽑아가는 방법
tmp <- sample(1:dim(dat)[1], 10) # 10개만 랜덤으로 뽑음
base <- dat[tmp,] # 가지고 있는 자료, 기준이 될 자료
pool <- dat[-tmp,] # 하나씩 뽑아올 자료
maxdiss <- maxDissim(base, pool, n = 140) #시간이 오래 걸림
Train_dat <- dat[maxdiss,]

createFolds

  • createDataPartition을 여러번 해주는 기능
  • 여러 번 시행함으로써 안정적인 결과를 얻을 수 있음
    Fold_5 <- createFolds(dat$Class, k = 5, list = T) # 5개의 fold 생성
    Fold_5
    Test_dat_fold1 <- dat[Fold_5[[1]],]
    Train_dat_fold1 <- dat[-Fold_5[[1]],]
    

createResample

  • 중복을 허용하여 자료를 뽑음
  • 부트스트래핑 방법이라고 할 수 있음
  • 중복을 허용했기 때문에 training set에 자료를 많이 넣어도 test set에도 자료가 많이 존재함
    resamp_dat <- createResample(dat$Class, times = 1)
    resamp_dat
    testIndex <- !(1:dim(dat)[1]) %in% unique(resamp_dat[[1]])
    # resamp_dat에 들어가 있는 것은 train으로 들어가 있지 않는 것은 test로 지정
    testIndex