aboutsummaryrefslogtreecommitdiff
path: root/examples/src
diff options
context:
space:
mode:
authorsethah <seth.hendrickson16@gmail.com>2016-10-05 18:28:21 +0000
committerDB Tsai <dbtsai@dbtsai.com>2016-10-05 18:28:21 +0000
commit9df54f5325c2942bb77008ff1810e2fb5f6d848b (patch)
treeb178ea4d0033a44e37902426104586be1a2ddf56 /examples/src
parent6a05eb24d043aa93390f353850d56efa6124e063 (diff)
downloadspark-9df54f5325c2942bb77008ff1810e2fb5f6d848b.tar.gz
spark-9df54f5325c2942bb77008ff1810e2fb5f6d848b.tar.bz2
spark-9df54f5325c2942bb77008ff1810e2fb5f6d848b.zip
[SPARK-17239][ML][DOC] Update user guide for multiclass logistic regression
## What changes were proposed in this pull request? Updates user guide to reflect that LogisticRegression now supports multiclass. Also adds new examples to show multiclass training. ## How was this patch tested? Ran locally using spark-submit, run-example, and copy/paste from user guide into shells. Generated docs and verified correct output. Author: sethah <seth.hendrickson16@gmail.com> Closes #15349 from sethah/SPARK-17239.
Diffstat (limited to 'examples/src')
-rw-r--r--examples/src/main/java/org/apache/spark/examples/ml/JavaLogisticRegressionWithElasticNetExample.java14
-rw-r--r--examples/src/main/java/org/apache/spark/examples/ml/JavaMulticlassLogisticRegressionWithElasticNetExample.java55
-rw-r--r--examples/src/main/python/ml/logistic_regression_with_elastic_net.py10
-rw-r--r--examples/src/main/python/ml/multiclass_logistic_regression_with_elastic_net.py48
-rw-r--r--examples/src/main/scala/org/apache/spark/examples/ml/LogisticRegressionWithElasticNetExample.scala13
-rw-r--r--examples/src/main/scala/org/apache/spark/examples/ml/MulticlassLogisticRegressionWithElasticNetExample.scala57
6 files changed, 197 insertions, 0 deletions
diff --git a/examples/src/main/java/org/apache/spark/examples/ml/JavaLogisticRegressionWithElasticNetExample.java b/examples/src/main/java/org/apache/spark/examples/ml/JavaLogisticRegressionWithElasticNetExample.java
index 6101c79fb0..b8fb5972ea 100644
--- a/examples/src/main/java/org/apache/spark/examples/ml/JavaLogisticRegressionWithElasticNetExample.java
+++ b/examples/src/main/java/org/apache/spark/examples/ml/JavaLogisticRegressionWithElasticNetExample.java
@@ -48,6 +48,20 @@ public class JavaLogisticRegressionWithElasticNetExample {
// Print the coefficients and intercept for logistic regression
System.out.println("Coefficients: "
+ lrModel.coefficients() + " Intercept: " + lrModel.intercept());
+
+ // We can also use the multinomial family for binary classification
+ LogisticRegression mlr = new LogisticRegression()
+ .setMaxIter(10)
+ .setRegParam(0.3)
+ .setElasticNetParam(0.8)
+ .setFamily("multinomial");
+
+ // Fit the model
+ LogisticRegressionModel mlrModel = mlr.fit(training);
+
+ // Print the coefficients and intercepts for logistic regression with multinomial family
+ System.out.println("Multinomial coefficients: "
+ + lrModel.coefficientMatrix() + "\nMultinomial intercepts: " + mlrModel.interceptVector());
// $example off$
spark.stop();
diff --git a/examples/src/main/java/org/apache/spark/examples/ml/JavaMulticlassLogisticRegressionWithElasticNetExample.java b/examples/src/main/java/org/apache/spark/examples/ml/JavaMulticlassLogisticRegressionWithElasticNetExample.java
new file mode 100644
index 0000000000..da410cba2b
--- /dev/null
+++ b/examples/src/main/java/org/apache/spark/examples/ml/JavaMulticlassLogisticRegressionWithElasticNetExample.java
@@ -0,0 +1,55 @@
+/*
+ * 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.classification.LogisticRegression;
+import org.apache.spark.ml.classification.LogisticRegressionModel;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+// $example off$
+
+public class JavaMulticlassLogisticRegressionWithElasticNetExample {
+ public static void main(String[] args) {
+ SparkSession spark = SparkSession
+ .builder()
+ .appName("JavaMulticlassLogisticRegressionWithElasticNetExample")
+ .getOrCreate();
+
+ // $example on$
+ // Load training data
+ Dataset<Row> training = spark.read().format("libsvm")
+ .load("data/mllib/sample_multiclass_classification_data.txt");
+
+ LogisticRegression lr = new LogisticRegression()
+ .setMaxIter(10)
+ .setRegParam(0.3)
+ .setElasticNetParam(0.8);
+
+ // Fit the model
+ LogisticRegressionModel lrModel = lr.fit(training);
+
+ // Print the coefficients and intercept for multinomial logistic regression
+ System.out.println("Coefficients: \n"
+ + lrModel.coefficientMatrix() + " \nIntercept: " + lrModel.interceptVector());
+ // $example off$
+
+ spark.stop();
+ }
+}
diff --git a/examples/src/main/python/ml/logistic_regression_with_elastic_net.py b/examples/src/main/python/ml/logistic_regression_with_elastic_net.py
index 33d0689f75..d095fbd373 100644
--- a/examples/src/main/python/ml/logistic_regression_with_elastic_net.py
+++ b/examples/src/main/python/ml/logistic_regression_with_elastic_net.py
@@ -40,6 +40,16 @@ if __name__ == "__main__":
# Print the coefficients and intercept for logistic regression
print("Coefficients: " + str(lrModel.coefficients))
print("Intercept: " + str(lrModel.intercept))
+
+ # We can also use the multinomial family for binary classification
+ mlr = LogisticRegression(maxIter=10, regParam=0.3, elasticNetParam=0.8, family="multinomial")
+
+ # Fit the model
+ mlrModel = mlr.fit(training)
+
+ # Print the coefficients and intercepts for logistic regression with multinomial family
+ print("Multinomial coefficients: " + str(mlrModel.coefficientMatrix))
+ print("Multinomial intercepts: " + str(mlrModel.interceptVector))
# $example off$
spark.stop()
diff --git a/examples/src/main/python/ml/multiclass_logistic_regression_with_elastic_net.py b/examples/src/main/python/ml/multiclass_logistic_regression_with_elastic_net.py
new file mode 100644
index 0000000000..bb9cd82d6b
--- /dev/null
+++ b/examples/src/main/python/ml/multiclass_logistic_regression_with_elastic_net.py
@@ -0,0 +1,48 @@
+#
+# 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.
+#
+
+from __future__ import print_function
+
+# $example on$
+from pyspark.ml.classification import LogisticRegression
+# $example off$
+from pyspark.sql import SparkSession
+
+if __name__ == "__main__":
+ spark = SparkSession \
+ .builder \
+ .appName("MulticlassLogisticRegressionWithElasticNet") \
+ .getOrCreate()
+
+ # $example on$
+ # Load training data
+ training = spark \
+ .read \
+ .format("libsvm") \
+ .load("data/mllib/sample_multiclass_classification_data.txt")
+
+ lr = LogisticRegression(maxIter=10, regParam=0.3, elasticNetParam=0.8)
+
+ # Fit the model
+ lrModel = lr.fit(training)
+
+ # Print the coefficients and intercept for multinomial logistic regression
+ print("Coefficients: \n" + str(lrModel.coefficientMatrix))
+ print("Intercept: " + str(lrModel.interceptVector))
+ # $example off$
+
+ spark.stop()
diff --git a/examples/src/main/scala/org/apache/spark/examples/ml/LogisticRegressionWithElasticNetExample.scala b/examples/src/main/scala/org/apache/spark/examples/ml/LogisticRegressionWithElasticNetExample.scala
index 616263b8e9..1847104908 100644
--- a/examples/src/main/scala/org/apache/spark/examples/ml/LogisticRegressionWithElasticNetExample.scala
+++ b/examples/src/main/scala/org/apache/spark/examples/ml/LogisticRegressionWithElasticNetExample.scala
@@ -45,6 +45,19 @@ object LogisticRegressionWithElasticNetExample {
// Print the coefficients and intercept for logistic regression
println(s"Coefficients: ${lrModel.coefficients} Intercept: ${lrModel.intercept}")
+
+ // We can also use the multinomial family for binary classification
+ val mlr = new LogisticRegression()
+ .setMaxIter(10)
+ .setRegParam(0.3)
+ .setElasticNetParam(0.8)
+ .setFamily("multinomial")
+
+ val mlrModel = mlr.fit(training)
+
+ // Print the coefficients and intercepts for logistic regression with multinomial family
+ println(s"Multinomial coefficients: ${mlrModel.coefficientMatrix}")
+ println(s"Multinomial intercepts: ${mlrModel.interceptVector}")
// $example off$
spark.stop()
diff --git a/examples/src/main/scala/org/apache/spark/examples/ml/MulticlassLogisticRegressionWithElasticNetExample.scala b/examples/src/main/scala/org/apache/spark/examples/ml/MulticlassLogisticRegressionWithElasticNetExample.scala
new file mode 100644
index 0000000000..42f0ace7a3
--- /dev/null
+++ b/examples/src/main/scala/org/apache/spark/examples/ml/MulticlassLogisticRegressionWithElasticNetExample.scala
@@ -0,0 +1,57 @@
+/*
+ * 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.classification.LogisticRegression
+// $example off$
+import org.apache.spark.sql.SparkSession
+
+object MulticlassLogisticRegressionWithElasticNetExample {
+
+ def main(args: Array[String]): Unit = {
+ val spark = SparkSession
+ .builder
+ .appName("MulticlassLogisticRegressionWithElasticNetExample")
+ .getOrCreate()
+
+ // $example on$
+ // Load training data
+ val training = spark
+ .read
+ .format("libsvm")
+ .load("data/mllib/sample_multiclass_classification_data.txt")
+
+ val lr = new LogisticRegression()
+ .setMaxIter(10)
+ .setRegParam(0.3)
+ .setElasticNetParam(0.8)
+
+ // Fit the model
+ val lrModel = lr.fit(training)
+
+ // Print the coefficients and intercept for multinomial logistic regression
+ println(s"Coefficients: \n${lrModel.coefficientMatrix}")
+ println(s"Intercepts: ${lrModel.interceptVector}")
+ // $example off$
+
+ spark.stop()
+ }
+}
+// scalastyle:on println