频繁模式挖掘

挖掘频繁项集、项集、子序列或其他子结构通常是分析大规模数据集的第一步,多年来,这一直是数据挖掘领域的一个活跃研究课题。我们建议用户参考维基百科上的关联规则学习了解更多信息。

目录

FP-Growth

FP-growth 算法在Han et al., Mining frequent patterns without candidate generation一文中进行了描述,其中“FP”代表频繁模式。给定一个事务数据集,FP-growth 的第一步是计算项频率并识别频繁项。与为相同目的设计的类似 Apriori 的算法不同,FP-growth 的第二步使用后缀树(FP-tree)结构对事务进行编码,而无需显式生成候选项集,而生成候选项集通常成本很高。在第二步之后,可以从 FP-tree 中提取频繁项集。在spark.mllib中,我们实现了一个名为 PFP 的 FP-growth 并行版本,如Li et al., PFP: Parallel FP-growth for query recommendation中所述。PFP 根据事务的后缀分配 FP-tree 的增长工作,因此比单机实现更具可扩展性。我们建议用户参考论文了解更多详细信息。

FP-growth 对*项集*进行操作。项集是唯一项的无序集合。Spark 没有*集合*类型,因此项集表示为数组。

spark.ml的 FP-growth 实现采用以下(超)参数

FPGrowthModel提供

示例

有关更多详细信息,请参阅Python API 文档

from pyspark.ml.fpm import FPGrowth

df = spark.createDataFrame([
    (0, [1, 2, 5]),
    (1, [1, 2, 3, 5]),
    (2, [1, 2])
], ["id", "items"])

fpGrowth = FPGrowth(itemsCol="items", minSupport=0.5, minConfidence=0.6)
model = fpGrowth.fit(df)

# Display frequent itemsets.
model.freqItemsets.show()

# Display generated association rules.
model.associationRules.show()

# transform examines the input items against all the association rules and summarize the
# consequents as prediction
model.transform(df).show()
在 Spark 存储库的“examples/src/main/python/ml/fpgrowth_example.py”中查找完整的示例代码。

有关更多详细信息,请参阅Scala API 文档

import org.apache.spark.ml.fpm.FPGrowth

val dataset = spark.createDataset(Seq(
  "1 2 5",
  "1 2 3 5",
  "1 2")
).map(t => t.split(" ")).toDF("items")

val fpgrowth = new FPGrowth().setItemsCol("items").setMinSupport(0.5).setMinConfidence(0.6)
val model = fpgrowth.fit(dataset)

// Display frequent itemsets.
model.freqItemsets.show()

// Display generated association rules.
model.associationRules.show()

// transform examines the input items against all the association rules and summarize the
// consequents as prediction
model.transform(dataset).show()
在 Spark 存储库的“examples/src/main/scala/org/apache/spark/examples/ml/FPGrowthExample.scala”中查找完整的示例代码。

有关更多详细信息,请参阅Java API 文档

import java.util.Arrays;
import java.util.List;

import org.apache.spark.ml.fpm.FPGrowth;
import org.apache.spark.ml.fpm.FPGrowthModel;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.*;

List<Row> data = Arrays.asList(
  RowFactory.create(Arrays.asList("1 2 5".split(" "))),
  RowFactory.create(Arrays.asList("1 2 3 5".split(" "))),
  RowFactory.create(Arrays.asList("1 2".split(" ")))
);
StructType schema = new StructType(new StructField[]{ new StructField(
  "items", new ArrayType(DataTypes.StringType, true), false, Metadata.empty())
});
Dataset<Row> itemsDF = spark.createDataFrame(data, schema);

FPGrowthModel model = new FPGrowth()
  .setItemsCol("items")
  .setMinSupport(0.5)
  .setMinConfidence(0.6)
  .fit(itemsDF);

// Display frequent itemsets.
model.freqItemsets().show();

// Display generated association rules.
model.associationRules().show();

// transform examines the input items against all the association rules and summarize the
// consequents as prediction
model.transform(itemsDF).show();
在 Spark 存储库的“examples/src/main/java/org/apache/spark/examples/ml/JavaFPGrowthExample.java”中查找完整的示例代码。

有关更多详细信息,请参阅R API 文档

# Load training data

