【问题标题】:best way to implement Apriori in python pandas在 python pandas 中实现 Apriori 的最佳方法
【发布时间】:2013-12-31 16:38:33
【问题描述】:

在 pandas 中实现 Apriori 算法的最佳方法是什么?到目前为止,我一直坚持使用 for 循环转换提取模式。从 for 循环开始的一切都不起作用。在 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

当我用支持 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

它应该输出类似的东西

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

【问题讨论】:

  • 你的回报在错误的地方,对于 n in len(pattern) 也是错误的......
  • @AndyHayden 第一个是粘贴错误,当我手动操作时,模式长度不起作用,因为我还没有弄清楚如何生成模式组合,例如 a、b; a, c;或 a,b,c
  • 如何定义支持?我有一个猜测,但它与你的 a,d 值不相符(我以为是 4,但你说是 3。)
  • @DSM 如果我使用 support =4 而不是 3,所有支持 =3 的行都将被丢弃
  • 不,我的意思是我不知道“支持”是什么意思。为什么“a,d”是 3?

标签: python pandas machine-learning


【解决方案1】:

添加支持、信心和提升计算。

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

【讨论】:

    【解决方案2】:

    假设我明白你的目标,也许

    from itertools import combinations
    def get_support(df):
        pp = []
        for cnum in range(1, len(df.columns)+1):
            for cols in combinations(df, cnum):
                s = df[list(cols)].all(axis=1).sum()
                pp.append([",".join(cols), s])
        sdf = pd.DataFrame(pp, columns=["Pattern", "Support"])
        return sdf
    

    会让你开始:

    >>> s = get_support(df)
    >>> s[s.Support >= 3]
       Pattern  Support
    0        a        6
    1        b        7
    2        c        7
    3        d        7
    4        e        3
    5      a,b        5
    6      a,c        4
    7      a,d        4
    9      b,c        6
    10     b,d        4
    12     c,d        4
    14     d,e        3
    15   a,b,c        4
    16   a,b,d        3
    21   b,c,d        3
    
    [15 rows x 2 columns]
    

    【讨论】:

    • 是的,就是这样。但是有没有办法只用熊猫来做到这一点?
    • @user3084006:我不确定,不幸的是我没有时间花在这个问题上。希望其他人可以帮助您!
    • 谢谢你解决了基本问题我应该再发一个问题
    • 只是为了确定;有更有效的方法来查找频繁项集,请在此处查看我的 cmets:codereview.stackexchange.com/a/112047/24783
    猜你喜欢
    • 2020-06-28
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2010-10-14
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多