SlideShare a Scribd company logo
關聯聯規則分析
Association Rules Analysis
on Spark
MLlib - Frequent Pattern Mining
Gil

2018-08-31
outline
• Introduction to Association Rules

• Association Rules on Distributed Computing (Spark)

• 案例例:旅遊關聯聯分析

• 案例例:競價關聯聯分析
Introduction to
Association Rules
莫再提了了!啤酒尿尿布是都市傳說 Chih-Cheng Liang, Nov, 2016
example
A B C D E
ㄎㄎ V V V
遙遙 V V V V
叮叮 V V
A  B
Support  P(A B):A 與 B 共同出現的機率,數值越⼤大越好。

Confidence  P(B|A):在 A 出現的前提下,出現 B 的機率,數值越⼤大越好。

Lift P(B|A)/P(B):B 單獨出現比率與前項 Confidence 的比較,
當數值⼤大於 1 表⽰示規則有效,數值越⼤大效果越好。
∩
user item
ㄎㄎ A
ㄎㄎ B
ㄎㄎ E
遙遙 A
叮叮 B
[ A, B, E ]

[ A, B, C, E ]

[ A, B ]
transactions
+---------+----+
| items|freq|
+---------+----+
| [A]| 3|
| [B]| 3|
| [B, A]| 3|
| [E]| 2|
| [E, B]| 2|
|[E, B, A]| 2|
| [E, A]| 2|
+---------+----+
+----------+----------+------------+
|antecedent|consequent| confidence|
+----------+----------+------------+
| [E]| [B]| 1.0|
| [E]| [A]| 1.0|
| [B, A]| [E]|0.6666666666|
| [E, B]| [A]| 1.0|
| [A]| [B]| 1.0|
| [A]| [E]|0.6666666666|
| [E, A]| [B]| 1.0|
| [B]| [A]| 1.0|
| [B]| [E]|0.6666666666|
+----------+----------+------------+
Apriori Algorithm
• 所有項⽬目集合中,若若某項⽬目集合的頻次很低,則該⺟母集合也很低。

• e.q. 消費記錄中,購買「林林X營鮮乳」的 transaction 很少,則
item sets 組合中出現前品項者,都會先被排除。
[ A, B, E ]

[ A, B, C, E ]

[ A, B ]
transactions
n = 4

item sets = 24 - 1 = 15

4: {A}, {B}, {C}, {E}

6: {A,B}, {A,C}, {A,E}, {B,C}, {B,E}, {C,E}

4: {A,B,C}, {A,B,E}, {A,C,E}, {B,C,E}

1: {A,B,C,E}
item sets
If support > 0.5

( filter out C )

{A} 3

{B} 3

{A,B} 3

{E} 2

{A,E} 2

{B,E} 2

{A,B,E} 2
frequent item sets
Agrawal, Rakesh; and Srikant, Ramakrishnan; Fast algorithms for mining association rules in large databases, in Bocca,
Jorge B.; Jarke, Matthias; and Zaniolo, Carlo; editors, Proceedings of the 20th International Conference on Very Large
Data Bases (VLDB), Santiago, Chile, September 1994, pages 487-499
FP-Growth Algorithm
• 原理理跟 Apriori ⼀一樣,但利利⽤用 FP-tree 加速搜尋。
[ A, B, E ]

[ A, B, C, E ]

[ A, B ]
transactions FP-tree
ϕ
A
B
E C
E
1
1
1 1
1
2
2
3
3
prefix path
E {A,B}, {A,B,C}
B {A}
A { }
ϕ
conditional tree
on E, ϕ
A
B
{E} 2

{A,E} 2

{B,E} 2

{A,B,E} 2
header table
A 3
B 3
C 1
E 2
on B, {B} 3

{A,B} 3
ϕ
A
{A} 3
on A,
Han (2000). "Mining Frequent Patterns Without Candidate Generation". Proceedings of the 2000 ACM SIGMOD
International Conference on Management of Data. SIGMOD '00: 1–12.
packages
• Python

• Apriori

• FP-growth

• R

• Apriori

https://cran.r-project.org/web/packages/arules/arules.pdf

• FP-growth

https://cran.r-project.org/web/packages/rCBA/rCBA.pdf
from mlxtend.frequent_patterns import apriori
from orangecontrib.associate import fpgrowth
Association Rules on
Distributed Computing
(on Spark)
DataFrame-based API

Since Spark 2.2.0 (July 2017)
https://spark.apache.org/docs/2.2.0/ml-guide.html
Spark 2.1.3 Spark 2.2.0
https://spark.apache.org/docs/2.2.0/ml-frequent-pattern-mining.html
https://spark.apache.org/docs/2.2.0/ml-frequent-pattern-mining.html
https://spark.apache.org/news/index.html
Frequent Pattern
Mining
DataFrames

/ML
(2018-08-31) https://en.wikipedia.org/wiki/Apache_Spark
PySpark
SparkR
Data
Science
spark.ml
DataFrames RDD-based API
Primary API
DateFrame-based

API
Frequent Pattern
Mining
DataFrames

/ML
PySpark
SparkR
Data
Science
spark.ml
DataFrames RDD-based API
Primary API
DateFrame-based

API
(2018-08-31) https://en.wikipedia.org/wiki/Apache_Spark
spark milestones
data process
s3 / HDFS
Spark / Hive
Algorithm

