광고 매크로 없는 청정한 블로그를 위해 노력중입니다. 근데 나만 노력하는 것 같음… ㅡㅡ
반응형

이걸 왜 알아야 하냐면 결과 주고 옳은 것은? 옳지 않은 것은? 해석하라고 나옵니다. 연관, 회귀(주로 다항), 또 뭐 있었는데 아무튼... 그래서 분석도 해볼 수 있으면 직접 찍먹정도는 해보는게 좋다는거임. 


연관분석이 뭔데요?

내가 겜덕이니까 겜 위주로 비유를 좀 해보겠음. 요즘 스위치 2 나왔잖아요? 연관분석에는 크게 장바구니 분석, 서열분석 두 개가 있는데, 장바구니 분석은 스위치 2를 사는 고객들이 뭘 같이 사는가(액정필름, 파우치, 냥발(스틱 커버) 이런거)이고 서열분석은 스위치 2를 구매한 고객들이 이후에 뭘 사는가를 분석하는거다.

 

게임기로는 잘 안 와닿는 분들을 위해 한가지 더 예를 들어보자면, 내가 지금쓰고 있는 폰이 아이폰 15 프맥인데 얘를 살 때 액정필름이랑 케이스를 같이 샀었다. (아이폰은 외장메모리 없음) 아이폰을 구매하는 고객들이 '핸드폰을 사면서 뭘 같이 사는가'는 장바구니 분석이고, 본인의 경우에는 액정필름이랑 케이스를 산 것이다. 근데 핸드폰이 갓 나왔을때는 살 수 있는 케이스 디자인이 되게 한정적이고, 내꺼 블랙 색감이 아주 쥑여줘서 투명으로 샀거든요? 근데 핸드폰이 나오고 좀 지나면 아주 이쁜 케이스들이 나와요. 그러면 예쁜 케이스로 바꾸게 되는 건 내가 핸드폰을 산 후잖아요? 아니면 액정필름을 갈거나, 카메라 보호 유리를 사거나 할 수도 있다. 핸드폰을 산 '후에' 뭘 사는 건 서열분석이다.

 

지지도, 신뢰도, 향상도

이거 계산하라고 나오니까 공식 외우시면 되는데, 분모는 어차피 A 교집합 B라 쉽다. 분자가 문제지.

 

귀찮아서 노션에 정리한거 걍 쌩으로 캡쳐했음.

 

1) 지지도: 전체 모바일 기기 구매 고객들 중에 갤럭시와 갤럭시 버즈를 동시에 포함하는(그니까 갤럭시랑 버즈 둘다 산) 고객의 비율

2) 신뢰도: 갤럭시 기기를 산 고객들 중에서 갤럭시 기기와 갤럭시 버즈를 같이 구매한 고객의 비율(버즈를 안 사는 사람도 있다. 나도 아이폰 쓰지만 에어팟은 귓구녕 막아서 안씀)

3) 향상도: 갤럭시 기기를 보유하지 않은 사람들이 갤럭시 버즈를 살 확률에 비해 갤럭시 기기를 보유한 사람이 갤럭시 버즈를 살 확률 증가 비율


결과가 어떻게 나오나요?

> rules=apriori(Groceries, parameter=list(support=0.01, confidence=0.3))
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport maxtime support minlen
        0.3    0.1    1 none FALSE            TRUE       5    0.01      1
 maxlen target  ext
     10  rules TRUE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 98

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.00s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [125 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s].

걍 분석하쇼 하면 이렇게 나온다.

 

> inspect(rules)
      lhs                           rhs                   support confidence   coverage     lift count
