关于python中apriori算法的编码关联规则的问题

问题描述 投票:0回答:1

我只是想知道是否有办法仅显示“支持”和“信心”列?我不需要显示前一列,后续列或提升列。

output after calling the association_rules function

这是我下面的代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from apyori import apriori


from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules

dataset = [['plates', 'forks', 'knives'],
 ['plates', 'bowls', 'glasses'],
 ['forks', 'knives'],
 ['glasses', 'forks', 'knives'],
 ['microwave', 'blender'],
 ['dumbbell', 'workout bands', 'water bottle'],
 ['dumbbell', 'sneakers'],
 ['workout bands', 'sneakers'],
 ['bike', 'earbuds'],
 ['yoga mat', 'headphones'],
 ['camera'],
 ['iPad', 'earbuds', 'phone charger'],
 ['iPad', 'laptop', 'laptop charger'],
 ['headphones', 'laptop', 'laptop charger'],
 ['iPad', 'bluetooth speaker', 'phone charger'],
 ['microwave', 'coffee maker'],
 ['camping tent', 'water bottle', 'flashlight'],
 ['sleeping bag', 'yoga mat', 'sneakers'],
 ['tv ', 'tv remote'],
 ['tv', 'tv remote', 'bluetooth speaker']]

te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)

df = pd.DataFrame(te_ary, columns=te.columns_)
frequent_itemsets = apriori(df, min_support=0.1, use_colnames=True)

frequent_itemsets

association_rules(frequent_itemsets, metric="confidence", min_threshold=0.25)

谢谢!

python database pandas data-mining apriori
1个回答
0
投票

我不知道该库,但是API文档说association_rules的返回类型是pandas Dataframe。

所以您可以用它来做标准的熊猫东西:

df = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.25)
print(df[["support", "confidence"]])
© www.soinside.com 2019 - 2024. All rights reserved.