Rで学ぶマーケットバスケット分析
Christopher Bruffaerts
Statistician
トランザクション: 何かを売買する行為。

トランザクションデータ: 1回の購入で、1人の顧客が買ったアイテムの一覧。
トランザクション例:
TID Product
1 1 Bread
2 1 Cheese
3 1 Cheese
4 1 Cheese
Transactions クラス: アイテム集合やルール抽出に用いるトランザクションデータを表します。
変換元:
ただし、事前にデータ準備が必要です。
トランザクションデータで重要な点
製品を識別するフィールド/列
トランザクションを識別するフィールド/列
店舗のトランザクションデータ
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
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
transaction クラスへ変換
# トランザクションデータに変換
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
トランザクションの概要
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])
transaction オブジェクトの要約
summary(data_trx)
ItemMatrix をプロット
image(data_trx)
注意: この関数は少数のトランザクションで使用してください
判別に有用:
密度 = 18/28 = 0.64

Rで学ぶマーケットバスケット分析