Reglas de Asociación con el algoritmo Apriori usando Python

In [14]:
# Cargamos las librerias necesarias
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [15]:
# Importamos el archivo de transacciones
dataset = pd.read_csv('Market_Basket_Optimisation.csv', header = None)

Dado que los registros se ecuentran en columnas con y sin articulos, los acomodamos en un arreglo con columnas variables

In [16]:
transactions = []
for i in range(0, 7501):
    transactions.append([str(dataset.values[i,j]) for j in range(0, 20)])

El siguiente paso es cargar la clase Apriori, crear el objeto y entrenarla con el conjunto de datos de transacciones

In [17]:
# Training Apriori on the dataset
from apyori import apriori
rules = apriori(transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2)

El objeto contiene las reglas de acuerdo a los grupos frecuentes, par poder visualizar los resultados creamos una lista

In [18]:
# Resultados
results = list(rules)

Una vez creada la lista, para poder visualizar los grupos frecuentes, creamos una función que nos ayuda a acomodarlos

In [19]:
def inspect(results):
    rh          = [tuple(result[2][0][0]) for result in results]
    lh          = [tuple(result[2][0][1]) for result in results]
    supports    = [result[1] for result in results]
    confidences = [result[2][0][2] for result in results]
    lifts       = [result[2][0][3] for result in results]
    return list(zip(rh, lh, supports, confidences, lifts))

Invocamos la función creando un frame con las reglas encontradas.

In [20]:
# Este comamdo crea un frame para ver los datos resultados
resultDataFrame=pd.DataFrame(inspect(results),
                columns=['rhs','lhs','support','confidence','lift'])
In [21]:
#Imprimimos el frame con las reglas
resultDataFrame
Out[21]:
rhs lhs support confidence lift
0 (light cream,) (chicken,) 0.004533 0.290598 4.843951
1 (mushroom cream sauce,) (escalope,) 0.005733 0.300699 3.790833
2 (pasta,) (escalope,) 0.005866 0.372881 4.700812
3 (fromage blanc,) (honey,) 0.003333 0.245098 5.164271
4 (herb & pepper,) (ground beef,) 0.015998 0.323450 3.291994
5 (tomato sauce,) (ground beef,) 0.005333 0.377358 3.840659
6 (light cream,) (olive oil,) 0.003200 0.205128 3.114710
7 (whole wheat pasta,) (olive oil,) 0.007999 0.271493 4.122410
8 (pasta,) (shrimp,) 0.005066 0.322034 4.506672
9 (avocado, spaghetti) (milk,) 0.003333 0.416667 3.215449
10 (milk, cake) (burgers,) 0.003733 0.280000 3.211437
11 (turkey, chocolate) (burgers,) 0.003066 0.270588 3.103490
12 (milk, turkey) (burgers,) 0.003200 0.282353 3.238424
13 (frozen vegetables, cake) (tomatoes,) 0.003066 0.298701 4.367560
14 (cereals, ground beef) (spaghetti,) 0.003066 0.676471 3.885303
15 (chicken, ground beef) (milk,) 0.003866 0.408451 3.152046
16 (light cream, nan) (chicken,) 0.004533 0.290598 4.843951
17 (chicken, milk) (olive oil,) 0.003600 0.243243 3.693457
18 (chicken, spaghetti) (olive oil,) 0.003466 0.201550 3.060384
19 (frozen vegetables, chocolate) (shrimp,) 0.005333 0.232558 3.254512
20 (herb & pepper, chocolate) (ground beef,) 0.003999 0.441176 4.490183
21 (soup, chocolate) (milk,) 0.003999 0.394737 3.046215
22 (cooking oil, ground beef) (spaghetti,) 0.004799 0.571429 3.281995
23 (eggs, ground beef) (herb & pepper,) 0.004133 0.206667 4.178455
24 (red wine, eggs) (spaghetti,) 0.003733 0.528302 3.034297
25 (mushroom cream sauce, nan) (escalope,) 0.005733 0.300699 3.790833
26 (pasta, nan) (escalope,) 0.005866 0.372881 4.700812
27 (french fries, ground beef) (herb & pepper,) 0.003200 0.230769 4.665768
28 (nan, fromage blanc) (honey,) 0.003333 0.245098 5.164271
29 (frozen vegetables, green tea) (tomatoes,) 0.003333 0.231481 3.384683
... ... ... ... ... ...
124 (milk, spaghetti, nan) (olive oil,) 0.007199 0.203008 3.082509
125 (tomatoes, milk, nan) (soup,) 0.003066 0.219048 4.335293
126 (spaghetti, whole wheat pasta, nan) (milk,) 0.003999 0.454545 3.507763
127 (mineral water, soup, nan) (olive oil,) 0.005199 0.225434 3.423030
128 (mineral water, whole wheat pasta, nan) (olive oil,) 0.003866 0.402778 6.115863
129 (pancakes, spaghetti, nan) (olive oil,) 0.005066 0.201058 3.052910
130 (olive oil, tomatoes, nan) (spaghetti,) 0.004399 0.611111 3.509912
131 (spaghetti, whole wheat rice, nan) (tomatoes,) 0.003066 0.216981 3.172662
132 (mineral water, eggs, chocolate, nan) (ground beef,) 0.003999 0.297030 3.023093
133 (frozen vegetables, mineral water, chocolate, ... (ground beef,) 0.003333 0.342466 3.485530
134 (frozen vegetables, chocolate, ground beef, nan) (spaghetti,) 0.003066 0.534884 3.072100
135 (frozen vegetables, mineral water, chocolate, ... (milk,) 0.003999 0.410959 3.171402
136 (frozen vegetables, spaghetti, chocolate, nan) (milk,) 0.003466 0.440678 3.400746
137 (frozen vegetables, mineral water, chocolate, ... (shrimp,) 0.003200 0.328767 4.600900
138 (mineral water, spaghetti, chocolate, nan) (olive oil,) 0.003866 0.243697 3.700354
139 (mineral water, spaghetti, chocolate, nan) (shrimp,) 0.003466 0.218487 3.057601
140 (frozen vegetables, mineral water, eggs, nan) (milk,) 0.003733 0.411765 3.177620
141 (mineral water, spaghetti, frozen smoothie, nan) (milk,) 0.003200 0.470588 3.631566
142 (frozen vegetables, mineral water, ground beef... (milk,) 0.003733 0.405797 3.131568
143 (frozen vegetables, milk, ground beef, nan) (spaghetti,) 0.003066 0.534884 3.072100
144 (frozen vegetables, mineral water, spaghetti, ... (ground beef,) 0.004399 0.366667 3.731841
145 (frozen vegetables, mineral water, milk, nan) (olive oil,) 0.003333 0.301205 4.573557
146 (frozen vegetables, mineral water, milk, nan) (soup,) 0.003066 0.277108 5.484407
147 (mineral water, milk, spaghetti, nan) (frozen vegetables,) 0.004533 0.288136 3.022804
148 (frozen vegetables, mineral water, spaghetti, ... (shrimp,) 0.003333 0.277778 3.887334
149 (frozen vegetables, mineral water, spaghetti, ... (tomatoes,) 0.003066 0.255556 3.736690
150 (mineral water, spaghetti, olive oil, nan) (ground beef,) 0.003066 0.298701 3.040106
151 (mineral water, tomatoes, ground beef, nan) (spaghetti,) 0.003066 0.560976 3.221959
152 (mineral water, milk, spaghetti, nan) (olive oil,) 0.003333 0.211864 3.216994
153 (mineral water, milk, spaghetti, nan) (tomatoes,) 0.003333 0.211864 3.097846

154 rows × 5 columns