๐ข ๋ถ๊ท ํ ๋ฐ์ดํฐ ๋ถ์(ROSE package, ovunsample() / rose()
๐ต ๋ถ๊ท ํ ๋ฐ์ดํฐ ๋ถ์ (๋ถ๋ฅ๋ชจํ)
์ค๋ฌด์์ ์ผํ๋ค๋ณด๋ฉด, ์์ธก๋ณ์์ ํด๋์ค๊ฐ ์ ์ ๊ฒฝ์ฐ๋ค ํํ๋ค๊ณ ํ๋ค. ๋ฌผ๋ก ๋์ ๊ฒฝ์ฐ ๋ถ๋ฅ ๋ถ์๋ณด๋ค๋ ์๊ณ์ด ๋ถ์์ ๋ง์ด ํ๋ค๋ณด๋ ์กฐ๊ธ ๋ค๋ฅธ ์๋ก์ฌํญ์ด ์๊ธด ํ์ง๋ง, ์ด๋ฌํ ๊ฒฝ์ฐ๊ฐ ํํ๊ธฐ ๋๋ฌธ์ ๋ถ์์ ํฐ ์ฐจ์ง์ด ์์ ์ ์๋ค. ์๋ํ๋ฉด, ํ์ชฝ์ผ๋ก ์น์ฐ์น unbalancedํ ๋ฐ์ดํฐ๋ ๋ชจํ์ ๊ตฌ์ถํ์ ๋, ํธํฅ๋ ์ฑํฅ์ ๊ฐ๊ฒ ๋๊ธฐ ๋๋ฌธ์ด๋ค. ์ฆ ๊ฐ์ค์น์ ๋ฌธ์ ์ด๋ค. ์ด๋ฌํ ์ ์ ์ฌ๋ฌ ๋ถ์ผ์์ ํฐ ๋ฌธ์ ๊ฐ ๋ ์ ์๋๋ฐ, ํด๋น ๋ถ์ผ๊ฐ ๋ฐ๋ก ์ ์ฉ์นด๋์ ์๋ฃ์ ๊ฐ์ ๋ถ์ผ์ด๋ค. ์๋ฅผ ๋ค์ด์ ์ ์ฉ์นด๋์ ๊ฒฝ์ฐ ๋งค๋ ์ฝ 2%๊ฐ ๋์ฉ๋๋ค๊ณ ํ๋ฉฐ, ์ง๋ณ๊ฒ์ฌ์ ์์ด์ ํฌ๊ท๋ณ ๋ฐ๋ณ๋ฅ ์ด 0.4%์ ๋ถ๊ณผํ๋ค. ๋ฐ๋ผ์ ์ด๋ฌํ ๋ฐ์ดํฐ๋ก ๋ชจํ์ ๊ตฌ์ถํ์ ๊ฒฝ์ฐ, ์ ์ฉ์นด๋๋ฅผ ๋์ฉํ์ง๋ง ๋๋ ํฌ๊ท๋ณ์ ๊ฑธ๋ ธ์ง๋ง, ๊ตฌ์ถํ ๋ชจํ์ด ํ๋จํ์ ๋, ๊ทธ๋ ์ง ์๋ค๋ ๊ฒฐ๋ก ์ ์ด๋์ด ๋ผ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. ์ฆ ์ด ๋ถ๋ถ์ ํ์คํ ์์ธก๋ชจํ๊ณผ๋ ๋ค๋ฅธ ์๋ก์ฌํญ์ธ ๊ฒ ๊ฐ๋ค. ๋ฐ๋ผ์ ์ด๋ฌํ ๋ถ๋ถ์ ๋ณด์ํ๊ธฐ ์ํด์ ๋ช ๊ฐ์ง ๋ฐฉ๋ฒ๋ก ์ด ์กด์ฌํ๋๋ฐ, ์ค๋์ ์ด๋ฅผ ์ด์ผ๊ธฐํด๋ณด๊ณ ์ ํ๋ค.
- ์๋ฌด๊ฒ๋ ๋์ํ์ง ์๋๋ค.
- Oversampling
- Undersampling
- ์์ ํ๋ณธ ๋ฐ์ดํฐ๋ฅผ ์กฐํฉํด์ ์์ฑ
- ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํด๋์ค ๋ฐ์ดํฐ๋ฅผ ๋ฏธ์ธ ์กฐ์ ํ๋ค.
- ํด๋์ค ๊ฐ์ค์น๋ฅผ ์กฐ์ ํ๋๊ฐ
- ์ปท์ค๋ธ ๊ธฐ์ค์ ์กฐ์ (ํด๋์ค ๊ตฌ๋ถ ๊ธฐ์ค์ ์กฐ์ )
- ์์ ํ๋ณธ ๋ฐ์ดํฐ์ ์ข๋ ๋ฏผ๊ฐํ๊ฒ ๋ฐ์ํ๋๋ก ์๊ณ ๋ฆฌ์ฆ์ ์กฐ์ ํ๋ค.
์ด๋ฌํ ๋ฐฉ๋ฒ๋ค์ด ์กด์ฌํ๋ค.
์ค๋์ ๋ถ๊ท ํ ๋ฐ์ดํฐ ๋ถ์ ์์๋ก ๋ง์ด ํ์ฉ๋๋ ROSE
ํจํค์ง์ hacide
๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด๋ณด์
install.packages("ROSE")
library(ROSE)
data(hacide)
ํด๋น ํจํค์ง ์์ ovun.sample์ด๋ ํจ์๋ฅผ ํตํด์ ์์์ ์ธ๊ธํ sampling์ ์งํํ ์ ์๋ค.
๊ณต์๋ฌธ์์ ๋ฐ๋ฅด๋ฉด ovun.sample ํจ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค๊ณ ํ๋ค.
name.function | method |
---|---|
Ovun.sample | Over-sampling, under-sampling, combination of over- and undersampling. |
Usage
ovun.sample(formula, data, method="both", N,p=0.5,
subset=options("subset")$subset,
na.action=options("na.action")$na.action, seed)
formula
: ์์ธกํ ๋ณ์ R์์ ๊ถ์ฅํ๋ ์์ ํํ๋ก ๋ฃ์ด์ค๋ค
data
: ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ค๋ค
method
: c("over", "under", "both")
์ค ํ๋๋ฅผ ์ ํํด์ ๋ฃ์ด์ค๋ค. ์ด๋ over
๋ oversample
, under
๋ undersample
, both
๋ ์์ชฝ(ํด๋์ค ๋ ๋ค ๋จ๋ค์ผ๋ก)์ ๋ฝ๋๋ค๋ก ์๊ฐํ๋ฉด ๋๊ฒ ๋ค.
N
: ์ํ๋ ๋ฐ์ดํฐ์ ํฌ๊ธฐ์ด๋ค. ์ด๋ ์ฃผ์์ ์ด ์๋๋ฐ, ํด๋น ๋ถ๋ถ์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๊ธธ ๋ฐ๋๋ค.
๋๋จธ์ง ์ธ์ ๋๋ ์ต์ ๋ ์ฌ์ฉ๋ฐฉ๋ฒ์ด ์ด๋ ต์ง ์๊ธฐ ๋๋ฌธ์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๊ธฐ ๋ฐ๋๋ค.
https://cran.r-project.org/web/packages/ROSE/ROSE.pdf
์ค์ํ๊ฑด ovun.sampleํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฒฐ๊ณผ๊ฐ์ด ๋ฆฌ์คํธ ํํ์ด๋ค.
oversampling <- ovun.sample(cls ~ ., data = hacide.train, method = "over", N = 1960)
class(oversampling)
[1] "ovun.sample"
class(oversampling[1])
[1] "list"
๋ฐ๋ผ์ $
๋ฅผ ํ์ฉํ์ฌ ๋ฐ์ดํฐ๋ง ๋ฝ์์ฃผ์. ์ด์ฐจํผ ์ด๊ฑด๋ง ์ฌ์ฉํ ๊ฑฐ๋๊น.
oversampling <- ovun.sample(cls ~ ., data = hacide.train, method = "over", N = 1960)$data
class(oversampling)
[1] "data.frame"
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ $data
๋ฅผ ์ฌ์ฉํ๋ฉด data.frame ํํ๋ก class๊ฐ ์ถ๋ ฅ๋จ์ ์ ์ ์๋ค.
# OverSampling
oversampling <- ovun.sample(cls ~ ., data = hacide.train, method = "over", N = 1960)$data
# UnderSampling
undersampling <- ovun.sample(cls ~ ., data = hacide.train, method = "under", N = 40, seed = 1)$data
# BothSampling
bothsampling <- ovun.sample(cls ~ ., data = hacide.train, method = "both", p = 0.5, N = 1000, seed = 1)$data
# ROSESampling
rose <- ROSE(cls ~ ., data = hacide.train)$data
# ๋๋ฌด ๋ชจํ
raw <- rpart(cls ~ ., data = hacide.train) # ์๋ฌด๊ฒ๋ ์ํจ
over <- rpart(cls ~ ., data = oversampling) # oversample
under <- rpart(cls ~ ., data = undersampling) # undersample
both <- rpart(cls ~ ., data = bothsampling) # both
rose <- rpart(cls ~ ., data = rose) # rosesample
# ์์ธก
pred_raw <- predict(raw , newdata = hacide.test)
pred_over <- predict(over , newdata = hacide.test)
pred_under <- predict(under, newdata = hacide.test)
pred_both <- predict(both , newdata = hacide.test)
pred_rose <- predict(rose , newdata = hacide.test)
# AUC ๋์ด (1์ ๊ฐ๊น์ธ์๋ก ์ข์ ๋ชจํ)
roc.curve(hacide.test$cls, pred_raw[,2], plot=FALSE) # 0.600
roc.curve(hacide.test$cls, pred_over[,2], plot=FALSE) # 0.798
roc.curve(hacide.test$cls, pred_under[,2], plot=FALSE) # 0.924
roc.curve(hacide.test$cls, pred_both[,2], plot=FALSE) # 0.798
roc.curve(hacide.test$cls, pred_rose[,2], plot=FALSE) # 0.985
#
#
#
์ฐธ๊ณ ๋ก roc.curve
๋ ROSE
ํจํค์ง ๋ด์ ์กด์ฌํ๋ค. ์ค์ test ๋ฐ์ดํฐ์ ์์ธก ๋ฐ์ดํฐ๋ฅผ ๋น๊ตํ๋ ๊ฒ์ด๋ค. plot์ TRUE
๋ก ๋ฐ๊พธ๋ฉด plot์ ๊ทธ๋ฆด ์ ์๋ค.
ROSE
ํจํค์ง์ ovun.sample()
์์ง๋ง์. under, over, both๋ฅผ ์ ํํ๊ฑฐ๋, rose()
ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.