You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Apriori算法中的DeprecationWarning及空DataFrame问题

Apriori算法实现问题排查与解决

问题描述

  • 运行代码时收到弃用警告:

DeprecationWarning: DataFrames with non-bool types result in worse computational performance and their support might be discontinued in the future.Please use a DataFrame with bool type

  • 最终生成的关联规则DataFrame为空,仅显示列名:

Empty DataFrame Columns: [antecedents, consequents, antecedent support, consequent support, support, confidence, lift, leverage, conviction] Index: []

用户原代码:

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)

# 生成关联规则
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
print(rules)

解决方案

1. 解决弃用警告

警告核心要求是使用bool类型的DataFrame,当前代码返回的1/0属于整数类型,直接修改转换逻辑即可:

  • 简化转换函数:
def convert_into_binary(x):
    return x > 0
  • 更高效的写法:直接用astype(bool)转换整个矩阵
basket_sets = basket.astype(bool)

2. 解决关联规则为空的问题

空规则本质是没有足够的频繁项集来生成符合条件的规则,常见原因和解决方法:

  • 降低min_support阈值:当前设置的0.07可能过高,尝试调整为0.01或更低,先确认frequent_itemsets是否有足够多的项集(如果只有单个商品的项集,无法生成关联规则)
  • 检查数据集有效性:确认cust_id和Product列数据格式正确,没有大量无效值;查看basket_sets的结构,确保每个用户的购物篮转换正确
  • 验证参数逻辑:association_rules的min_threshold=1已经是lift的最低值,所以重点还是调整频繁项集的生成参数

修正后完整代码

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)

# 生成购物篮矩阵并转换为bool类型
basket = pd.pivot_table(data=df, index='cust_id', columns='Product', values='quantity', aggfunc='count', fill_value=0)
basket_sets = basket.astype(bool)

# 降低最小支持度阈值,生成频繁项集
frequent_itemsets = apriori(basket_sets, min_support=0.01, use_colnames=True)
print("频繁项集:")
print(frequent_itemsets)

# 生成关联规则
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
print("\n关联规则:")
print(rules)

内容的提问来源于stack exchange,提问作者dnz07

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 09:35:20