-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-13012] [Documentation] Replace example code in ml-guide.md using include_example #11053
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
12 commits
Select commit
Hold shift + click to select a range
d485a7d
[SPARK-13012] [Documentation] Replace example code in ml-guide.md using
1373996
Fixed the style errors and corrected .py files
6cc98c9
Fixed python style check warnings
48cafb1
Fixed scala warning in this example
3a871c1
Fixed the review comments
50f5fd9
Changed the package from mllib to ml and indentation to 2 spaces for all
8c561f0
Fixed the review comments about the style issues
6b63899
Merge branch 'master' into SPARK-13012
446d6a4
Removed setters for Java beans and spaces inside imports
ea6e77c
Review comments fix about code style
e20c920
Review comments fix
2fe0667
Updated to overwrite the files to support running the example multiple
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
Large diffs are not rendered by default.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
examples/src/main/java/org/apache/spark/examples/ml/JavaDocument.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,43 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * Unlabeled instance type, Spark SQL can infer schema from Java Beans. | ||
| */ | ||
| @SuppressWarnings("serial") | ||
| public class JavaDocument implements Serializable { | ||
|
|
||
| private long id; | ||
| private String text; | ||
|
|
||
| public JavaDocument(long id, String text) { | ||
| this.id = id; | ||
| this.text = text; | ||
| } | ||
|
|
||
| public long getId() { | ||
| return this.id; | ||
| } | ||
|
|
||
| public String getText() { | ||
| return this.text; | ||
| } | ||
| } |
111 changes: 111 additions & 0 deletions
111
...ples/src/main/java/org/apache/spark/examples/ml/JavaEstimatorTransformerParamExample.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,111 @@ | ||
| /* | ||
| * 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; | ||
| // $example off$ | ||
|
|
||
| import org.apache.spark.SparkConf; | ||
| import org.apache.spark.SparkContext; | ||
| // $example on$ | ||
| import org.apache.spark.ml.classification.LogisticRegression; | ||
| import org.apache.spark.ml.classification.LogisticRegressionModel; | ||
| import org.apache.spark.ml.param.ParamMap; | ||
| import org.apache.spark.mllib.linalg.Vectors; | ||
| import org.apache.spark.mllib.regression.LabeledPoint; | ||
| import org.apache.spark.sql.DataFrame; | ||
| import org.apache.spark.sql.Row; | ||
| // $example off$ | ||
| import org.apache.spark.sql.SQLContext; | ||
|
|
||
| /** | ||
| * Java example for Estimator, Transformer, and Param. | ||
| */ | ||
| public class JavaEstimatorTransformerParamExample { | ||
| public static void main(String[] args) { | ||
| SparkConf conf = new SparkConf() | ||
| .setAppName("JavaEstimatorTransformerParamExample"); | ||
| SparkContext sc = new SparkContext(conf); | ||
| SQLContext sqlContext = new SQLContext(sc); | ||
|
|
||
| // $example on$ | ||
| // Prepare training data. | ||
| // We use LabeledPoint, which is a JavaBean. Spark SQL can convert RDDs of JavaBeans into | ||
| // DataFrames, where it uses the bean metadata to infer the schema. | ||
| DataFrame training = sqlContext.createDataFrame( | ||
| Arrays.asList( | ||
| new LabeledPoint(1.0, Vectors.dense(0.0, 1.1, 0.1)), | ||
| new LabeledPoint(0.0, Vectors.dense(2.0, 1.0, -1.0)), | ||
| new LabeledPoint(0.0, Vectors.dense(2.0, 1.3, 1.0)), | ||
| new LabeledPoint(1.0, Vectors.dense(0.0, 1.2, -0.5)) | ||
| ), LabeledPoint.class); | ||
|
|
||
| // Create a LogisticRegression instance. This instance is an Estimator. | ||
| LogisticRegression lr = new LogisticRegression(); | ||
| // Print out the parameters, documentation, and any default values. | ||
| System.out.println("LogisticRegression parameters:\n" + lr.explainParams() + "\n"); | ||
|
|
||
| // We may set parameters using setter methods. | ||
| lr.setMaxIter(10).setRegParam(0.01); | ||
|
|
||
| // Learn a LogisticRegression model. This uses the parameters stored in lr. | ||
| LogisticRegressionModel model1 = lr.fit(training); | ||
| // Since model1 is a Model (i.e., a Transformer produced by an Estimator), | ||
| // we can view the parameters it used during fit(). | ||
| // This prints the parameter (name: value) pairs, where names are unique IDs for this | ||
| // LogisticRegression instance. | ||
| System.out.println("Model 1 was fit using parameters: " + model1.parent().extractParamMap()); | ||
|
|
||
| // We may alternatively specify parameters using a ParamMap. | ||
| ParamMap paramMap = new ParamMap() | ||
| .put(lr.maxIter().w(20)) // Specify 1 Param. | ||
| .put(lr.maxIter(), 30) // This overwrites the original maxIter. | ||
| .put(lr.regParam().w(0.1), lr.threshold().w(0.55)); // Specify multiple Params. | ||
|
|
||
| // One can also combine ParamMaps. | ||
| ParamMap paramMap2 = new ParamMap() | ||
| .put(lr.probabilityCol().w("myProbability")); // Change output column name | ||
| ParamMap paramMapCombined = paramMap.$plus$plus(paramMap2); | ||
|
|
||
| // Now learn a new model using the paramMapCombined parameters. | ||
|
Contributor
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. reformat comments |
||
| // paramMapCombined overrides all parameters set earlier via lr.set* methods. | ||
| LogisticRegressionModel model2 = lr.fit(training, paramMapCombined); | ||
| System.out.println("Model 2 was fit using parameters: " + model2.parent().extractParamMap()); | ||
|
|
||
| // Prepare test documents. | ||
| DataFrame test = sqlContext.createDataFrame(Arrays.asList( | ||
| new LabeledPoint(1.0, Vectors.dense(-1.0, 1.5, 1.3)), | ||
| new LabeledPoint(0.0, Vectors.dense(3.0, 2.0, -0.1)), | ||
| new LabeledPoint(1.0, Vectors.dense(0.0, 2.2, -1.5)) | ||
| ), LabeledPoint.class); | ||
|
|
||
| // Make predictions on test documents using the Transformer.transform() method. | ||
| // LogisticRegression.transform will only use the 'features' column. | ||
| // Note that model2.transform() outputs a 'myProbability' column instead of the usual | ||
| // 'probability' column since we renamed the lr.probabilityCol parameter previously. | ||
| DataFrame results = model2.transform(test); | ||
| for (Row r : results.select("features", "label", "myProbability", "prediction").collect()) { | ||
| System.out.println("(" + r.get(0) + ", " + r.get(1) + ") -> prob=" + r.get(2) | ||
| + ", prediction=" + r.get(3)); | ||
| } | ||
| // $example off$ | ||
|
|
||
| sc.stop(); | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
examples/src/main/java/org/apache/spark/examples/ml/JavaLabeledDocument.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,38 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * Labeled instance type, Spark SQL can infer schema from Java Beans. | ||
| */ | ||
| @SuppressWarnings("serial") | ||
| public class JavaLabeledDocument extends JavaDocument implements Serializable { | ||
|
|
||
| private double label; | ||
|
|
||
| public JavaLabeledDocument(long id, String text, double label) { | ||
| super(id, text); | ||
| this.label = label; | ||
| } | ||
|
|
||
| public double getLabel() { | ||
| return this.label; | ||
| } | ||
| } |
122 changes: 122 additions & 0 deletions
122
...c/main/java/org/apache/spark/examples/ml/JavaModelSelectionViaCrossValidationExample.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,122 @@ | ||
| /* | ||
| * 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; | ||
| // $example off$ | ||
|
|
||
| import org.apache.spark.SparkConf; | ||
| import org.apache.spark.SparkContext; | ||
| // $example on$ | ||
| import org.apache.spark.ml.Pipeline; | ||
| import org.apache.spark.ml.PipelineStage; | ||
| import org.apache.spark.ml.classification.LogisticRegression; | ||
| import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator; | ||
| import org.apache.spark.ml.feature.HashingTF; | ||
| import org.apache.spark.ml.feature.Tokenizer; | ||
| import org.apache.spark.ml.param.ParamMap; | ||
| import org.apache.spark.ml.tuning.CrossValidator; | ||
| import org.apache.spark.ml.tuning.CrossValidatorModel; | ||
| import org.apache.spark.ml.tuning.ParamGridBuilder; | ||
| import org.apache.spark.sql.DataFrame; | ||
| import org.apache.spark.sql.Row; | ||
| // $example off$ | ||
| import org.apache.spark.sql.SQLContext; | ||
|
|
||
| /** | ||
| * Java example for Model Selection via Cross Validation. | ||
| */ | ||
| public class JavaModelSelectionViaCrossValidationExample { | ||
| public static void main(String[] args) { | ||
| SparkConf conf = new SparkConf() | ||
| .setAppName("JavaModelSelectionViaCrossValidationExample"); | ||
| SparkContext sc = new SparkContext(conf); | ||
| SQLContext sqlContext = new SQLContext(sc); | ||
|
|
||
| // $example on$ | ||
| // Prepare training documents, which are labeled. | ||
| DataFrame training = sqlContext.createDataFrame(Arrays.asList( | ||
| new JavaLabeledDocument(0L, "a b c d e spark", 1.0), | ||
| new JavaLabeledDocument(1L, "b d", 0.0), | ||
| new JavaLabeledDocument(2L,"spark f g h", 1.0), | ||
| new JavaLabeledDocument(3L, "hadoop mapreduce", 0.0), | ||
| new JavaLabeledDocument(4L, "b spark who", 1.0), | ||
| new JavaLabeledDocument(5L, "g d a y", 0.0), | ||
| new JavaLabeledDocument(6L, "spark fly", 1.0), | ||
| new JavaLabeledDocument(7L, "was mapreduce", 0.0), | ||
| new JavaLabeledDocument(8L, "e spark program", 1.0), | ||
| new JavaLabeledDocument(9L, "a e c l", 0.0), | ||
| new JavaLabeledDocument(10L, "spark compile", 1.0), | ||
| new JavaLabeledDocument(11L, "hadoop software", 0.0) | ||
| ), JavaLabeledDocument.class); | ||
|
|
||
| // Configure an ML pipeline, which consists of three stages: tokenizer, hashingTF, and lr. | ||
| Tokenizer tokenizer = new Tokenizer() | ||
| .setInputCol("text") | ||
| .setOutputCol("words"); | ||
| HashingTF hashingTF = new HashingTF() | ||
| .setNumFeatures(1000) | ||
| .setInputCol(tokenizer.getOutputCol()) | ||
| .setOutputCol("features"); | ||
| LogisticRegression lr = new LogisticRegression() | ||
| .setMaxIter(10) | ||
| .setRegParam(0.01); | ||
| Pipeline pipeline = new Pipeline() | ||
| .setStages(new PipelineStage[] {tokenizer, hashingTF, lr}); | ||
|
|
||
| // We use a ParamGridBuilder to construct a grid of parameters to search over. | ||
| // With 3 values for hashingTF.numFeatures and 2 values for lr.regParam, | ||
| // this grid will have 3 x 2 = 6 parameter settings for CrossValidator to choose from. | ||
| ParamMap[] paramGrid = new ParamGridBuilder() | ||
| .addGrid(hashingTF.numFeatures(), new int[] {10, 100, 1000}) | ||
| .addGrid(lr.regParam(), new double[] {0.1, 0.01}) | ||
| .build(); | ||
|
|
||
| // We now treat the Pipeline as an Estimator, wrapping it in a CrossValidator instance. | ||
| // This will allow us to jointly choose parameters for all Pipeline stages. | ||
| // A CrossValidator requires an Estimator, a set of Estimator ParamMaps, and an Evaluator. | ||
| // Note that the evaluator here is a BinaryClassificationEvaluator and its default metric | ||
| // is areaUnderROC. | ||
| CrossValidator cv = new CrossValidator() | ||
| .setEstimator(pipeline) | ||
| .setEvaluator(new BinaryClassificationEvaluator()) | ||
| .setEstimatorParamMaps(paramGrid).setNumFolds(2); // Use 3+ in practice | ||
|
|
||
| // Run cross-validation, and choose the best set of parameters. | ||
| CrossValidatorModel cvModel = cv.fit(training); | ||
|
|
||
| // Prepare test documents, which are unlabeled. | ||
| DataFrame test = sqlContext.createDataFrame(Arrays.asList( | ||
| new JavaDocument(4L, "spark i j k"), | ||
| new JavaDocument(5L, "l m n"), | ||
| new JavaDocument(6L, "mapreduce spark"), | ||
| new JavaDocument(7L, "apache hadoop") | ||
| ), JavaDocument.class); | ||
|
|
||
| // Make predictions on test documents. cvModel uses the best model found (lrModel). | ||
| DataFrame predictions = cvModel.transform(test); | ||
| for (Row r : predictions.select("id", "text", "probability", "prediction").collect()) { | ||
| System.out.println("(" + r.get(0) + ", " + r.get(1) + ") --> prob=" + r.get(2) | ||
| + ", prediction=" + r.get(3)); | ||
| } | ||
| // $example off$ | ||
|
|
||
| sc.stop(); | ||
| } | ||
| } |
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.
remove the blank line