거래 데이터

R로 배우는 Market Basket Analysis

Christopher Bruffaerts

Statistician

트랜잭션이란?

트랜잭션: 무언가를 사고파는 행위.

transaction

트랜잭션 데이터: 한 번의 구매에서 한 고객이 산 모든 품목 목록.

트랜잭션 예시:

  TID Product
1   1   Bread
2   1  Cheese
3   1  Cheese
4   1  Cheese
R로 배우는 Market Basket Analysis

R의 트랜잭션 클래스

transactions 클래스: 아이템셋/규칙 마이닝에 쓰이는 트랜잭션 데이터를 표현.

다음에서 변환 가능:

  • 리스트
  • 매트릭스
  • 데이터프레임

단, 먼저 데이터를 준비해야 합니다.

트랜잭션 데이터에서 중요한 점

  • 상품을 식별하는 필드/열

  • 트랜잭션을 식별하는 필드/열

R로 배우는 Market Basket Analysis

마트로 돌아가기 (1)

매장 트랜잭션 데이터

my_transactions = data.frame(
  "TID" = c(1,1,1,1, 2,2,2, 3,3, 4,4,4, 5,5, 6,6, 7,7),
  "Product" = c("Bread", "Cheese", "Cheese", "Cheese",
                "Bread", "Butter", "Wine",
                "Butter", "Butter",
                "Butter", "Wine", "Wine",
                "Butter", "Cheese",
                "Cheese", "Wine",
                "Wine", "Wine")
)

트랜잭션 미리보기

head(my_transactions, 10)
   TID Product
1    1   Bread
2    1  Butter
3    1  Cheese
4    1    Wine
5    2   Bread
6    2  Butter
7    2    Wine
8    3   Bread
9    3  Butter
10   4  Butter
R로 배우는 Market Basket Analysis

마트로 돌아가기 (2)

split 함수로 리스트 생성

# TID를 팩터로 변환
my_transactions$TID = 
  factor(my_transactions$TID)

# 그룹으로 분할
data_list = split(my_transactions$Product,
                   my_transactions$TID)
data_list
$`1`
[1] Bread  Butter Cheese Wine  
Levels: Bread Butter Cheese Wine

$`2`
[1] Bread  Butter Wine  
Levels: Bread Butter Cheese Wine

$`3`
[1] Bread  Butter
Levels: Bread Butter Cheese Wine
R로 배우는 Market Basket Analysis

마트로 돌아가기 (3)

transactions 클래스로 변환

# 트랜잭션 데이터셋으로 변환
data_trx = as(data_list,"transactions")

# 트랜잭션 확인
inspect(data_trx)

트랜잭션 데이터 확인

    items                      transactionID
[1] {Bread,Butter,Cheese,Wine} 1            
[2] {Bread,Butter,Wine}        2            
[3] {Bread,Butter}             3            
[4] {Butter,Cheese,Wine}       4            
[5] {Butter,Cheese}            5            
[6] {Cheese,Wine}              6            
[7] {Butter,Wine}              7
R로 배우는 Market Basket Analysis

트랜잭션 추가 확인

트랜잭션 개요

inspect(head(data_trx))
    items                      transactionID
[1] {Bread,Butter,Cheese,Wine} 1            
[2] {Bread,Butter,Wine}        2            
[3] {Bread,Butter}             3            
[4] {Butter,Cheese,Wine}       4            
[5] {Butter,Cheese}            5            
[6] {Cheese,Wine}              6

특정 트랜잭션 접근

inspect(data_trx[1])
inspect(data_trx[1:3])

트랜잭션 객체 요약

summary(data_trx)
R로 배우는 Market Basket Analysis

트랜잭션 개요

ItemMatrix 시각화

image(data_trx)

주의: 소수의 트랜잭션에만 사용하세요

유용함:

  • 트랜잭션 내 패턴
  • 데이터 희소성

밀도 = 18/28 = 0.64

trx_image

R로 배우는 Market Basket Analysis

트랜잭션을 살펴봅시다!

R로 배우는 Market Basket Analysis

Preparing Video For Download...