【发布时间】: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