1 回答
TA贡献1111条经验 获得超0个赞
似乎没有直接的方法可以通过apriori. 但是,一种方法如下:
from mlxtend.frequent_patterns import apriori
frequent_itemsets = apriori(df, min_support=0.1, use_colnames=True)
# lists of columns where value is 1 per row
cols = df.dot(df.columns).map(set).values.tolist()
# use sets to see which rows are a superset of the sets in cols
set_itemsets = map(set,frequent_itemsets.itemsets.values.tolist())
frequent_itemsets['indices'] = [[ix for ix,j in enumerate(cols) if i.issubset(j)]
for i in set_itemsets]
print(frequent_itemsets)
support itemsets indices
0 0.50 (A) [1, 3]
1 1.00 (B) [0, 1, 2, 3]
2 0.50 (C) [2, 3]
3 0.75 (D) [0, 1, 2]
4 0.50 (A, B) [1, 3]
5 0.25 (A, C) [3]
6 0.25 (A, D) [1]
7 0.50 (C, B) [2, 3]
8 0.75 (B, D) [0, 1, 2]
9 0.25 (C, D) [2]
10 0.25 (A, B, C) [3]
11 0.25 (A, B, D) [1]
12 0.25 (C, B, D) [2]
添加回答
举报