Java 类名:com.alibaba.alink.operator.batch.associationrule.FpGrowthBatchOp
Python 类名:FpGrowthBatchOp

功能介绍

FP Growth(Frequent Pattern growth)算法是一种非时序的关联分析算法. 它利用FP tree生成频繁项集和规则,效率优于传统的Apriori算法。
该算法只需要扫描数据2次。其中第1次扫描获得当个项目的频率,去掉不符合支持度要求的项,并对剩下的项按照置信度降序排序。第2遍扫描是建立一棵频繁项树FP-Tree。
算法生成频繁项集分为两个过程:(1)构建FP树;(2)从FP树中挖掘频繁项集。
论文:Han et al., 《Mining frequent patterns without candidate generation》

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| itemsCol | 项集列名 | 项集列名 | String | ✓ | 所选列类型为 [STRING] | |

| maxConsequentLength | 最大关联规则后继长度 | 最大关联规则后继(consequent)长度 | Integer | | | 1 |

| maxPatternLength | 最大频繁项集长度 | 最大频繁项集长度 | Integer | | | 10 |

| minConfidence | 最小置信度 | 最小置信度,同时包含X和Y的样本与包含X的样本之比,反映了当样本中包含项集X时,项集Y同时出现的概率。 | Double | | | 0.05 |

| minLift | 最小提升度 | 最小提升度,提升度是用来衡量A出现的情况下,是否会对B出现的概率有所提升。 | Double | | | 1.0 |

| minSupportCount | 最小支持度数目 | 最小支持度目,当取值大于或等于0时起作用,当小于0时参数minSupportPercent起作用 | Integer | | | -1 |

| minSupportPercent | 最小支持度占比 | 最小支持度占比,当minSupportCount取值小于0时起作用,当minSupportCount大于或等于0时该参数不起作用 | Double | | | 0.02 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["A,B,C,D"],
  6. ["B,C,E"],
  7. ["A,B,C,E"],
  8. ["B,D,E"],
  9. ["A,B,C,D"],
  10. ])
  11. data = BatchOperator.fromDataframe(df, schemaStr='items string')
  12. fpGrowth = FpGrowthBatchOp() \
  13. .setItemsCol("items") \
  14. .setMinSupportPercent(0.4) \
  15. .setMinConfidence(0.6)
  16. fpGrowth.linkFrom(data)
  17. fpGrowth.print()
  18. fpGrowth.getSideOutput(0).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.associationrule.FpGrowthBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class FpGrowthBatchOpTest {
  9. @Test
  10. public void testFpGrowthBatchOp() throws Exception {
  11. List <Row> df = Arrays.asList(
  12. Row.of("A,B,C,D"),
  13. Row.of("B,C,E"),
  14. Row.of("A,B,C,E"),
  15. Row.of("B,D,E"),
  16. Row.of("A,B,C,D")
  17. );
  18. BatchOperator <?> data = new MemSourceBatchOp(df, "items string");
  19. BatchOperator <?> fpGrowth = new FpGrowthBatchOp()
  20. .setItemsCol("items")
  21. .setMinSupportPercent(0.4)
  22. .setMinConfidence(0.6);
  23. fpGrowth.linkFrom(data);
  24. fpGrowth.print();
  25. fpGrowth.getSideOutput(0).print();
  26. }
  27. }

二元组作为输入

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  4. import org.junit.Test;
  5. public class FpGrowthBatchOpTest {
  6. @Test
  7. public void testFpGrowth() throws Exception {
  8. Row[] rows = new Row[] {
  9. Row.of(1, "A"),
  10. Row.of(1, "B"),
  11. Row.of(1, "C"),
  12. Row.of(1, "D"),
  13. Row.of(2, "B"),
  14. Row.of(2, "C"),
  15. Row.of(2, "E"),
  16. Row.of(3, "A"),
  17. Row.of(3, "B"),
  18. Row.of(3, "C"),
  19. Row.of(3, "E"),
  20. Row.of(4, "B"),
  21. Row.of(4, "D"),
  22. Row.of(4, "E"),
  23. Row.of(5, "A"),
  24. Row.of(5, "B"),
  25. Row.of(5, "C"),
  26. Row.of(5, "D"),
  27. };
  28. BatchOperator data = new MemSourceBatchOp(rows, "id int, item string")
  29. .groupBy("id", "id,CONCAT_AGG(item) AS items");
  30. FpGrowthBatchOp fpGrowth = new FpGrowthBatchOp()
  31. .setItemsCol("items")
  32. .setMinSupportPercent(0.4)
  33. .setMinConfidence(0.6);
  34. fpGrowth.linkFrom(data);
  35. fpGrowth.print();
  36. fpGrowth.getSideOutputAssociationRules().print();
  37. }
  38. }

