从规则冻结集中提取字符串

Ere*_*she 3 python apriori market-basket-analysis frozenset mlxtend

有以下声明:

rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.2) 
Run Code Online (Sandbox Code Playgroud)

我得到一个格式的规则数据框:

frozenset({'Co_Apples'})
Run Code Online (Sandbox Code Playgroud)

但我需要将 a 提取Co_Apples为字符串。

我怎样才能做到这一点?

小智 6

您可以使用以下代码从 freezeset 类型列获取字符串,然后将该字符串转换为 unicode。

rules["antecedents"] = rules["antecedents"].apply(lambda x: list(x)[0]).astype("unicode")
rules["consequents"] = rules["consequents"].apply(lambda x: list(x)[0]).astype("unicode")
Run Code Online (Sandbox Code Playgroud)

  • 如果您有项目组合(具有多个值的冻结集),使用“list(x)[0]”将仅显示第一个值。您可以使用 '', '.join( list(x) )` 来代替用逗号分隔项目。`规则["前因"] = 规则["前因"].apply(lambda x: ', '.join(list(x))).astype("unicode")` (6认同)

小智 5

rules["antecedents"] = rules["antecedents"].apply(lambda x: ', '.join(list(x))).astype("unicode")
Run Code Online (Sandbox Code Playgroud)

It is work for me. Thanks Frank Herfert save my day!