[1]   {hard cheese}              => {whole milk}       0.01006609  0.4107884 0.02450432 1.607682    99
[2]   {butter milk}              => {other vegetables} 0.01037112  0.3709091 0.02796136 1.916916   102
[3]   {butter milk}              => {whole milk}       0.01159126  0.4145455 0.02796136 1.622385   114
[4]   {ham}                      => {whole milk}       0.01148958  0.4414062 0.02602949 1.727509   113
[5]   {sliced cheese}            => {whole milk}       0.01077783  0.4398340 0.02450432 1.721356   106
[6]   {oil}                      => {whole milk}       0.01128622  0.4021739 0.02806304 1.573968   111
[7]   {onions}                   => {other vegetables} 0.01423488  0.4590164 0.03101169 2.372268   140
[8]   {onions}                   => {whole milk}       0.01209964  0.3901639 0.03101169 1.526965   119
[9]   {berries}                  => {yogurt}           0.01057448  0.3180428 0.03324860 2.279848   104
[10]  {berries}                  => {other vegetables} 0.01026945  0.3088685 0.03324860 1.596280   101
[11]  {berries}                  => {whole milk}       0.01179461  0.3547401 0.03324860 1.388328   116
[12]  {hamburger meat}           => {other vegetables} 0.01382816  0.4159021 0.03324860 2.149447   136
[13]  {hamburger meat}           => {whole milk}       0.01474326  0.4434251 0.03324860 1.735410   145
[14]  {hygiene articles}         => {whole milk}       0.01281139  0.3888889 0.03294357 1.521975   126
[15]  {sugar}                    => {other vegetables} 0.01077783  0.3183183 0.03385867 1.645119   106
[16]  {sugar}                    => {whole milk}       0.01504830  0.4444444 0.03385867 1.739400   148
[17]  {waffles}                  => {whole milk}       0.01270971  0.3306878 0.03843416 1.294196   125
[18]  {long life bakery product} => {whole milk}       0.01352313  0.3614130 0.03741739 1.414444   133
[19]  {dessert}                  => {other vegetables} 0.01159126  0.3123288 0.03711235 1.614164   114
[20]  {dessert}                  => {whole milk}       0.01372649  0.3698630 0.03711235 1.447514   135
[21]  {cream cheese }            => {yogurt}           0.01240468  0.3128205 0.03965430 2.242412   122
[22]  {cream cheese }            => {other vegetables} 0.01372649  0.3461538 0.03965430 1.788977   135
[23]  {cream cheese }            => {whole milk}       0.01647178  0.4153846 0.03965430 1.625670   162
[24]  {chicken}                  => {other vegetables} 0.01789527  0.4170616 0.04290798 2.155439   176
[25]  {chicken}                  => {whole milk}       0.01759024  0.4099526 0.04290798 1.604411   173
[26]  {white bread}              => {other vegetables} 0.01372649  0.3260870 0.04209456 1.685268   135
[27]  {white bread}              => {whole milk}       0.01708185  0.4057971 0.04209456 1.588147   168
[28]  {chocolate}                => {whole milk}       0.01667514  0.3360656 0.04961871 1.315243   164
[29]  {coffee}                   => {whole milk}       0.01870869  0.3222417 0.05805796 1.261141   184
[30]  {frozen vegetables}        => {other vegetables} 0.01779359  0.3699789 0.04809354 1.912108   175
[31]  {frozen vegetables}        => {whole milk}       0.02043721  0.4249471 0.04809354 1.663094   201
[32]  {beef}                     => {root vegetables}  0.01738688  0.3313953 0.05246568 3.040367   171
[33]  {beef}                     => {other vegetables} 0.01972547  0.3759690 0.05246568 1.943066   194
[34]  {beef}                     => {whole milk}       0.02125064  0.4050388 0.05246568 1.585180   209
[35]  {curd}                     => {yogurt}           0.01728521  0.3244275 0.05327911 2.325615   170
[36]  {curd}                     => {other vegetables} 0.01718353  0.3225191 0.05327911 1.666829   169
[37]  {curd}                     => {whole milk}       0.02613116  0.4904580 0.05327911 1.919481   257
[38]  {napkins}                  => {whole milk}       0.01972547  0.3766990 0.05236401 1.474268   194
[39]  {pork}                     => {other vegetables} 0.02165735  0.3756614 0.05765125 1.941476   213
[40]  {pork}                     => {whole milk}       0.02216573  0.3844797 0.05765125 1.504719   218
[41]  {frankfurter}              => {rolls/buns}       0.01921708  0.3258621 0.05897306 1.771616   189
[42]  {frankfurter}              => {whole milk}       0.02053889  0.3482759 0.05897306 1.363029   202
[43]  {brown bread}              => {whole milk}       0.02521607  0.3887147 0.06487036 1.521293   248
[44]  {margarine}                => {other vegetables} 0.01972547  0.3368056 0.05856634 1.740663   194
[45]  {margarine}                => {whole milk}       0.02419929  0.4131944 0.05856634 1.617098   238
[46]  {butter}                   => {other vegetables} 0.02003050  0.3614679 0.05541434 1.868122   197
[47]  {butter}                   => {whole milk}       0.02755465  0.4972477 0.05541434 1.946053   271
[48]  {newspapers}               => {whole milk}       0.02735130  0.3426752 0.07981698 1.341110   269
[49]  {domestic eggs}            => {other vegetables} 0.02226741  0.3509615 0.06344687 1.813824   219
[50]  {domestic eggs}            => {whole milk}       0.02999492  0.4727564 0.06344687 1.850203   295
[51]  {fruit/vegetable juice}    => {whole milk}       0.02663955  0.3684951 0.07229283 1.442160   262
[52]  {whipped/sour cream}       => {other vegetables} 0.02887646  0.4028369 0.07168277 2.081924   284
[53]  {whipped/sour cream}       => {whole milk}       0.03223183  0.4496454 0.07168277 1.759754   317
[54]  {pip fruit}                => {other vegetables} 0.02613116  0.3454301 0.07564820 1.785237   257
[55]  {pip fruit}                => {whole milk}       0.03009659  0.3978495 0.07564820 1.557043   296
[56]  {pastry}                   => {whole milk}       0.03324860  0.3737143 0.08896797 1.462587   327
[57]  {citrus fruit}             => {other vegetables} 0.02887646  0.3488943 0.08276563 1.803140   284
[58]  {citrus fruit}             => {whole milk}       0.03050330  0.3685504 0.08276563 1.442377   300
[59]  {sausage}                  => {rolls/buns}       0.03060498  0.3257576 0.09395018 1.771048   301
[60]  {sausage}                  => {whole milk}       0.02989324  0.3181818 0.09395018 1.245252   294
[61]  {bottled water}            => {whole milk}       0.03436706  0.3109476 0.11052364 1.216940   338
[62]  {tropical fruit}           => {other vegetables} 0.03589222  0.3420543 0.10493137 1.767790   353
[63]  {tropical fruit}           => {whole milk}       0.04229792  0.4031008 0.10493137 1.577595   416
[64]  {root vegetables}          => {other vegetables} 0.04738180  0.4347015 0.10899847 2.246605   466
[65]  {root vegetables}          => {whole milk}       0.04890696  0.4486940 0.10899847 1.756031   481
[66]  {yogurt}                   => {other vegetables} 0.04341637  0.3112245 0.13950178 1.608457   427
[67]  {yogurt}                   => {whole milk}       0.05602440  0.4016035 0.13950178 1.571735   551
[68]  {rolls/buns}               => {whole milk}       0.05663447  0.3079049 0.18393493 1.205032   557
[69]  {other vegetables}         => {whole milk}       0.07483477  0.3867578 0.19349263 1.513634   736
[70]  {curd,
       yogurt}                   => {whole milk}       0.01006609  0.5823529 0.01728521 2.279125    99
