如何修复“DeprecationWarning:具有非 bool 类型的 DataFrame 会导致更差的计算性能......”

dnz*_*z07 3 python apriori dataframe pandas

我一直在尝试用Python实现Apriori算法。网上有几个例子,它们都使用类似的方法,并且大多使用相同的示例数据集。参考链接:https://www.kaggle.com/code/rockystats/apriori-algorithm-or-market-basket-analysis/notebook (从[26]行开始)

我有一个不同的数据集,其结构与在线示例数据集相同。我不断得到

“DeprecationWarning:具有非 bool 类型的 DataFrame 会导致更差的计算性能,并且将来可能会停止对它们的支持。请使用具有 bool 类型的 DataFrame”

错误。

这是我的代码:

 import pandas as pd
    import numpy as np
    from mlxtend.frequent_patterns import apriori, association_rules
    
    df1 = pd.read_csv(r'C:\Users\USER\dataset', sep=';')

    df=df1.fillna(0)
    basket = pd.pivot_table(data=df, index='cust_id', columns='Product', values='quantity', aggfunc='count',fill_value=0.0)
       
      def convert_into_binary(x):
        if x > 0:
            return 1
        else:
            return 0

          
       basket_sets = basket.applymap(convert_into_binary)
    
     
     frequent_itemsets = apriori(basket_sets, min_support=0.07, use_colnames=True)
    
    print(frequent_itemsets)
    
    # association rule
    rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
   
    print(rules)
Run Code Online (Sandbox Code Playgroud)

此外,在代码的最后一步中,我得到一个空数据框;我可以看到数据集的列标题,但输出为空。

空数据框列:[前因、后果、前因支持、后因支持、支持、信心、提升、杠杆、信念] 索引:[]

我不确定这个问题是否与我遇到的错误有关。我是 python 新手,非常感谢在这个问题上的帮助和支持。

ll *_* ll 5

即使将数据帧字段转换为 0 和 1 后,我也遇到了同样的问题。

修复只是确保 apriori 模块知道数据帧是布尔类型,因此在您的情况下您应该运行以下命令:

    frequent_itemsets = apriori(basket_sets.astype('bool'), min_support=0.07, use_colnames=True)
Run Code Online (Sandbox Code Playgroud)

此外,在代码的最后一步中,我得到一个空数据框;我可以看到数据集的列标题,但输出为空。

尝试使用较小的 min_support