1. \textbf{1. } 1. 一些基本概念
1️⃣频繁项集
- 定义: X X X频繁 ⇔ 等价于 \xLeftrightarrow{等价于} 等价于 事务集 D D D中含 X X X的 T i T_i Ti数量( X X X支持度)超过阈值 ⇔ 等价于 X \xLeftrightarrow{等价于}X 等价于 X满足最小支持度
- 性质:
- 如果 X X X频繁 → X \text{→}X →X的子集也一定频繁(向下封闭)
- 如果 X X X非频繁 → X \text{→}X →X的超集(如 { X , x n + 1 , x n + 2 , . . . } \{X,x_{n+1},x_{n+2},...\} {X,xn+1,xn+2,...})也一定非频繁
2️⃣闭合集 & \& &最大集:为解决组合爆炸( 规则数目 ∝ 2 数据集规模 规则数目\text{ ∝ }2^{数据集规模} 规则数目 ∝ 2数据集规模)问题
- 定义:对于事务集 D D D
集合 含义 意义 闭合集(模式) X X X闭合 ⇔ 等价于 X \xLeftrightarrow{等价于}X 等价于 X频繁 ∩ X \text{∩}X ∩X所有超集的支持度小于 X X X的 D D D无损压缩 最大集(模式) X X X最大 ⇔ 等价于 X \xLeftrightarrow{等价于}X 等价于 X频繁 ∩ X \text{∩}X ∩X所有超集都非频繁 D D D有损压缩 - 示例: D = { A 1 = ⟨ a 1 , a 2 , … , a 100 ⟩ , A 2 = ⟨ a 1 , a 2 , … , a 50 ⟩ } D\text{=}\left\{A_1\text{=}\left\langle a_1, a_2, \ldots, a_{100}\right\rangle,A_2\text{=}\left\langle a_1, a_2, \ldots, a_{50}\right\rangle\right\} D={A1=⟨a1,a2,…,a100⟩,A2=⟨a1,a2,…,a50⟩},最小支持度 =1 \text{=1} =1
集合 Item \textbf{Item} Item 闭合模式 A 1 A_1 A1(支持度 =1 / \text{=1}/ =1/无频繁超集), A 2 A_2 A2(支持度 =2/ \text{=2/} =2/频繁超集支持度 =2 \text{=2} =2) 最大模式 A 1 A_1 A1(支持度 =1 / \text{=1}/ =1/无频繁超集) 所有模式 所有的频繁子集,比如 ⟨ a 1 , … , a 49 ⟩ \left\langle a_1, \ldots, a_{49}\right\rangle ⟨a1,…,a49⟩
2. \textbf{2.} 2. Apriori \textbf{Apriori} Apriori算法
0️⃣总论
- 原有方案:原始数据 → ( 暴力 ) 生成 \xrightarrow{(暴力)生成} (暴力)生成关联规则
- 现有方案:原始数据 → Apriori算法 \xrightarrow{\text{Apriori}算法} Apriori算法频繁项集 → 生成 \xrightarrow{生成} 生成关联规则
1️⃣算法流程:原始数据 → Apriori算法 \xrightarrow{\text{Apriori}算法} Apriori算法频繁项集
![]()
- 初始化:事务 D → 清洗 D\xrightarrow{清洗} D清洗单项 { { T 1 } , { T 2 } , . . . , { T n } } → 满足最小支持度 \small\{\{\mathrm{T_1}\}, \{\mathrm{T_2}\},..., \{\mathrm{T_n}\}\}\xrightarrow{满足最小支持度} {{T1},{T2},...,{Tn}}满足最小支持度 L 1 = { { T i 1 } , { T i 2 } , . . . , { T i m } } L_1\text{=}\small\{\{\mathrm{T_{i_1}}\}, \{\mathrm{T_{i_2}}\},..., \{\mathrm{T_{i_m}}\}\} L1={{Ti1},{Ti2},...,{Tim}}
- 主循环:候选集 L 1 → 执行以下操作 L_1\xrightarrow{执行以下操作} L1执行以下操作候选集 L 2 L_2 L2 (下一轮循环)
- 组合:本轮频繁项集 L 1 → ( 具体见例子 ) 两两组合 L_{1}\xrightarrow[(具体见例子)]{两两组合} L1两两组合(具体见例子)候选项集 C 2 C_2 C2
- 剪枝:候选项集 C 2 → 去处有非频繁子集的项 C_2\xrightarrow{去处有非频繁子集的项} C2去处有非频繁子集的项下一候频繁集 L 2 L_2 L2,以进行下轮以此循环
- 输出:当循环到 L α L_\alpha Lα为空集时停止循环,频繁项集 L = { L 1 ∪ L 2 ∪ . . . ∪ L α } L\text{=}\{L_1\text{∪}L_2\text{∪}...\text{∪}L_\alpha\} L={L1∪L2∪...∪Lα}
2️⃣频繁项集 → 生成 \xrightarrow{生成} 生成关联规则
- 基本流程:
- 子集:频繁项集 L L L所有非空子集 S = { S 1 , S 2 , . . . , S 2 ∣ L ∣ − 2 } S\text{=}\{S_1,S_2,...,S_{2^{|L|}-2}\} S={S1,S2,...,S2∣L∣−2},任意 S i → S j S_i\text{→}S_j Si→Sj组合满足支持度
- 规则:对每个子集计算 S i → L \ S i S_i\text{→}L\backslash{}S_i Si→L\Si的置信度,若小于阈值则视 S i → L \ S i S_i\text{→}L\backslash{}S_i Si→L\Si强相关
- 效率优化:
- 原理:对 ( X → L - X ) (X\text{→}L\text{-}X) (X→L-X)置信度不满足阈值 → X ′ ⊆ X ( X ′ → L - X ′ ) \xrightarrow{X^{\prime}\text{⊆}X}(X^{\prime}\text{→}L\text{-}X^{\prime}) X′⊆X(X′→L-X′)也不满足
- 优化:先验证 L - X L\text{-}X L-X只有单一项的规则,若不满足则剪枝/满足则再去验证 L - X L\text{-}X L-X多项的规则
3. \textbf{3.} 3. Apriori \textbf{Apriori} Apriori算法示例
0️⃣基本条件:事务及其项集如下表,设定最小支持度为 2 2 2
T \small\textbf{T} T Beer \small\textbf{Beer} Beer Diap. \small\textbf{Diap.} Diap. Powd \small\textbf{Powd} Powd Bread \small\textbf{Bread} Bread Umbre. \small\textbf{Umbre.} Umbre. Milk \small\textbf{Milk} Milk Deter. \small\textbf{Deter.} Deter. Cola \small\textbf{Cola} Cola 1 1 1 ✅ ✅ ✅ ✅ ✅ ❌ ❌ ❌ 2 2 2 ❌ ✅ ✅ ❌ ❌ ❌ ❌ ❌ 3 3 3 ✅ ✅ ❌ ❌ ❌ ✅ ❌ ❌ 4 4 4 ✅ ✅ ❌ ❌ ❌ ❌ ✅ ❌ 5 5 5 ✅ ❌ ❌ ❌ ❌ ✅ ❌ ✅ 1️⃣ Apriori \text{Apriori} Apriori算法:得到频繁项集
- 初始化: L 1 ⇐ 出现超过两次的商品 { Beer,Diap,Powd,Milk } L_1\xLeftarrow{出现超过两次的商品}\{\small\text{Beer,Diap,Powd,Milk}\} L1出现超过两次的商品{Beer,Diap,Powd,Milk}
- 主循环:当前为 L 1 L_1 L1
- 组合: L 1 → 简单两两组合 C 2 = { [ Beer Diap ] , [ Beer Powd ] , [ Beer Milk ] , [ Diap Powd ] , [ Diap Milk ] , [ Powd Milk ] } L_1 \xrightarrow{\text{简单两两组合}} C_2 \text{=} \left\{ \small {\begin{bmatrix} \text{Beer} \\ \text{Diap} \end{bmatrix}}, \cancel{\begin{bmatrix} \text{Beer} \\ \text{Powd} \end{bmatrix}}, \begin{bmatrix} \text{Beer} \\ \text{Milk} \end{bmatrix}, \begin{bmatrix} \text{Diap} \\ \text{Powd} \end{bmatrix}, \cancel{ \begin{bmatrix} \text{Diap} \\ \text{Milk} \end{bmatrix}}, \cancel{ \begin{bmatrix} \text{Powd} \\ \text{Milk} \end{bmatrix}} \right\} L1简单两两组合C2={[BeerDiap],[BeerPowd] ,[BeerMilk],[DiapPowd],[DiapMilk] ,[PowdMilk] }
- 剪枝: C 2 → 剪掉包含非频繁子集的 L 2 = { [ Beer Diap ] , [ Beer Milk ] , [ Diap Powd ] } C_2\xrightarrow{剪掉包含非频繁子集的}L_2\text{=}\left\{ \small \begin{bmatrix} \text{Beer} \\ \text{Diap} \end{bmatrix}, \begin{bmatrix} \text{Beer} \\ \text{Milk} \end{bmatrix}, \begin{bmatrix} \text{Diap} \\ \text{Powd} \end{bmatrix} \right\} C2剪掉包含非频繁子集的L2={[BeerDiap],[BeerMilk],[DiapPowd]}
- 主循环:当前为 L 2 L_2 L2
- 组合: L 2 → 组合逻辑见下表 两两组合 C 3 = { [ Beer Diap Milk ] , [ Beer Diap Powd ] } L_2 \xrightarrow[组合逻辑见下表]{\text{两两组合}} C_3\text{=}\left\{ \small \cancel{ \begin{bmatrix} \text{Beer} \\ \text{Diap} \\ \text{Milk} \\ \end{bmatrix}}, \cancel{ \begin{bmatrix} \text{Beer} \\ \text{Diap} \\ \text{Powd} \end{bmatrix}}\right\} L2两两组合组合逻辑见下表C3={[BeerDiapMilk] ,[BeerDiapPowd] }
合并的集 条件: 二者有 k -1 k\text{-1} k-1项(此处为 1 1 1)相等 操作 L 2 [ 1 ] / L 2 [ 2 ] L_2[1]/L_2[2] L2[1]/L2[2] 共有 Beer→ \text{Beer}\text{→} Beer→满足 执行组合 L 2 [ 1 ] / L 2 [ 3 ] L_2[1]/L_2[3] L2[1]/L2[3] 共有 Diap→ \text{Diap}\text{→} Diap→满足 执行组合 L 2 [ 2 ] / L 2 [ 3 ] L_2[2]/L_2[3] L2[2]/L2[3] 不满足 不执行组合 - 剪枝: C 2 → 剪掉包含非频繁子集的 L 3 = ∅ C_2\xrightarrow{剪掉包含非频繁子集的}L_3\text{=}\varnothing C2剪掉包含非频繁子集的L3=∅,故终止循环
- 输出: L = L 1 ∪ L 2 = { Beer , Diap , Powd , Milk , [ Beer Diap ] , [ Beer Milk ] , [ Diap Powd ] } L\text{=}L_1\text{∪}L_2\text{=}\left\{ \small \text{Beer}, \text{Diap}, \text{Powd}, \text{Milk}, \begin{bmatrix} \text{Beer} \\ \text{Diap} \end{bmatrix}, \begin{bmatrix} \text{Beer} \\ \text{Milk} \end{bmatrix}, \begin{bmatrix} \text{Diap} \\ \text{Powd} \end{bmatrix} \right\} L=L1∪L2={Beer,Diap,Powd,Milk,[BeerDiap],[BeerMilk],[DiapPowd]}
2️⃣生成规则 → Item Support(A,B) Support A Confidence Beer → Diaper 60 % 80 % 75 % Beer → Milk 40 % 80 % 50 % Diaper → Powd 40 % 80 % 50 % Diaper → Beer 60 % 80 % 75 % Milk → Beer 40 % 40 % 100 % Powd → Diaper 40 % 40 % 100 % etc..... \text{→ } \small \begin{array}{|c|c|c|c|} \hline \text{Item} & \text{Support(A,B)} & \text{Support A} & \text{Confidence} \\ \hline \text{Beer} \to \text{Diaper} & 60 \% & 80 \% & 75 \% \\ \hline \text{Beer} \to \text{Milk} & 40 \% & 80 \% & 50 \% \\ \hline \text{Diaper} \to \text{Powd} & 40 \% & 80 \% & 50 \% \\ \hline \text{Diaper} \to \text{Beer} & 60 \% & 80 \% & 75 \% \\ \hline \text{Milk} \to \text{Beer} & 40 \% & 40 \% & 100 \% \\ \hline \text{Powd} \to \text{Diaper} & 40 \% & 40 \% & 100 \% \\ \hline \end{array} \text{ etc.....} → ItemBeer→DiaperBeer→MilkDiaper→PowdDiaper→BeerMilk→BeerPowd→DiaperSupport(A,B)60%40%40%60%40%40%Support A80%80%80%80%40%40%Confidence75%50%50%75%100%100% etc.....