[71]  {whole milk,
       curd}                     => {yogurt}           0.01006609  0.3852140 0.02613116 2.761356    99
[72]  {pork,
       other vegetables}         => {whole milk}       0.01016777  0.4694836 0.02165735 1.837394   100
[73]  {pork,
       whole milk}               => {other vegetables} 0.01016777  0.4587156 0.02216573 2.370714   100
[74]  {other vegetables,
       butter}                   => {whole milk}       0.01148958  0.5736041 0.02003050 2.244885   113
[75]  {whole milk,
       butter}                   => {other vegetables} 0.01148958  0.4169742 0.02755465 2.154987   113
[76]  {other vegetables,
       domestic eggs}            => {whole milk}       0.01230300  0.5525114 0.02226741 2.162336   121
[77]  {whole milk,
       domestic eggs}            => {other vegetables} 0.01230300  0.4101695 0.02999492 2.119820   121
[78]  {other vegetables,
       fruit/vegetable juice}    => {whole milk}       0.01047280  0.4975845 0.02104728 1.947371   103
[79]  {whole milk,
       fruit/vegetable juice}    => {other vegetables} 0.01047280  0.3931298 0.02663955 2.031756   103
[80]  {yogurt,
       whipped/sour cream}       => {other vegetables} 0.01016777  0.4901961 0.02074225 2.533410   100