df <- selectExpr(createDataFrame(data.frame(rawItems = c(
  "1,2,5", "1,2,3,5", "1,2"
))), "split(rawItems, ',') AS items")

fpm <- spark.fpGrowth(df, itemsCol="items", minSupport=0.5, minConfidence=0.6)

# Extracting frequent itemsets

spark.freqItemsets(fpm)

# Extracting association rules

spark.associationRules(fpm)

# Predict uses association rules to and combines possible consequents

predict(fpm, df)
在 Spark 存储库的“examples/src/main/r/ml/fpm.R”中查找完整的示例代码。

PrefixSpan

PrefixSpan 是一种序列模式挖掘算法,在Pei et al., Mining Sequential Patterns by Pattern-Growth: The PrefixSpan Approach中进行了描述。我们建议读者参考引用的论文,以正式了解序列模式挖掘问题。

spark.ml的 PrefixSpan 实现采用以下参数

示例

有关更多详细信息,请参阅Python API 文档

from pyspark.ml.fpm import PrefixSpan

df = sc.parallelize([Row(sequence=[[1, 2], [3]]),
                     Row(sequence=[[1], [3, 2], [1, 2]]),
                     Row(sequence=[[1, 2], [5]]),
                     Row(sequence=[[6]])]).toDF()

prefixSpan = PrefixSpan(minSupport=0.5, maxPatternLength=5,
                        maxLocalProjDBSize=32000000)

# Find frequent sequential patterns.
prefixSpan.findFrequentSequentialPatterns(df).show()
在 Spark 存储库的“examples/src/main/python/ml/prefixspan_example.py”中查找完整的示例代码。

有关更多详细信息,请参阅Scala API 文档

import org.apache.spark.ml.fpm.PrefixSpan

val smallTestData = Seq(
  Seq(Seq(1, 2), Seq(3)),
  Seq(Seq(1), Seq(3, 2), Seq(1, 2)),
  Seq(Seq(1, 2), Seq(5)),
  Seq(Seq(6)))

val df = smallTestData.toDF("sequence")
val result = new PrefixSpan()
  .setMinSupport(0.5)
  .setMaxPatternLength(5)
  .setMaxLocalProjDBSize(32000000)
  .findFrequentSequentialPatterns(df)
  .show()
在 Spark 存储库的“examples/src/main/scala/org/apache/spark/examples/ml/PrefixSpanExample.scala”中查找完整的示例代码。

有关更多详细信息,请参阅Java API 文档

import java.util.Arrays;
import java.util.List;

import org.apache.spark.ml.fpm.PrefixSpan;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.*;

List<Row> data = Arrays.asList(
  RowFactory.create(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3))),
  RowFactory.create(Arrays.asList(Arrays.asList(1), Arrays.asList(3, 2), Arrays.asList(1,2))),
  RowFactory.create(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(5))),
  RowFactory.create(Arrays.asList(Arrays.asList(6)))
);
StructType schema = new StructType(new StructField[]{ new StructField(
  "sequence", new ArrayType(new ArrayType(DataTypes.IntegerType, true), true),
  false, Metadata.empty())
});
Dataset<Row> sequenceDF = spark.createDataFrame(data, schema);

PrefixSpan prefixSpan = new PrefixSpan().setMinSupport(0.5).setMaxPatternLength(5);

// Finding frequent sequential patterns
prefixSpan.findFrequentSequentialPatterns(sequenceDF).show();
在 Spark 存储库的“examples/src/main/java/org/apache/spark/examples/ml/JavaPrefixSpanExample.java”中查找完整的示例代码。

有关更多详细信息,请参阅R API 文档

# Load training data

df <- createDataFrame(list(list(list(list(1L, 2L), list(3L))),
                           list(list(list(1L), list(3L, 2L), list(1L, 2L))),
                           list(list(list(1L, 2L), list(5L))),
                           list(list(list(6L)))),
                      schema = c("sequence"))

# Finding frequent sequential patterns
frequency <- spark.findFrequentSequentialPatterns(df, minSupport = 0.5, maxPatternLength = 5L,
                                                  maxLocalProjDBSize = 32000000L)
showDF(frequency)
在 Spark 存储库的“examples/src/main/r/ml/prefixSpan.R”中查找完整的示例代码。