Python Apyori 按提升排序

标签 python apriori

所以我一直在使用 Apyori 从泰坦尼克号幸存者的数据集中获得最低限度的支持。

rules = apriori(titanic, min_support = 0.1, min_confidence = 1.0)
print(list(rules))

这是我将作为输出获得的元素之一的示例

[RelationRecord(items=frozenset({'Crew', 'Adult'}), 
support=0.4020899591094957, ordered_statistics= 
[OrderedStatistic(items_base=frozenset({'Crew'}), 
items_add=frozenset({'Adult'}), confidence=1.0, lift=1.0521032504780115)])

但是,我正在尝试按升力排序,但我不确定如何解决这个问题,因为升力元素似乎在元组内,但我不确定我将如何去关于排序。

非常感谢任何帮助。

谢谢。

最佳答案

您可以将输出放在 Pandas DataFrame 中,然后按升力排序

你有这个:

rules = apriori(titanic, min_support = 0.1, min_confidence = 1.0)

让我们创造成果

results = list(rules)

现在我们要将结果写入 Pandas DataFrame,然后按 Lift 排序

df = pd.DataFrame(columns=('Items','Antecedent','Consequent','Support','Confidence','Lift'))

Support =[]
Confidence = []
Lift = []
Items = []
Antecedent = []
Consequent=[]

for RelationRecord in results:
    for ordered_stat in RelationRecord.ordered_statistics:
        Support.append(RelationRecord.support)
        Items.append(RelationRecord.items)
        Antecedent.append(ordered_stat.items_base)
        Consequent.append(ordered_stat.items_add)
        Confidence.append(ordered_stat.confidence)
        Lift.append(ordered_stat.lift)

df['Items'] = list(map(set, Items))                                   
df['Antecedent'] = list(map(set, Antecedent))
df['Consequent'] = list(map(set, Consequent))
df['Support'] = Support
df['Confidence'] = Confidence
df['Lift']= Lift

按提升度对结果数据框进行排序

df.sort_values(by ='Lift', ascending = False, inplace = True)

关于Python Apyori 按提升排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52688220/

相关文章:

Python:优化函数以在给定候选项集的情况下查找大小为 k 的频繁项集

mysql - 是否可以在mysql语句中运行先验关联规则?

machine-learning - 即使对于少量数据,将 apriori 对象转换为列表也会花费更多时间

python - 自动编译父模板

python - 如何访问 SWIG Python 生成的抽象 c++ 类方法?

python - 防止 Pandas 在垂直和水平方向上连接我的数据框

algorithm - 在 Weka 的 Apriori 算法中获取频率

r - MSapriori 和 CARapriori 算法在 Python 或 R 中的实现

python - 为什么我的 Scrapy 蜘蛛没有按预期运行?

python - 如何从命令行在 Python 中初始化 ctypes 数组