[81]  {other vegetables,
       whipped/sour cream}       => {yogurt}           0.01016777  0.3521127 0.02887646 2.524073   100
[82]  {yogurt,
       whipped/sour cream}       => {whole milk}       0.01087951  0.5245098 0.02074225 2.052747   107
[83]  {whole milk,
       whipped/sour cream}       => {yogurt}           0.01087951  0.3375394 0.03223183 2.419607   107
[84]  {other vegetables,
       whipped/sour cream}       => {whole milk}       0.01464159  0.5070423 0.02887646 1.984385   144
[85]  {whole milk,
       whipped/sour cream}       => {other vegetables} 0.01464159  0.4542587 0.03223183 2.347679   144
[86]  {pip fruit,
       other vegetables}         => {whole milk}       0.01352313  0.5175097 0.02613116 2.025351   133
[87]  {pip fruit,
       whole milk}               => {other vegetables} 0.01352313  0.4493243 0.03009659 2.322178   133
[88]  {other vegetables,
       pastry}                   => {whole milk}       0.01057448  0.4684685 0.02257245 1.833421   104
[89]  {whole milk,
       pastry}                   => {other vegetables} 0.01057448  0.3180428 0.03324860 1.643695   104
[90]  {citrus fruit,
       root vegetables}          => {other vegetables} 0.01037112  0.5862069 0.01769192 3.029608   102
[91]  {citrus fruit,
       other vegetables}         => {root vegetables}  0.01037112  0.3591549 0.02887646 3.295045   102
[92]  {citrus fruit,
       yogurt}                   => {whole milk}       0.01026945  0.4741784 0.02165735 1.855768   101
[93]  {citrus fruit,
       whole milk}               => {yogurt}           0.01026945  0.3366667 0.03050330 2.413350   101
[94]  {citrus fruit,
       other vegetables}         => {whole milk}       0.01301474  0.4507042 0.02887646 1.763898   128
[95]  {citrus fruit,
       whole milk}               => {other vegetables} 0.01301474  0.4266667 0.03050330 2.205080   128
[96]  {sausage,
       other vegetables}         => {whole milk}       0.01016777  0.3773585 0.02694459 1.476849   100
[97]  {sausage,
       whole milk}               => {other vegetables} 0.01016777  0.3401361 0.02989324 1.757876   100
[98]  {other vegetables,
       bottled water}            => {whole milk}       0.01077783  0.4344262 0.02480935 1.700192   106
[99]  {whole milk,
       bottled water}            => {other vegetables} 0.01077783  0.3136095 0.03436706 1.620783   106
[100] {tropical fruit,
       root vegetables}          => {other vegetables} 0.01230300  0.5845411 0.02104728 3.020999   121
