Database for Everything

[R 프로그램] 의사결정나무 - 사과의 품질 분류하기 본문

R

[R 프로그램] 의사결정나무 - 사과의 품질 분류하기

Yeenn 2021. 12. 9. 10:53
728x90

이번 실습을 위한 데이터는 아래에서 다운받을 수 있다. (apple.csv)

apple.csv
0.00MB

 

데이터 Load 후 살펴보기

#의사결정나무
apple <- read.csv("apple.csv")
apple
      model weight sugar acid color
1    아오리    286  12.9 0.31  홍색
2      홍옥    256  13.4 0.69  적색
3    아오리    251  12.1 0.32  홍색
4    미시마    396  16.3 0.39  홍색
5    아오리    282  15.0 0.29  홍색
6      홍로    342  13.4 0.31  홍색
7  로얄후지    407  13.6 0.40  적색
8    아오리    238  13.5 0.31  홍색
9      홍로    295  15.1 0.29  홍색
10 로얄후지    392  15.6 0.38  적색
11   아오리    298  13.3 0.38  홍색
12     홍로    352  14.4 0.28  홍색
13   미시마    409  14.2 0.39  홍색
14     홍로    152  13.5 0.32  홍색
15     홍로    329  13.8 0.31  홍색
16 로얄후지    394  13.0 0.38  적색
17   미시마    408  14.1 0.39  홍색
18     홍옥    224  12.1 0.73  적색
19     홍옥    211  13.4 0.64  적색
20     홍옥    235  14.2 0.65  적색
21     홍옥    239  12.3 0.72  적색
22   미시마    380  16.7 0.38  홍색
23 로얄후지    381  14.2 0.40  적색
24   미시마    391  16.8 0.41  홍색
25 로얄후지    384  13.3 0.41  적색

summary(apple)
   model               weight          sugar            acid           color          
 Length:25          Min.   :152.0   Min.   :12.10   Min.   :0.2800   Length:25         
 Class :character   1st Qu.:251.0   1st Qu.:13.30   1st Qu.:0.3100   Class :character  
 Mode  :character   Median :329.0   Median :13.60   Median :0.3800   Mode  :character  
                    Mean   :317.3   Mean   :14.01   Mean   :0.4192                     
                    3rd Qu.:391.0   3rd Qu.:14.40   3rd Qu.:0.4100                     
                    Max.   :409.0   Max.   :16.80   Max.   :0.7300   
                    
str(apple)
'data.frame':	25 obs. of  5 variables:
 $ model : chr  "아오리" "홍옥" "아오리" "미시마" ...
 $ weight: int  286 256 251 396 282 342 407 238 295 392 ...
 $ sugar : num  12.9 13.4 12.1 16.3 15 13.4 13.6 13.5 15.1 15.6 ...
 $ acid  : num  0.31 0.69 0.32 0.39 0.29 0.31 0.4 0.31 0.29 0.38 ...
 $ color : chr  "홍색" "적색" "홍색" "홍색" ...

 

 

 

품종별 분포 살펴보기

#품종별 무게분포
boxplot(weight~model, data=apple)

 

#품종별 당도분포
boxplot(sugar~model, data=apple)

 

#품종별 산도분포
boxplot(acid~model, data=apple)

 

#사과품종
ggplot(data=apple, aes(x=color, fill=model))+geom_bar()

 

데이터를 살펴보니, 사과 품종별로 서로 다른 특성이 존재한다는 것을 알 수 있었다.

 

 

 

 

Train/Test 데이터 Split

#Split
library(caret)
apple_index <- createDataPartition(apple$model, p=0.8, list=F)
apple_index
     Resample1
 [1,]         1
 [2,]         2
 [3,]         3
 [4,]         4
 [5,]         5
 [6,]         6
 [7,]         7
 [8,]         8
 [9,]        12
[10,]        13
[11,]        14
[12,]        15
[13,]        16
[14,]        17
[15,]        18
[16,]        19
[17,]        20
[18,]        23
[19,]        24
[20,]        25

 

caret 패키지를 통해 train/test 데이터를 8:2 비율로 나누었다.

 

 

apple_train <- apple[apple_index, ]
apple_test <- apple[-apple_index, ]
nrow(apple_train)
[1] 20
nrow(apple_test)
[1] 5

 

 

 

 

분류분석 실행

#분류분석
library(rpart)

apple_model <- rpart(model~., data=apple_train, control=rpart.control(minsplit=2))
apple_model
n= 20 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

 1) root 20 16 로얄후지 (0.2 0.2 0.2 0.2 0.2)  
   2) acid< 0.53 16 12 로얄후지 (0.25 0.25 0.25 0.25 0)  
     4) weight>=366 8  4 로얄후지 (0.5 0.5 0 0 0)  
       8) color=적색 4  0 로얄후지 (1 0 0 0 0) *
       9) color=홍색 4  0 미시마 (0 1 0 0 0) *
     5) weight< 366 8  4 아오리 (0 0 0.5 0.5 0)  
      10) weight< 290.5 4  0 아오리 (0 0 1 0 0) *
      11) weight>=290.5 4  0 홍로 (0 0 0 1 0) *
   3) acid>=0.53 4  0 홍옥 (0 0 0 0 1) *

 

 

 

 

모델 시각화

#시각화
install.packages("rpart.plot")
library(rpart.plot)
rpart.plot(apple_model)

 

rpart.plot 패키지를 로드하여 분류모델을 시각화하였다. 

 

 

 

예측 및 평가하기

#예측
predict(apple_model, apple_test, type="class")
 10       11       14       19       24 
로얄후지     홍로   아오리     홍옥   미시마 
Levels: 로얄후지 미시마 아오리 홍로 홍옥

#평가
apple_test
model weight sugar acid color
10 로얄후지    392  15.6 0.38  적색
11   아오리    298  13.3 0.38  홍색
14     홍로    152  13.5 0.32  홍색
19     홍옥    211  13.4 0.64  적색
24   미시마    391  16.8 0.41  홍색

 

predict 함수를 통해 apple_model을 예측한 후, 평가하였다. 

728x90
Comments