best way to implement Apriori in python pandas

Viewed 10044

What is the best way to implement the Apriori algorithm in pandas? So far I got stuck on transforming extracting out the patterns using for loops. Everything from the for loop onward does not work. Is there a vectorized way to do this in pandas?

import pandas as pd
import numpy as np

trans=pd.read_table('output.txt', header=None,index_col=0)

def apriori(trans, support=4):
    ts=pd.get_dummies(trans.unstack().dropna()).groupby(level=1).sum()
    #user input

    collen, rowlen  =ts.shape

    #max length of items
    tssum=ts.sum(axis=1)
    maxlen=tssum.loc[tssum.idxmax()]

    items=list(ts.columns)

    results=[]
    #loop through items
    for c in  range(1, maxlen):
        #generate patterns
        pattern=[]
        for n in  len(pattern):
            #calculate support
            pattern=['supp']=pattern.sum/rowlen
            #filter by support level
            Condit=pattern['supp']> support
            pattern=pattern[Condit]
            results.append(pattern)
   return results

results =apriori(trans)
print results

When I insert this with support 3

        a  b  c  d  e
0                    
11      1  1  1  0  0
666     1  0  0  1  1
10101   0  1  1  1  0
1010    1  1  1  1  0
414147  0  1  1  0  0
10101   1  1  0  1  0
1242    0  0  0  1  1
101     1  1  1  1  0
411     0  0  1  1  1
444     1  1  1  0  0

it should output something like

   Pattern   support
    a         6
    b         7
    c         7
    d         7
    e         3
    a,b       5
    a,c       4
    a,d       4
2 Answers

add support, confidence, and lift caculation。

def apriori(data, set_length=2):
    import pandas as pd
    df_supports = []
    dataset_size = len(data)
    for combination_number in range(1, set_length+1):
        for cols in combinations(data.columns, combination_number):
            supports = data[list(cols)].all(axis=1).sum() * 1.0 / dataset_size
            confidenceAB = data[list(cols)].all(axis=1).sum() * 1.0 / len(data[data[cols[0]]==1])
            confidenceBA = data[list(cols)].all(axis=1).sum() * 1.0 / len(data[data[cols[-1]]==1])
            liftAB = confidenceAB * dataset_size / len(data[data[cols[-1]]==1])
            liftBA = confidenceAB * dataset_size / len(data[data[cols[0]]==1])
            df_supports.append([",".join(cols), supports, confidenceAB, confidenceBA, liftAB, liftBA])
    df_supports = pd.DataFrame(df_supports, columns=['Pattern', 'Support', 'ConfidenceAB', 'ConfidenceBA', 'liftAB', 'liftBA'])
    df_supports.sort_values(by='Support', ascending=False)

    return df_supports
Related