[101] {tropical fruit,
       other vegetables}         => {root vegetables}  0.01230300  0.3427762 0.03589222 3.144780   121
[102] {tropical fruit,
       root vegetables}          => {whole milk}       0.01199797  0.5700483 0.02104728 2.230969   118
[103] {tropical fruit,
       yogurt}                   => {other vegetables} 0.01230300  0.4201389 0.02928317 2.171343   121
[104] {tropical fruit,
       other vegetables}         => {yogurt}           0.01230300  0.3427762 0.03589222 2.457146   121
[105] {tropical fruit,
       yogurt}                   => {whole milk}       0.01514997  0.5173611 0.02928317 2.024770   149
[106] {tropical fruit,
       whole milk}               => {yogurt}           0.01514997  0.3581731 0.04229792 2.567516   149
[107] {tropical fruit,
       rolls/buns}               => {whole milk}       0.01098119  0.4462810 0.02460600 1.746587   108
[108] {tropical fruit,
       other vegetables}         => {whole milk}       0.01708185  0.4759207 0.03589222 1.862587   168
[109] {tropical fruit,
       whole milk}               => {other vegetables} 0.01708185  0.4038462 0.04229792 2.087140   168
[110] {root vegetables,
       yogurt}                   => {other vegetables} 0.01291307  0.5000000 0.02582613 2.584078   127
[111] {root vegetables,
       yogurt}                   => {whole milk}       0.01453991  0.5629921 0.02582613 2.203354   143
[112] {root vegetables,                                                                               
       rolls/buns}               => {other vegetables} 0.01220132  0.5020921 0.02430097 2.594890   120
[113] {root vegetables,
       rolls/buns}               => {whole milk}       0.01270971  0.5230126 0.02430097 2.046888   125
[114] {root vegetables,
       other vegetables}         => {whole milk}       0.02318251  0.4892704 0.04738180 1.914833   228
[115] {root vegetables,
       whole milk}               => {other vegetables} 0.02318251  0.4740125 0.04890696 2.449770   228
[116] {other vegetables,
       whole milk}               => {root vegetables}  0.02318251  0.3097826 0.07483477 2.842082   228
[117] {yogurt,
       soda}                     => {whole milk}       0.01047280  0.3828996 0.02735130 1.498535   103
[118] {other vegetables,
       soda}                     => {whole milk}       0.01392984  0.4254658 0.03274021 1.665124   137
[119] {whole milk,
       soda}                     => {other vegetables} 0.01392984  0.3477157 0.04006101 1.797049   137
[120] {yogurt,
       rolls/buns}               => {other vegetables} 0.01148958  0.3343195 0.03436706 1.727815   113
[121] {yogurt,
       rolls/buns}               => {whole milk}       0.01555669  0.4526627 0.03436706 1.771563   153
[122] {other vegetables,
       yogurt}                   => {whole milk}       0.02226741  0.5128806 0.04341637 2.007235   219
[123] {whole milk,
       yogurt}                   => {other vegetables} 0.02226741  0.3974592 0.05602440 2.054131   219
[124] {other vegetables,
       rolls/buns}               => {whole milk}       0.01789527  0.4200477 0.04260295 1.643919   176
[125] {whole milk,
       rolls/buns}               => {other vegetables} 0.01789527  0.3159785 0.05663447 1.633026   176

inspect 하면 이렇게 나온다.

 

일단 저 분석에서 support랑 confidence에 각각 0.01, 0.3이라고 되어 있을텐데, 사실 이 연관분석이라는 게 브루트 포스랑 다를 게 없음. 브루트 포스가 뭐냐면 통장 비번 맞추려고 0000부터 9999까지 다 쳐보는거다. 물론 하나하나 다 쳐보는거니까 답을 찾을 수는 있겠지만 시간이 증말 드럽게 오래걸리고 리소스도 무지막지하게 잡아먹는 게 특징… 그래서 저건 뭔가요? 일종의 마지노선이다. 왜 회사에 지원할때 보면 지원자격 있죠? 그런겁니다. 최소지지도에서 0.01보다 크면 서류전형에서 합격이고, 최소신뢰도가 0.3보다 크면 실무진 면접까지 합격하는.

 

