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

借助Python与Excel,如何分析沙发系列关联销售的Top3商品品类?

购物篮分析解决方案:Top3沙发及关联品类识别

一、方案确认:Apriori是正确选择

Apriori算法就是用来挖掘交易数据中频繁项集和关联规则的经典工具,完全匹配你的需求——识别沙发与其他品类的共现销售关系,你的方向没问题。

二、分步操作(Excel+Python结合)

1. 数据预处理

先把数据整理成可用格式:

  • 清洗数据:删除空值、重复交易记录,确保每一行是「唯一交易ID + 商品品类/子品类 + 系列名称」的结构
  • 拆分字段:单独提取交易ID、Category(沙发/休闲椅)、Subcategory(咖啡桌/边桌)、系列名称这几个核心字段

2. 找出Top3沙发系列

直接统计每个沙发系列的交易频次即可:

  • Python实现示例:
import pandas as pd

# 读取Excel数据
df = pd.read_excel('交易数据.xlsx')

# 筛选出所有沙发品类的记录
sofa_data = df[df['Category'] == '沙发']

# 统计各沙发系列的交易次数,取Top3
top3_sofas = sofa_data['系列名称'].value_counts().head(3).index.tolist()
print("Top3沙发系列:", top3_sofas)
  • Excel实现:用数据透视表,行选「系列名称」,值选「交易ID」(设置为计数),排序后取前3

3. 挖掘关联的Top3品类/子品类

针对每个Top3沙发,挖掘与之共现的高频商品:

步骤1:构建交易数据集

把每个交易ID对应的目标品类商品整理成列表:

# 筛选包含Top3沙发的所有交易
target_trans = df[df['交易ID'].isin(sofa_data[sofa_data['系列名称'].isin(top3_sofas)]['交易ID'])]

# 按交易ID分组,仅保留休闲椅、咖啡桌、边桌的系列信息
def filter_target_items(group):
    items = []
    for _, row in group.iterrows():
        if row['Category'] == '休闲椅':
            items.append(f"休闲椅_{row['系列名称']}")
        elif row['Subcategory'] == '咖啡桌':
            items.append(f"咖啡桌_{row['系列名称']}")
        elif row['Subcategory'] == '边桌':
            items.append(f"边桌_{row['系列名称']}")
    return items

transaction_list = target_trans.groupby('交易ID').apply(filter_target_items).tolist()

步骤2:用Apriori生成关联规则

先安装新手友好的mlxtend库:pip install mlxtend,再运行代码:

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

# 把交易列表编码成算法可识别的格式
te = TransactionEncoder()
encoded_data = te.fit(transaction_list).transform(transaction_list)
encoded_df = pd.DataFrame(encoded_data, columns=te.columns_)

# 挖掘频繁项集(最小支持度可根据数据调整,比如设为0.01)
frequent_sets = apriori(encoded_df, min_support=0.01, use_colnames=True)

# 生成关联规则,用置信度(购买沙发后买对应商品的概率)排序
rules = association_rules(frequent_sets, metric="confidence", min_threshold=0.1)

# 逐个筛选Top3沙发的关联品类
for sofa in top3_sofas:
    # 筛选包含当前沙发的规则
    sofa_rel_rules = rules[rules['antecedents'].apply(lambda x: sofa in str(x))]
    
    print(f"\n=== 与【{sofa}】关联的Top3品类 ===")
    # 提取Top3休闲椅
    top_chairs = sofa_rel_rules[sofa_rel_rules['consequents'].apply(lambda x: '休闲椅_' in str(x))].sort_values('confidence', ascending=False).head(3)
    print("Top3休闲椅:")
    print(top_chairs['consequents'].tolist())
    
    # 提取Top3咖啡桌
    top_coffee_tables = sofa_rel_rules[sofa_rel_rules['consequents'].apply(lambda x: '咖啡桌_' in str(x))].sort_values('confidence', ascending=False).head(3)
    print("Top3咖啡桌:")
    print(top_coffee_tables['consequents'].tolist())
    
    # 提取Top3边桌
    top_side_tables = sofa_rel_rules[sofa_rel_rules['consequents'].apply(lambda x: '边桌_' in str(x))].sort_values('confidence', ascending=False).head(3)
    print("Top3边桌:")
    print(top_side_tables['consequents'].tolist())

4. Excel辅助验证

  • 把Python输出的结果导出到Excel,用条件格式高亮高置信度的关联规则
  • 用数据透视表手动统计沙发与各品类的共现频次,交叉验证结果准确性

三、新手避坑提示

  • 支持度和置信度的阈值要灵活调整:68000行数据可以从0.01(支持度)、0.1(置信度)开始测试,再根据结果微调
  • 必须保证交易ID的唯一性,避免重复统计同一交易
  • 如果mlxtend安装有问题,也可以用Excel的COUNTIFS函数手动计算共现频次,适合小范围验证

内容的提问来源于stack exchange,提问作者Abdul Aziz Nathani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 20:01:33