R로 배우는 Feature Engineering
Jorge Zazueta
Research Professor and Head of the Modeling Group at the School of Economics, UASLP
텍스트 값에 따라 각 carrier에 인덱스 번호를 부여합니다.

flights 데이터셋에는 carrier가 요인으로 포함되어 있으나, 새 데이터를 보면 새로운 carrier가 나타날지 알 수 없습니다.
flights %>%
select(carrier) %>%
table()
carrier
9E AA AS B6 DL EV F9 FL HA MQ OO UA US VX WN YV
859 1744 26 2503 2619 3014 38 186 14 1540 2 3367 1228 244 757 41
textrecipes 패키지를 사용해 요인 값을 나타내는 더미 해시를 만들 수 있습니다.
recipe <- recipe(~carrier,
data = flights_train) %>%
step_dummy_hash(carrier, prefix = NULL,
signed = FALSE,
num_terms = 50L)
# Prep the recipe
object <- recipe %>%
prep()
# Bake the recipe object with new data
baked <- bake(object,
new_data = flights_test)
step_dummy_hash() 표현을 살펴봅니다.
bind_cols(flights_test$carrier,baked)[1:6,c(1,18:20)]
New names:
• `` -> `...1`
# A tibble: 10 × 4
...1 `_carrier_17` `_carrier_18` `_carrier_19`
<chr> <int> <int> <int>
1 EV 0 0 0
2 B6 0 1 0
3 EV 0 0 0
4 MQ 0 0 0
5 DL 0 0 0
6 EV 0 0 0
plot.matrix 패키지로 행렬을 살펴볼 수 있습니다.
flights_hash <-
as.matrix(baked)[1:50,]
plot(flights_hash,
col = c("white","steelblue"),
key = NULL,
border = NA)

R로 배우는 Feature Engineering