-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-19791] [ML] Add doc and example for fpgrowth #17130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fdce240
fpm doc
YY-OnCall ca12877
change transform to filter
YY-OnCall 4223d94
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall 9ce0093
merge and move
YY-OnCall fa4c734
merge conflict
YY-OnCall 0a5dbb2
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall de1bfc8
comments and doc refine
YY-OnCall d4828b7
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall 9fef280
adapt to itemsCol
YY-OnCall 9e908d0
resolve conflict
YY-OnCall 16f845c
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall 2f0ef8e
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall c957ba5
add python example
YY-OnCall 8d0ccb1
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall e9b090a
docs update
YY-OnCall 99530f1
resolve conflict
YY-OnCall 0fb5a87
refine python example
YY-OnCall 170c31e
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall 2b1efb3
add R example
YY-OnCall 45139cd
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall af0b755
Merge remote-tracking branch 'upstream/master' into fpmdoc
YY-OnCall ea3b973
remove code change per comments
YY-OnCall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --- | ||
| layout: global | ||
| title: Frequent Pattern Mining | ||
| displayTitle: Frequent Pattern Mining | ||
| --- | ||
|
|
||
| Mining frequent items, itemsets, subsequences, or other substructures is usually among the | ||
| first steps to analyze a large-scale dataset, which has been an active research topic in | ||
| data mining for years. | ||
| We refer users to Wikipedia's [association rule learning](http://en.wikipedia.org/wiki/Association_rule_learning) | ||
| for more information. | ||
|
|
||
| **Table of Contents** | ||
|
|
||
| * This will become a table of contents (this text will be scraped). | ||
| {:toc} | ||
|
|
||
| ## FP-Growth | ||
|
|
||
| The FP-growth algorithm is described in the paper | ||
| [Han et al., Mining frequent patterns without candidate generation](http://dx.doi.org/10.1145/335191.335372), | ||
| where "FP" stands for frequent pattern. | ||
| Given a dataset of transactions, the first step of FP-growth is to calculate item frequencies and identify frequent items. | ||
| Different from [Apriori-like](http://en.wikipedia.org/wiki/Apriori_algorithm) algorithms designed for the same purpose, | ||
| the second step of FP-growth uses a suffix tree (FP-tree) structure to encode transactions without generating candidate sets | ||
| explicitly, which are usually expensive to generate. | ||
| After the second step, the frequent itemsets can be extracted from the FP-tree. | ||
| In `spark.mllib`, we implemented a parallel version of FP-growth called PFP, | ||
| as described in [Li et al., PFP: Parallel FP-growth for query recommendation](http://dx.doi.org/10.1145/1454008.1454027). | ||
| PFP distributes the work of growing FP-trees based on the suffixes of transactions, | ||
| and hence is more scalable than a single-machine implementation. | ||
| We refer users to the papers for more details. | ||
|
|
||
| `spark.ml`'s FP-growth implementation takes the following (hyper-)parameters: | ||
|
|
||
| * `minSupport`: the minimum support for an itemset to be identified as frequent. | ||
| For example, if an item appears 3 out of 5 transactions, it has a support of 3/5=0.6. | ||
| * `minConfidence`: minimum confidence for generating Association Rule. Confidence is an indication of how often an | ||
| association rule has been found to be true. For example, if in the transactions itemset `X` appears 4 times, `X` | ||
| and `Y` co-occur only 2 times, the confidence for the rule `X => Y` is then 2/4 = 0.5. The parameter will not | ||
| affect the mining for frequent itemsets, but specify the minimum confidence for generating association rules | ||
| from frequent itemsets. | ||
| * `numPartitions`: the number of partitions used to distribute the work. By default the param is not set, and | ||
| number of partitions of the input dataset is used. | ||
|
|
||
| The `FPGrowthModel` provides: | ||
|
|
||
| * `freqItemsets`: frequent itemsets in the format of DataFrame("items"[Array], "freq"[Long]) | ||
| * `associationRules`: association rules generated with confidence above `minConfidence`, in the format of | ||
| DataFrame("antecedent"[Array], "consequent"[Array], "confidence"[Double]). | ||
| * `transform`: For each transaction in `itemsCol`, the `transform` method will compare its items against the antecedents | ||
| of each association rule. If the record contains all the antecedents of a specific association rule, the rule | ||
| will be considered as applicable and its consequents will be added to the prediction result. The transform | ||
| method will summarize the consequents from all the applicable rules as prediction. The prediction column has | ||
| the same data type as `itemsCol` and does not contain existing items in the `itemsCol`. | ||
|
|
||
|
|
||
| **Examples** | ||
|
|
||
| <div class="codetabs"> | ||
|
|
||
| <div data-lang="scala" markdown="1"> | ||
| Refer to the [Scala API docs](api/scala/index.html#org.apache.spark.ml.fpm.FPGrowth) for more details. | ||
|
|
||
| {% include_example scala/org/apache/spark/examples/ml/FPGrowthExample.scala %} | ||
| </div> | ||
|
|
||
| <div data-lang="java" markdown="1"> | ||
| Refer to the [Java API docs](api/java/org/apache/spark/ml/fpm/FPGrowth.html) for more details. | ||
|
|
||
| {% include_example java/org/apache/spark/examples/ml/JavaFPGrowthExample.java %} | ||
| </div> | ||
|
|
||
| <div data-lang="python" markdown="1"> | ||
| Refer to the [Python API docs](api/python/pyspark.ml.html#pyspark.ml.fpm.FPGrowth) for more details. | ||
|
|
||
| {% include_example python/ml/fpgrowth_example.py %} | ||
| </div> | ||
|
|
||
| <div data-lang="r" markdown="1"> | ||
|
|
||
| Refer to the [R API docs](api/R/spark.fpGrowth.html) for more details. | ||
|
|
||
| {% include_example r/ml/fpm.R %} | ||
| </div> | ||
|
|
||
| </div> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
examples/src/main/java/org/apache/spark/examples/ml/JavaFPGrowthExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.examples.ml; | ||
|
|
||
| // $example on$ | ||
| 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.*; | ||
| // $example off$ | ||
|
|
||
| /** | ||
| * An example demonstrating FPGrowth. | ||
| * Run with | ||
| * <pre> | ||
| * bin/run-example ml.JavaFPGrowthExample | ||
| * </pre> | ||
| */ | ||
| public class JavaFPGrowthExample { | ||
| public static void main(String[] args) { | ||
| SparkSession spark = SparkSession | ||
| .builder() | ||
| .appName("JavaFPGrowthExample") | ||
| .getOrCreate(); | ||
|
|
||
| // $example on$ | ||
| 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(); | ||
| // $example off$ | ||
|
|
||
| spark.stop(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| # $example on$ | ||
| from pyspark.ml.fpm import FPGrowth | ||
| # $example off$ | ||
| from pyspark.sql import SparkSession | ||
|
|
||
| """ | ||
| An example demonstrating FPGrowth. | ||
| Run with: | ||
| bin/spark-submit examples/src/main/python/ml/fpgrowth_example.py | ||
| """ | ||
|
|
||
| if __name__ == "__main__": | ||
| spark = SparkSession\ | ||
| .builder\ | ||
| .appName("FPGrowthExample")\ | ||
| .getOrCreate() | ||
|
|
||
| # $example on$ | ||
| 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() | ||
| # $example off$ | ||
|
|
||
| spark.stop() |
67 changes: 67 additions & 0 deletions
67
examples/src/main/scala/org/apache/spark/examples/ml/FPGrowthExample.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.examples.ml | ||
|
|
||
| // scalastyle:off println | ||
|
|
||
| // $example on$ | ||
| import org.apache.spark.ml.fpm.FPGrowth | ||
| // $example off$ | ||
| import org.apache.spark.sql.SparkSession | ||
|
|
||
| /** | ||
| * An example demonstrating FP-Growth. | ||
| * Run with | ||
| * {{{ | ||
| * bin/run-example ml.FPGrowthExample | ||
| * }}} | ||
| */ | ||
| object FPGrowthExample { | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove blank line |
||
| def main(args: Array[String]): Unit = { | ||
| val spark = SparkSession | ||
| .builder | ||
| .appName(s"${this.getClass.getSimpleName}") | ||
| .getOrCreate() | ||
| import spark.implicits._ | ||
|
|
||
| // $example on$ | ||
| 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() | ||
| // $example off$ | ||
|
|
||
| spark.stop() | ||
| } | ||
| } | ||
| // scalastyle:on println | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add R please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Added reference to R example. Manually checked on generated doc.