aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorWeichenXu <WeichenXu123@outlook.com>2016-06-16 17:35:40 -0700
committerYanbo Liang <ybliang8@gmail.com>2016-06-16 17:35:40 -0700
commit9040d83bc2cdce06dab0e1bdee4f796da9a9a55c (patch)
tree1d13dfa807220fb5d796d23f3e1d60802f680b2b /examples
parentd9c6628c47de547dc537310e3c775c7f3e0e4a12 (diff)
downloadspark-9040d83bc2cdce06dab0e1bdee4f796da9a9a55c.tar.gz
spark-9040d83bc2cdce06dab0e1bdee4f796da9a9a55c.tar.bz2
spark-9040d83bc2cdce06dab0e1bdee4f796da9a9a55c.zip
[SPARK-15608][ML][EXAMPLES][DOC] add examples and documents of ml.isotonic regression
## What changes were proposed in this pull request? add ml doc for ml isotonic regression add scala example for ml isotonic regression add java example for ml isotonic regression add python example for ml isotonic regression modify scala example for mllib isotonic regression modify java example for mllib isotonic regression modify python example for mllib isotonic regression add data/mllib/sample_isotonic_regression_libsvm_data.txt delete data/mllib/sample_isotonic_regression_data.txt ## How was this patch tested? N/A Author: WeichenXu <WeichenXu123@outlook.com> Closes #13381 from WeichenXu123/add_isotonic_regression_doc.
Diffstat (limited to 'examples')
-rw-r--r--examples/src/main/java/org/apache/spark/examples/ml/JavaIsotonicRegressionExample.java62
-rw-r--r--examples/src/main/java/org/apache/spark/examples/mllib/JavaIsotonicRegressionExample.java19
-rw-r--r--examples/src/main/python/ml/isotonic_regression_example.py54
-rw-r--r--examples/src/main/python/mllib/isotonic_regression_example.py11
-rw-r--r--examples/src/main/scala/org/apache/spark/examples/ml/IsotonicRegressionExample.scala62
-rw-r--r--examples/src/main/scala/org/apache/spark/examples/mllib/IsotonicRegressionExample.scala9
6 files changed, 203 insertions, 14 deletions
diff --git a/examples/src/main/java/org/apache/spark/examples/ml/JavaIsotonicRegressionExample.java b/examples/src/main/java/org/apache/spark/examples/ml/JavaIsotonicRegressionExample.java
new file mode 100644
index 0000000000..0ec17b0471
--- /dev/null
+++ b/examples/src/main/java/org/apache/spark/examples/ml/JavaIsotonicRegressionExample.java
@@ -0,0 +1,62 @@
+/*
+ * 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 org.apache.spark.ml.regression.IsotonicRegression;
+import org.apache.spark.ml.regression.IsotonicRegressionModel;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+// $example off$
+import org.apache.spark.sql.SparkSession;
+
+/**
+ * An example demonstrating IsotonicRegression.
+ * Run with
+ * <pre>
+ * bin/run-example ml.JavaIsotonicRegressionExample
+ * </pre>
+ */
+public class JavaIsotonicRegressionExample {
+
+ public static void main(String[] args) {
+ // Create a SparkSession.
+ SparkSession spark = SparkSession
+ .builder()
+ .appName("JavaIsotonicRegressionExample")
+ .getOrCreate();
+
+ // $example on$
+ // Loads data.
+ Dataset<Row> dataset = spark.read().format("libsvm")
+ .load("data/mllib/sample_isotonic_regression_libsvm_data.txt");
+
+ // Trains an isotonic regression model.
+ IsotonicRegression ir = new IsotonicRegression();
+ IsotonicRegressionModel model = ir.fit(dataset);
+
+ System.out.println("Boundaries in increasing order: " + model.boundaries());
+ System.out.println("Predictions associated with the boundaries: " + model.predictions());
+
+ // Makes predictions.
+ model.transform(dataset).show();
+ // $example off$
+
+ spark.stop();
+ }
+}
diff --git a/examples/src/main/java/org/apache/spark/examples/mllib/JavaIsotonicRegressionExample.java b/examples/src/main/java/org/apache/spark/examples/mllib/JavaIsotonicRegressionExample.java
index c6361a3729..a30b5f1f73 100644
--- a/examples/src/main/java/org/apache/spark/examples/mllib/JavaIsotonicRegressionExample.java
+++ b/examples/src/main/java/org/apache/spark/examples/mllib/JavaIsotonicRegressionExample.java
@@ -17,6 +17,7 @@
package org.apache.spark.examples.mllib;
// $example on$
+
import scala.Tuple2;
import scala.Tuple3;
import org.apache.spark.api.java.function.Function;
@@ -27,6 +28,8 @@ import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.mllib.regression.IsotonicRegression;
import org.apache.spark.mllib.regression.IsotonicRegressionModel;
+import org.apache.spark.mllib.regression.LabeledPoint;
+import org.apache.spark.mllib.util.MLUtils;
// $example off$
import org.apache.spark.SparkConf;
@@ -35,27 +38,29 @@ public class JavaIsotonicRegressionExample {
SparkConf sparkConf = new SparkConf().setAppName("JavaIsotonicRegressionExample");
JavaSparkContext jsc = new JavaSparkContext(sparkConf);
// $example on$
- JavaRDD<String> data = jsc.textFile("data/mllib/sample_isotonic_regression_data.txt");
+ JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(
+ jsc.sc(), "data/mllib/sample_isotonic_regression_libsvm_data.txt").toJavaRDD();
// Create label, feature, weight tuples from input data with weight set to default value 1.0.
JavaRDD<Tuple3<Double, Double, Double>> parsedData = data.map(
- new Function<String, Tuple3<Double, Double, Double>>() {
- public Tuple3<Double, Double, Double> call(String line) {
- String[] parts = line.split(",");
- return new Tuple3<>(new Double(parts[0]), new Double(parts[1]), 1.0);
+ new Function<LabeledPoint, Tuple3<Double, Double, Double>>() {
+ public Tuple3<Double, Double, Double> call(LabeledPoint point) {
+ return new Tuple3<>(new Double(point.label()),
+ new Double(point.features().apply(0)), 1.0);
}
}
);
// Split data into training (60%) and test (40%) sets.
JavaRDD<Tuple3<Double, Double, Double>>[] splits =
- parsedData.randomSplit(new double[]{0.6, 0.4}, 11L);
+ parsedData.randomSplit(new double[]{0.6, 0.4}, 11L);
JavaRDD<Tuple3<Double, Double, Double>> training = splits[0];
JavaRDD<Tuple3<Double, Double, Double>> test = splits[1];
// Create isotonic regression model from training data.
// Isotonic parameter defaults to true so it is only shown for demonstration
- final IsotonicRegressionModel model = new IsotonicRegression().setIsotonic(true).run(training);
+ final IsotonicRegressionModel model =
+ new IsotonicRegression().setIsotonic(true).run(training);
// Create tuples of predicted and real labels.
JavaPairRDD<Double, Double> predictionAndLabel = test.mapToPair(
diff --git a/examples/src/main/python/ml/isotonic_regression_example.py b/examples/src/main/python/ml/isotonic_regression_example.py
new file mode 100644
index 0000000000..1e61bd8eff
--- /dev/null
+++ b/examples/src/main/python/ml/isotonic_regression_example.py
@@ -0,0 +1,54 @@
+#
+# 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.
+#
+
+"""
+Isotonic Regression Example.
+"""
+from __future__ import print_function
+
+# $example on$
+from pyspark.ml.regression import IsotonicRegression, IsotonicRegressionModel
+# $example off$
+from pyspark.sql import SparkSession
+
+"""
+An example demonstrating isotonic regression.
+Run with:
+ bin/spark-submit examples/src/main/python/ml/isotonic_regression_example.py
+"""
+if __name__ == "__main__":
+
+ spark = SparkSession\
+ .builder\
+ .appName("PythonIsotonicRegressionExample")\
+ .getOrCreate()
+
+ # $example on$
+ # Loads data.
+ dataset = spark.read.format("libsvm")\
+ .load("data/mllib/sample_isotonic_regression_libsvm_data.txt")
+
+ # Trains an isotonic regression model.
+ model = IsotonicRegression().fit(dataset)
+ print("Boundaries in increasing order: " + str(model.boundaries))
+ print("Predictions associated with the boundaries: " + str(model.predictions))
+
+ # Makes predictions.
+ model.transform(dataset).show()
+ # $example off$
+
+ spark.stop()
diff --git a/examples/src/main/python/mllib/isotonic_regression_example.py b/examples/src/main/python/mllib/isotonic_regression_example.py
index 89dc9f4b66..33d618ab48 100644
--- a/examples/src/main/python/mllib/isotonic_regression_example.py
+++ b/examples/src/main/python/mllib/isotonic_regression_example.py
@@ -23,7 +23,8 @@ from __future__ import print_function
from pyspark import SparkContext
# $example on$
import math
-from pyspark.mllib.regression import IsotonicRegression, IsotonicRegressionModel
+from pyspark.mllib.regression import LabeledPoint, IsotonicRegression, IsotonicRegressionModel
+from pyspark.mllib.util import MLUtils
# $example off$
if __name__ == "__main__":
@@ -31,10 +32,14 @@ if __name__ == "__main__":
sc = SparkContext(appName="PythonIsotonicRegressionExample")
# $example on$
- data = sc.textFile("data/mllib/sample_isotonic_regression_data.txt")
+ # Load and parse the data
+ def parsePoint(labeledData):
+ return (labeledData.label, labeledData.features[0], 1.0)
+
+ data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_isotonic_regression_libsvm_data.txt")
# Create label, feature, weight tuples from input data with weight set to default value 1.0.
- parsedData = data.map(lambda line: tuple([float(x) for x in line.split(',')]) + (1.0,))
+ parsedData = data.map(parsePoint)
# Split data into training (60%) and test (40%) sets.
training, test = parsedData.randomSplit([0.6, 0.4], 11)
diff --git a/examples/src/main/scala/org/apache/spark/examples/ml/IsotonicRegressionExample.scala b/examples/src/main/scala/org/apache/spark/examples/ml/IsotonicRegressionExample.scala
new file mode 100644
index 0000000000..7c5d3f2341
--- /dev/null
+++ b/examples/src/main/scala/org/apache/spark/examples/ml/IsotonicRegressionExample.scala
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+
+// scalastyle:off println
+package org.apache.spark.examples.ml
+
+// $example on$
+import org.apache.spark.ml.regression.IsotonicRegression
+// $example off$
+import org.apache.spark.sql.SparkSession
+
+/**
+ * An example demonstrating Isotonic Regression.
+ * Run with
+ * {{{
+ * bin/run-example ml.IsotonicRegressionExample
+ * }}}
+ */
+object IsotonicRegressionExample {
+
+ def main(args: Array[String]): Unit = {
+
+ // Creates a SparkSession.
+ val spark = SparkSession
+ .builder
+ .appName(s"${this.getClass.getSimpleName}")
+ .getOrCreate()
+
+ // $example on$
+ // Loads data.
+ val dataset = spark.read.format("libsvm")
+ .load("data/mllib/sample_isotonic_regression_libsvm_data.txt")
+
+ // Trains an isotonic regression model.
+ val ir = new IsotonicRegression()
+ val model = ir.fit(dataset)
+
+ println(s"Boundaries in increasing order: ${model.boundaries}")
+ println(s"Predictions associated with the boundaries: ${model.predictions}")
+
+ // Makes predictions.
+ model.transform(dataset).show()
+ // $example off$
+
+ spark.stop()
+ }
+}
+// scalastyle:on println
diff --git a/examples/src/main/scala/org/apache/spark/examples/mllib/IsotonicRegressionExample.scala b/examples/src/main/scala/org/apache/spark/examples/mllib/IsotonicRegressionExample.scala
index c4336639d7..e5dea129c1 100644
--- a/examples/src/main/scala/org/apache/spark/examples/mllib/IsotonicRegressionExample.scala
+++ b/examples/src/main/scala/org/apache/spark/examples/mllib/IsotonicRegressionExample.scala
@@ -21,6 +21,7 @@ package org.apache.spark.examples.mllib
import org.apache.spark.{SparkConf, SparkContext}
// $example on$
import org.apache.spark.mllib.regression.{IsotonicRegression, IsotonicRegressionModel}
+import org.apache.spark.mllib.util.MLUtils
// $example off$
object IsotonicRegressionExample {
@@ -30,12 +31,12 @@ object IsotonicRegressionExample {
val conf = new SparkConf().setAppName("IsotonicRegressionExample")
val sc = new SparkContext(conf)
// $example on$
- val data = sc.textFile("data/mllib/sample_isotonic_regression_data.txt")
+ val data = MLUtils.loadLibSVMFile(sc,
+ "data/mllib/sample_isotonic_regression_libsvm_data.txt").cache()
// Create label, feature, weight tuples from input data with weight set to default value 1.0.
- val parsedData = data.map { line =>
- val parts = line.split(',').map(_.toDouble)
- (parts(0), parts(1), 1.0)
+ val parsedData = data.map { labeledPoint =>
+ (labeledPoint.label, labeledPoint.features(0), 1.0)
}
// Split data into training (60%) and test (40%) sets.