훈련에서 데이터를 분할하는 방법 & #038; r의 테스트 세트(3가지 방법)
종종 기계 학습 알고리즘을 데이터 세트에 적용할 때 먼저 데이터 세트를 훈련 세트와 테스트 세트로 나눕니다.
R에서 데이터를 훈련 세트와 테스트 세트로 분할하는 세 가지 일반적인 방법이 있습니다.
방법 1: 기본 R 사용
#make this example reproducible set. seeds (1) #use 70% of dataset as training set and 30% as test set sample <- sample(c( TRUE , FALSE ), nrow(df), replace= TRUE , prob=c( 0.7 , 0.3 )) train <- df[sample, ] test <- df[!sample, ]
방법 2: caTools 패키지 사용
library (caTools) #make this example reproducible set. seeds (1) #use 70% of dataset as training set and 30% as test set sample <- sample. split (df$any_column_name, SplitRatio = 0.7 ) train <- subset(df, sample == TRUE ) test <- subset(df, sample == FALSE )
방법 3: dplyr 패키지 사용
library (dplyr) #make this example reproducible set. seeds (1) #create ID column df$id <- 1:nrow(df) #use 70% of dataset as training set and 30% as test set train <- df %>% dplyr::sample_frac( 0.70 ) test <- dplyr::anti_join(df, train, by = ' id ')
다음 예에서는 R에 내장된 붓꽃 데이터 세트를 사용하여 실제로 각 방법을 사용하는 방법을 보여줍니다.
예 1: Base R을 사용하여 데이터를 훈련 세트와 테스트 세트로 분할
다음 코드는 R 베이스를 사용하여 붓꽃 데이터 세트를 훈련 세트와 테스트 세트로 분할하고 행의 70%를 훈련 세트로 사용하고 나머지 30%를 테스트 세트로 사용하는 방법을 보여줍니다.
#load iris dataset data(iris) #make this example reproducible set. seeds (1) #Use 70% of dataset as training set and remaining 30% as testing set sample <- sample(c( TRUE , FALSE ), nrow(iris), replace= TRUE , prob=c( 0.7 , 0.3 )) train <- iris[sample, ] test <- iris[!sample, ] #view dimensions of training set sun(train) [1] 106 5 #view dimensions of test set dim(test) [1] 44 5
결과에서 우리는 다음을 볼 수 있습니다:
- 훈련 세트는 106개의 행과 5개의 열로 구성된 데이터 프레임입니다.
- 테스트는 44행 5열의 데이터 블록입니다.
원본 데이터베이스에는 총 150개의 행이 있으므로 훈련 세트에는 원본 행의 약 106/150 = 70.6%가 포함됩니다.
원하는 경우 훈련 세트의 처음 몇 행을 표시할 수도 있습니다.
#view first few rows of training set
head(train)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
예 2: caTools를 사용하여 데이터를 교육 및 테스트 세트로 분할
다음 코드는 R의 caTools 패키지를 사용하여 붓꽃 데이터 세트를 훈련 및 테스트 세트로 분할하고 행의 70%를 훈련 세트로 사용하고 나머지 30%를 테스트 세트로 사용하는 방법을 보여줍니다.
library (caTools) #load iris dataset data(iris) #make this example reproducible set. seeds (1) #Use 70% of dataset as training set and remaining 30% as testing set sample <- sample. split (iris$Species, SplitRatio = 0.7 ) train <- subset(iris, sample == TRUE ) test <- subset(iris, sample == FALSE ) #view dimensions of training set sun(train) [1] 105 5 #view dimensions of test set dim(test) [1] 45 5
결과에서 우리는 다음을 볼 수 있습니다:
- 훈련 세트는 105개의 행과 5개의 열로 구성된 데이터 프레임입니다.
- 테스트는 45행 5열의 데이터 블록입니다.
예시 3: dplyr을 사용하여 데이터를 훈련 세트와 테스트 세트로 분할
다음 코드는 R의 caTools 패키지를 사용하여 붓꽃 데이터 세트를 훈련 및 테스트 세트로 분할하고 행의 70%를 훈련 세트로 사용하고 나머지 30%를 테스트 세트로 사용하는 방법을 보여줍니다.
library (dplyr) #load iris dataset data(iris) #make this example reproducible set. seeds (1) #create variable ID iris$id <- 1:nrow(iris) #Use 70% of dataset as training set and remaining 30% as testing set train <- iris %>% dplyr::sample_frac( 0.7 ) test <- dplyr::anti_join(iris, train, by = ' id ') #view dimensions of training set sun(train) [1] 105 6 #view dimensions of test set dim(test) [1] 45 6
결과에서 우리는 다음을 볼 수 있습니다:
- 훈련 세트는 105개의 행과 6개의 열로 구성된 데이터 프레임입니다.
- 테스트는 45행 6열의 데이터 블록입니다.
이러한 학습 및 테스트 세트에는 우리가 만든 추가 “id” 열이 포함되어 있습니다.
기계 학습 알고리즘을 조정할 때 이 열을 사용하지 마십시오(또는 데이터 프레임에서 완전히 제거하십시오).
추가 리소스
다음 튜토리얼에서는 R에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.