(pure python)
output
PySpark
+
Spark MLlib
output
Scale problems:

1. toPandas( )

2. Limitations of algorithm
GOAL !!!
Association Rules Analysis on Spark (20180831 RD sharing)
Demo code
旅遊關聯聯分析
- 以 Osaka Report 為例例
Trata DMP Recap
• Location Data from SDK

• Geography Information System (GIS) from Google API

• Home Country Tags & Cross-Border Travelers -> passport holders

• Reports

• Asia Report since 2016

• Collaboration with GFK - Great China Travelers in Japan

• 台北市觀傳局、⾼高雄市觀傳局

• O2O in ITF

• Osaka Report for Workshop@Osaka

• … etc.
Association Rules Analysis on Spark (20180831 RD sharing)
Association Rules Analysis on Spark (20180831 RD sharing)
⼤大阪市
京都市
有天...來來⾃自 Marketing 的需求...
Gil~ 有辦法從知道...旅客到

⼤大阪後的旅遊路路徑或⾏行行程嗎?
[路路徑的定義]

裝置有發 Request 才有位置資訊,
有些軌跡很連續、有些很離散,難

以客觀定義路路徑。
[呈現與統計]
很難⽤用視覺化⽅方式同時呈現多⼈人的

移動軌跡。也不易易針對路路徑做任何

的 summary 或統計。
我覺得不⾏行行。
Shiny Spark, Gil 2017/11/24
關⻄西⾏行行程
( )
( )
association rules results
除了了京(京都) 阪(⼤大阪) 神(神⼾戶) ...
奈良
和歌⼭山
+ %
⾏行行程 (購物籃) 中包含:
1. ⼤大阪+京都 ➡ 23.8%
2. ⼤大阪+神⼾戶 ➡ 11.9%
5. ⼤大阪+京都+神⼾戶➡ 5.5%
⼤大阪
京都
兵庫(神⼾戶)
競價關聯聯分析
- 以 Fantasy Basketball 遊戲為例例
Fantasy Basketball
• 線上遊戲:虛擬總經理理 / 總教練

• 非動作類,純數字競賽、管理理遊戲

• ⼀一年年⼀一次,為期約六個⽉月,跟真實 NBA 世界同步

• 季前選秀、季中對戰、季末調整

• 了了解更更多:

• https://www.slideshare.net/wqchen/nba-moneyball-in-web-
application-using-r-20160307-mldm-59782958

• http://www.fansboard.com/
Gil 2016/3/7
球隊陣容(⾃自⼰己組球隊)
對戰實況
季中競標 -12 個玩家(隊)、每隊 13 位球員為例例
• 12 x 13 = 156 位球員,在開季前選秀被選
走,其餘球員則為⾃自由球員 (Free Agent,
FA),FA 可在球季中任何時間被玩家替換。

• 每撿⼀一個 FA,就要丟出隊中⼀一名球員,以
維持各隊 13 ⼈人編制。

• 被丟棄的球員,在三天內,則處於讓渡
(waiver) 狀狀態,⽽而非 FA。三天過後,沒有任
何玩家競標,則成為 FA。

• 被丟棄的球員在 waiver 狀狀態下,如有其他
玩家想要搶先收下該球員,則在三天內,需
要投標,且為⼀一次性暗標,與其他玩家競
標。
全 NBA 每⼀一季⼤大約有 600 位球員
FA
156 picked
players
example
有⼀一天阿岳覺得 J.R. Smith 不⾏行行,於是把 J.R. 丟棄 ...
… 於是,亦凡可以收下 J.R.,

熱狗則飲恨差⼀一點拿到,

瑋柏則拍拍屁股沒事。
$ 18
$ 20 $ 5
這時,有三位玩家對 J.R. 有興趣,於是在 waiver 

期間三天內,各⾃自投下預期的⾦金金額,來來競標 J.R.。
$ 20 $ 18 $ 5
Association Rules Analysis on Spark (20180831 RD sharing)
Data -
競價歷史紀錄
Association Rules Analysis on Spark (20180831 RD sharing)
association rules results
take away
• DataFrames 版本的分散式運算的實現

• 關聯聯規則分析,⼜又叫購物籃分析,但不是只有購物相關的
數據可以玩,換個⾓角度,都可以找關聯聯規則。
Thank you for your listening ~

More Related Content

PPT
Maximum likelihood estimation
PPTX
行動廣告與大數據資料分析策略與執行
PDF
2021/08 - 暑期新尖兵App - 全端技術開發分享(AWS)
PDF
AWS re:Invent 2018 Recap
PDF
非監督是學習_Kmeans_process_visualization20241110.pdf
 
PDF
2021/12 - 天氣即時預報App-創業與開發歷程(AWS)
PPT
Google的搜尋方式與網頁設計注意事項
PDF
2_學院碩士班_資料視覺化_20220411_0509.pdf
 
Maximum likelihood estimation
行動廣告與大數據資料分析策略與執行
2021/08 - 暑期新尖兵App - 全端技術開發分享(AWS)
AWS re:Invent 2018 Recap
非監督是學習_Kmeans_process_visualization20241110.pdf
 
2021/12 - 天氣即時預報App-創業與開發歷程(AWS)
Google的搜尋方式與網頁設計注意事項
2_學院碩士班_資料視覺化_20220411_0509.pdf
 
Ad

Association Rules Analysis on Spark (20180831 RD sharing)