Absolute minimum support count: 98

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.00s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [125 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s].

이 부분을 다시 보자. 여기서 총 생성된 규칙을 찾으라고 하면 뭘 찾아야 하나요? 바로 writing … [125 rule(s)] done [0.00s]. 이거다. Absolute minimum support count는 생성된 규칙 수가 아니라 전체 트랜잭션과 최소신뢰도를 기반으로 계산한 것으로, 집계되는 거래 건수의 마지노선이다. 아무튼 생성된 규칙 수는 쟤 아니고 다른 애니까 그거 헷갈리지 마십셔.

 

> inspect(rules)
      lhs                           rhs                   support confidence   coverage     lift count
[1]   {hard cheese}              => {whole milk}       0.01006609  0.4107884 0.02450432 1.607682    99
[2]   {butter milk}              => {other vegetables} 0.01037112  0.3709091 0.02796136 1.916916   102
[3]   {butter milk}              => {whole milk}       0.01159126  0.4145455 0.02796136 1.622385   114
[4]   {ham}                      => {whole milk}       0.01148958  0.4414062 0.02602949 1.727509   113
[5]   {sliced cheese}            => {whole milk}       0.01077783  0.4398340 0.02450432 1.721356   106

 

이건 위에 다섯개만 보고 가자. lhs->rhs는 이걸 산 사람이 이것도 샀다, 이런 의미이고 서포트 컨피던스는 지지도 신뢰도, 커버리지 쟈는 모르것고 리프트가 향상도이다. 카운트는 아마도 거래건수? 보통은 결과 이런 식으로 안 나오고

> inspect(sorted_rules_lift)
      lhs                           rhs                   support confidence   coverage     lift count
[1]   {citrus fruit,
       other vegetables}         => {root vegetables}  0.01037112  0.3591549 0.02887646 3.295045   102
[2]   {tropical fruit,
       other vegetables}         => {root vegetables}  0.01230300  0.3427762 0.03589222 3.144780   121
[3]   {beef}                     => {root vegetables}  0.01738688  0.3313953 0.05246568 3.040367   171
[4]   {citrus fruit,
       root vegetables}          => {other vegetables} 0.01037112  0.5862069 0.01769192 3.029608   102
[5]   {tropical fruit,

어떤 기준으로든 정렬해서 이런 식으로 낸다. 얘는 향상도를 기준으로 정렬한것. 여기서는 그냥 아 지지도 신뢰도 향상도 이렇게 나왔구나...하시고... {citrus fruit, other vegetables}  => {root vegetables}의 지지도가 약 0.0104, 신뢰도가 약 0.3592, 향상도가 약 3.2950일 때 해석을 어떻게 하냐면 

 

1) 지지도: 전체 구매 고객 중 시트러스류 과일(귤, 레몬, 유자 이런거)과 다른 야채, 그리고 뿌리채소를 같이 구매한 고객의 비율이 약 0.0104

2) 신뢰도: 시트러스류 과일과 다른 야채까지만 구입한 고객 대비 뿌리채소까지 같이 구매한 고객의 비율이 약 0.3593

3) 향상도: 고객들이 그냥 뿌리채소만 구매할 확률 대비 시트러스류 과일과 채소, 뿌리채소를 같이 구매할 확률 증가 비율이 약 3.2950

 

저 세개 개념만 알고 있으면 어느정도는 풀 수 있을 것이다. 기출에서는 그로서리즈 나왔는데 혹시 모름… 얘네 어느순간 다른 데이터셋으로 낼 수도 있어…

 

반응형