Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

12.1 关联规则算法的概念、原理、流程和程序实现

观看本章节视频

一、关联规则的核心概念

关联规则分析(Association Rule Learning)是发现大数据集中变量间有趣关系的无监督学习方法。在金融领域典型应用包括:

  • 信用卡交叉销售推荐(A→B:申请信用卡客户同时购买保险概率)

  • 金融产品组合分析

  • 反欺诈规则挖掘(异常交易行为关联)

关键术语:

  1. 支持度(Support):P(A∩B),项目集同时出现的概率

  2. 置信度(Confidence):P(B|A),A出现时B的条件概率

  3. 提升度(Lift):P(B|A)/P(B),规则的有效性指标

  4. 杠杆率(Leverage):实际共现频率与独立假设下的期望频率之差

  5. 事务项集: 由一组项(items)组成的集合,这些项通常来自某个事务数据库(transaction database)中的一条记录(即一个事务)

  6. 频繁项集: 数据集中出现频率不低于给定最小支持度阈值(min_support)的项集。

二、Apriori算法流程

  1. 生成事务项集

  2. 生成频繁项集((满足最小支持度阈值)

  3. 基于频繁项集产生关联规则

  4. 基于置信度、提升度、业务需求等赛选关联规则

三、Python实战演示

1 生成事务项集

从 mlxtend 库的 preprocessing 模块中引入 TransactionEncoder 类,专门用于将事务型数据(如购买清单)转换为机器学习算法( Apriori)所需的布尔(0-1)矩阵格式。

import pandas as pd   #导入pandas库
from mlxtend.preprocessing 
import TransactionEncoder 
df = pd.read_csv("purchase_list.csv")  # 从购买清单表读取数据 
transactions = df["商品列表"].apply(lambda x: x.split(", ")).tolist() # 将表中数据转化为事务列表数据 
te = TransactionEncoder() 
te_ary = te.fit(transactions).transform(transactions) 
df_encoded = pd.DataFrame(te_ary, columns=te.columns_)  #将事务列表转化为布尔矩阵

2. 生成频繁项集

from mlxtend.frequent_patterns 
import apriori, association_rules  #挖掘频繁项集(min_support=0.15) 
frequent_itemsets = apriori(df_encoded, min_support=0.15, use_colnames=True) 

3. 基于频繁项集产生关联规则并根据置信度过滤关联规则

rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.6) 
print(rules[["antecedents", "consequents", "support", "confidence"]])

四、扩展思考

  1. 如何将关联规则与监督学习结合构建金融风控模型?

  2. 高频交易数据中关联规则的应用局限?

  3. 非结构化金融文本信息的关联分析可能?