运行结果

频繁项集输出:

| itemset | supportcount | itemcount | | —- | —- | —- |

| E | 3 | 1 |

| B,E | 3 | 2 |

| C,E | 2 | 2 |

| B,C,E | 2 | 3 |

| D | 3 | 1 |

| B,D | 3 | 2 |

| C,D | 2 | 2 |

| B,C,D | 2 | 3 |

| A,D | 2 | 2 |

| B,A,D | 2 | 3 |

| C,A,D | 2 | 3 |

| B,C,A,D | 2 | 4 |

| A | 3 | 1 |

| B,A | 3 | 2 |

| C,A | 3 | 2 |

| B,C,A | 3 | 3 |

| C | 4 | 1 |

| B,C | 4 | 2 |

| B | 5 | 1 |

关联规则输出:

| rule | itemcount | lift | support_percent | confidence_percent | transaction_count | | —- | —- | —- | —- | —- | —- |

| D=>B | 2 | 1.0000 | 0.6000 | 1.0000 | 3 |

| D=>A | 2 | 1.1111 | 0.4000 | 0.6667 | 2 |

| C,D=>B | 3 | 1.0000 | 0.4000 | 1.0000 | 2 |

| A,D=>B | 3 | 1.0000 | 0.4000 | 1.0000 | 2 |

| B,D=>A | 3 | 1.1111 | 0.4000 | 0.6667 | 2 |

| A,D=>C | 3 | 1.2500 | 0.4000 | 1.0000 | 2 |

| C,D=>A | 3 | 1.6667 | 0.4000 | 1.0000 | 2 |

| C,A,D=>B | 4 | 1.0000 | 0.4000 | 1.0000 | 2 |

| B,A,D=>C | 4 | 1.2500 | 0.4000 | 1.0000 | 2 |

| B,C,D=>A | 4 | 1.6667 | 0.4000 | 1.0000 | 2 |

| C=>A | 2 | 1.2500 | 0.6000 | 0.7500 | 3 |

| C=>B | 2 | 1.0000 | 0.8000 | 1.0000 | 4 |

| B,C=>A | 3 | 1.2500 | 0.6000 | 0.7500 | 3 |

| E=>B | 2 | 1.0000 | 0.6000 | 1.0000 | 3 |

| C,E=>B | 3 | 1.0000 | 0.4000 | 1.0000 | 2 |

| B=>E | 2 | 1.0000 | 0.6000 | 0.6000 | 3 |

| B=>D | 2 | 1.0000 | 0.6000 | 0.6000 | 3 |

| B=>A | 2 | 1.0000 | 0.6000 | 0.6000 | 3 |

| B=>C | 2 | 1.0000 | 0.8000 | 0.8000 | 4 |

| A=>D | 2 | 1.1111 | 0.4000 | 0.6667 | 2 |

| A=>B | 2 | 1.0000 | 0.6000 | 1.0000 | 3 |

| A=>C | 2 | 1.2500 | 0.6000 | 1.0000 | 3 |

| B,A=>D | 3 | 1.1111 | 0.4000 | 0.6667 | 2 |

| C,A=>D | 3 | 1.1111 | 0.4000 | 0.6667 | 2 |

| C,A=>B | 3 | 1.0000 | 0.6000 | 1.0000 | 3 |

| B,A=>C | 3 | 1.2500 | 0.6000 | 1.0000 | 3 |

| B,C,A=>D | 4 | 1.1111 | 0.4000 | 0.6667 | 2 |