Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TokyoR#22 アソシエーション分析

bob3bob3
January 12, 2022

TokyoR#22 アソシエーション分析

R言語によるアソシエーション分析(associations analysis、Association rule learning、連関規則、アソシエーション・ルール、併売分析、マーケットバスケット分析、market basket analysis )。
2012年4月。

bob3bob3

January 12, 2022
Tweet

More Decks by bob3bob3

Other Decks in Programming

Transcript

  1. アソシエーション分析とは? • Association • 1 (共通の目的で組織された)団体, 会, 会社;組合;((しばしばA-))(…)協会 the student

    body association 学生自治会. • 2 [U](…との)交際, つき合い, かかわり, つながり, 共同, 提携((with ...)) in association with ... …に関連して;…と共同で He denied any association with the plane maker. 彼はその航空機メーカーとは何のかかわりもないと言った. • 3 [U]連想;[C]連想されるもの[意味] the association of ideas 《心理学》観念連合, 連想 my associations from the poem その詩から私が思い浮かべること. • 出典:「プログレッシブ英和中辞典」
  2. アソシエーション分析とは? • 活用例 • 店舗のレイアウトや棚割り設計のための資料として活用。 • より併売されやすい商品を近くに配置。 • 顧客の利便性向上とクロスセリングによる売り上げ向上を目指す。 •

    実用上 • 商品管理単位そのままのデータをアソシエーション分析にかけても、ア イテム数が多すぎて有用な結果は得られにくいので工夫が必要。 • カテゴリごとにグールプ化する。 • ABC分析に基づいて、主力商品を中心に分析する。
  3. アソシエーションルール • X⇒Y • 「XならばY」 • X:条件部、ルールヘッド、前提。 • Y:結論部、ルールボディ。 •

    シャンプー⇒リンス。 • ビール⇒枝豆。 • 月末⇒残業 • 金曜日の夜&山手線⇒酔っ払い • 実用上 • 商品管理単位そのままのデータをアソシエーション分析にかけても、ア イテム数が多すぎて有用な結果は得られにくいので工夫が必要。 • カテゴリごとにグールプ化する。 • ABC分析に基づいて、主力商品を中心に分析する。
  4. Rでアソシエーション分析 summary(Groceries) # transactions as itemMatrix in sparse format with

    # 9835 rows (elements/itemsets/transactions) and # 169 columns (items) and a density of 0.02609146 # most frequent items: # whole milk other vegetables rolls/buns soda # 2513 1903 1809 1715 # yogurt (Other) # 1372 34055 # element (itemset/transaction) length distribution: # sizes # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 2159 1643 1299 1005 855 645 545 438 350 246 182 117 78 77 55 # 16 17 18 19 20 21 22 23 24 26 27 28 29 32 # 46 29 14 14 9 11 4 6 1 1 1 1 3 1 # Min. 1st Qu. Median Mean 3rd Qu. Max. # 1.000 2.000 3.000 4.409 6.000 32.000 # includes extended item information - examples: # labels level2 level1 # 1 frankfurter sausage meet and sausage # 2 sausage sausage meet and sausage # 3 liver loaf sausage meet and sausage
  5. Rでアソシエーション分析 inspect(Groceries) # 8283 {frankfurter, # onions, # liquor (appetizer),

    # napkins} # 8284 {butter} # 8285 {organic sausage, # tropical fruit, # packaged fruit/vegetables, # whole milk, # curd, # yogurt, # soft cheese, # curd cheese, # frozen vegetables, # domestic eggs, # rolls/buns, # pastry, # margarine, # bottled water, # cooking chocolate, # hygiene articles, # shopping bags}
  6. Rでアソシエーション分析 # 元データのアイテムごとの件数を確認 head(sort(itemFrequency(Groceries, type="absolute"), d=TRUE)) # whole milk other

    vegetables rolls/buns soda yogurt bottled water # 2513 1903 1809 1715 1372 1087 # アイテムごとの出現頻度の確認 itemFrequencyPlot(Groceries)
  7. Rでアソシエーション分析 # aprioriアルゴリズムの実行 #デフォルトは信頼度0.8以上、支持度0.1以上のルールのみ抽出 grule1 <- apriori(Groceries) # parameter specification:

    # confidence minval smax arem aval originalSupport support minlen maxlen target ext # 0.8 0.1 1 none FALSE TRUE 0.1 1 10 rules FALSE # algorithmic control: # filter tree heap memopt load sort verbose # 0.1 TRUE TRUE FALSE TRUE 2 TRUE # apriori - find association rules with the apriori algorithm # version 4.21 (2004.05.09) (c) 1996-2004 Christian Borgelt # set item appearances ...[0 item(s)] done [0.00s]. # set transactions ...[169 item(s), 9835 transaction(s)] done [0.01s]. # sorting and recoding items ... [8 item(s)] done [0.00s]. # creating transaction tree ... done [0.00s]. # checking subsets of size 1 2 done [0.00s]. # writing ... [0 rule(s)] done [0.00s]. # creating S4 object ... done [0.00s]. ## writing...の行で何個のルールが抽出されたか確認できる。
  8. Rでアソシエーション分析 #パラメータの指定。extは事前確率の表示。 # 確信度0.5、支持度0.01 grule2 <- apriori(Groceries, p=list(support=0.01, confidence=0.5, maxlen=4,

    ext=TRUE)) # parameter specification: # confidence minval smax arem aval originalSupport support minlen maxlen target ext # 0.5 0.1 1 none FALSE TRUE 0.01 1 4 rules TRUE # algorithmic control: # filter tree heap memopt load sort verbose # 0.1 TRUE TRUE FALSE TRUE 2 TRUE # apriori - find association rules with the apriori algorithm # version 4.21 (2004.05.09) (c) 1996-2004 Christian Borgelt # set item appearances ...[0 item(s)] done [0.00s]. # set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s]. # sorting and recoding items ... [88 item(s)] done [0.00s]. # creating transaction tree ... done [0.01s]. # checking subsets of size 1 2 3 4 done [0.00s]. # writing ... [15 rule(s)] done [0.00s]. # creating S4 object ... done [0.00s]. ## ルールが15個出た!
  9. Rでアソシエーション分析 # 得られたルールの表示 inspect(grule2) # lhs rhs support confidence lhs.support

    lift # 1 {curd, # yogurt} => {whole milk} 0.01006609 0.5823529 0.01728521 2.279125 # 2 {other vegetables, # butter} => {whole milk} 0.01148958 0.5736041 0.02003050 2.244885 # 3 {other vegetables, # domestic eggs} => {whole milk} 0.01230300 0.5525114 0.02226741 2.162336 # 4 {yogurt, # whipped/sour cream} => {whole milk} 0.01087951 0.5245098 0.02074225 2.052747 # 5 {other vegetables, # whipped/sour cream} => {whole milk} 0.01464159 0.5070423 0.02887646 1.984385 # 6 {pip fruit, # other vegetables} => {whole milk} 0.01352313 0.5175097 0.02613116 2.025351 # 7 {citrus fruit, # root vegetables} => {other vegetables} 0.01037112 0.5862069 0.01769192 3.029608 # 8 {tropical fruit, # root vegetables} => {other vegetables} 0.01230300 0.5845411 0.02104728 3.020999 # 9 {tropical fruit, # root vegetables} => {whole milk} 0.01199797 0.5700483 0.02104728 2.230969 # 10 {tropical fruit, # yogurt} => {whole milk} 0.01514997 0.5173611 0.02928317 2.024770 # ……
  10. Rでアソシエーション分析 # 任意の評価指標で並び替えてルールを表示。ここではリフト値順。 inspect(sort(grule2, by="lift")) # lhs rhs support confidence

    lhs.support lift # 1 {citrus fruit, # root vegetables} => {other vegetables} 0.01037112 0.5862069 0.01769192 3.029608 # 2 {tropical fruit, # root vegetables} => {other vegetables} 0.01230300 0.5845411 0.02104728 3.020999 # 3 {root vegetables, # rolls/buns} => {other vegetables} 0.01220132 0.5020921 0.02430097 2.594890 # 4 {root vegetables, # yogurt} => {other vegetables} 0.01291307 0.5000000 0.02582613 2.584078 # 5 {curd, # yogurt} => {whole milk} 0.01006609 0.5823529 0.01728521 2.279125 # 6 {other vegetables, # butter} => {whole milk} 0.01148958 0.5736041 0.02003050 2.244885 # 7 {tropical fruit, # root vegetables} => {whole milk} 0.01199797 0.5700483 0.02104728 2.230969 # 8 {root vegetables, # yogurt} => {whole milk} 0.01453991 0.5629921 0.02582613 2.203354 # 9 {other vegetables, # domestic eggs} => {whole milk} 0.01230300 0.5525114 0.02226741 2.162336 # 10 {yogurt, # whipped/sour cream} => {whole milk} 0.01087951 0.5245098 0.02074225 2.052747 # ……
  11. Rでアソシエーション分析 # 任意の評価指標で並び替えてルールを表示。ここではリフト値順。 inspect(sort(grule2, by="lift")) # lhs rhs support confidence

    lhs.support lift # 1 {citrus fruit, # root vegetables} => {other vegetables} 0.01037112 0.5862069 0.01769192 3.029608 # 2 {tropical fruit, # root vegetables} => {other vegetables} 0.01230300 0.5845411 0.02104728 3.020999 # 3 {root vegetables, # rolls/buns} => {other vegetables} 0.01220132 0.5020921 0.02430097 2.594890 # 4 {root vegetables, # yogurt} => {other vegetables} 0.01291307 0.5000000 0.02582613 2.584078 # 5 {curd, # yogurt} => {whole milk} 0.01006609 0.5823529 0.01728521 2.279125 # 6 {other vegetables, # butter} => {whole milk} 0.01148958 0.5736041 0.02003050 2.244885 # 7 {tropical fruit, # root vegetables} => {whole milk} 0.01199797 0.5700483 0.02104728 2.230969 # 8 {root vegetables, # yogurt} => {whole milk} 0.01453991 0.5629921 0.02582613 2.203354 # 9 {other vegetables, # domestic eggs} => {whole milk} 0.01230300 0.5525114 0.02226741 2.162336 # 10 {yogurt, # whipped/sour cream} => {whole milk} 0.01087951 0.5245098 0.02074225 2.052747 # ……
  12. Rでアソシエーション分析 # 前提や結論を指定してルールを抽出したい。前提にwhole milkを含むルールのみ抽出。 head(inspect(sort(grule3, by="lift"))) # lhs rhs support

    confidence lift # 1 {whole milk} => {butter} 0.02755465 0.1078392 1.9460530 # 2 {whole milk} => {curd} 0.02613116 0.1022682 1.9194805 # 3 {whole milk} => {domestic eggs} 0.02999492 0.1173896 1.8502027 # 4 {whole milk} => {whipped/sour cream} 0.03223183 0.1261441 1.7597542 # 5 {whole milk} => {root vegetables} 0.04890696 0.1914047 1.7560310 # 6 {whole milk} => {tropical fruit} 0.04229792 0.1655392 1.5775950 # 7 {whole milk} => {yogurt} 0.05602440 0.2192598 1.5717351 # 8 {whole milk} => {pip fruit} 0.03009659 0.1177875 1.5570432 # 9 {whole milk} => {other vegetables} 0.07483477 0.2928770 1.5136341 # 10 {whole milk} => {pastry} 0.03324860 0.1301234 1.4625865 # 11 {whole milk} => {citrus fruit} 0.03050330 0.1193792 1.4423768 # 12 {whole milk} => {fruit/vegetable juice} 0.02663955 0.1042579 1.4421604 # 13 {whole milk} => {newspapers} 0.02735130 0.1070434 1.3411103 # 14 {whole milk} => {sausage} 0.02989324 0.1169916 1.2452520 # 15 {whole milk} => {bottled water} 0.03436706 0.1345006 1.2169396 # 16 {whole milk} => {rolls/buns} 0.05663447 0.2216474 1.2050318 # ……
  13. Rでアソシエーション分析 # そのうち、liftが1.5以上のルールのみ grule4 <- subset(grule3, subset=(lift>=1.5)) inspect(grule4) # lhs

    rhs support confidence lift # 1 {whole milk} => {curd} 0.02613116 0.1022682 1.919481 # 2 {whole milk} => {butter} 0.02755465 0.1078392 1.946053 # 3 {whole milk} => {domestic eggs} 0.02999492 0.1173896 1.850203 # 4 {whole milk} => {whipped/sour cream} 0.03223183 0.1261441 1.759754 # 5 {whole milk} => {pip fruit} 0.03009659 0.1177875 1.557043 # 6 {whole milk} => {tropical fruit} 0.04229792 0.1655392 1.577595 # 7 {whole milk} => {root vegetables} 0.04890696 0.1914047 1.756031 # 8 {whole milk} => {yogurt} 0.05602440 0.2192598 1.571735 # 9 {whole milk} => {other vegetables} 0.07483477 0.2928770 1.513634