aboutsummaryrefslogtreecommitdiff
path: root/mllib
diff options
context:
space:
mode:
Diffstat (limited to 'mllib')
-rw-r--r--mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala78
-rw-r--r--mllib/src/main/scala/org/apache/spark/mllib/tree/configuration/Strategy.scala3
-rw-r--r--mllib/src/test/scala/org/apache/spark/mllib/tree/DecisionTreeSuite.scala3
3 files changed, 82 insertions, 2 deletions
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala b/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala
index 7d912737b8..1d5d3762ed 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala
@@ -19,6 +19,8 @@ package org.apache.spark.mllib.api.python
import java.nio.{ByteBuffer, ByteOrder}
+import scala.collection.JavaConverters._
+
import org.apache.spark.annotation.DeveloperApi
import org.apache.spark.api.java.{JavaRDD, JavaSparkContext}
import org.apache.spark.mllib.classification._
@@ -29,6 +31,11 @@ import org.apache.spark.mllib.linalg.{Matrix, SparseVector, Vector, Vectors}
import org.apache.spark.mllib.random.{RandomRDDGenerators => RG}
import org.apache.spark.mllib.recommendation._
import org.apache.spark.mllib.regression._
+import org.apache.spark.mllib.tree.configuration.Algo._
+import org.apache.spark.mllib.tree.configuration.Strategy
+import org.apache.spark.mllib.tree.DecisionTree
+import org.apache.spark.mllib.tree.impurity.{Entropy, Gini, Impurity, Variance}
+import org.apache.spark.mllib.tree.model.DecisionTreeModel
import org.apache.spark.mllib.stat.Statistics
import org.apache.spark.mllib.stat.correlation.CorrelationNames
import org.apache.spark.mllib.util.MLUtils
@@ -473,6 +480,76 @@ class PythonMLLibAPI extends Serializable {
}
/**
+ * Java stub for Python mllib DecisionTree.train().
+ * This stub returns a handle to the Java object instead of the content of the Java object.
+ * Extra care needs to be taken in the Python code to ensure it gets freed on exit;
+ * see the Py4J documentation.
+ * @param dataBytesJRDD Training data
+ * @param categoricalFeaturesInfoJMap Categorical features info, as Java map
+ */
+ def trainDecisionTreeModel(
+ dataBytesJRDD: JavaRDD[Array[Byte]],
+ algoStr: String,
+ numClasses: Int,
+ categoricalFeaturesInfoJMap: java.util.Map[Int, Int],
+ impurityStr: String,
+ maxDepth: Int,
+ maxBins: Int): DecisionTreeModel = {
+
+ val data = dataBytesJRDD.rdd.map(deserializeLabeledPoint)
+
+ val algo: Algo = algoStr match {
+ case "classification" => Classification
+ case "regression" => Regression
+ case _ => throw new IllegalArgumentException(s"Bad algoStr parameter: $algoStr")
+ }
+ val impurity: Impurity = impurityStr match {
+ case "gini" => Gini
+ case "entropy" => Entropy
+ case "variance" => Variance
+ case _ => throw new IllegalArgumentException(s"Bad impurityStr parameter: $impurityStr")
+ }
+
+ val strategy = new Strategy(
+ algo = algo,
+ impurity = impurity,
+ maxDepth = maxDepth,
+ numClassesForClassification = numClasses,
+ maxBins = maxBins,
+ categoricalFeaturesInfo = categoricalFeaturesInfoJMap.asScala.toMap)
+
+ DecisionTree.train(data, strategy)
+ }
+
+ /**
+ * Predict the label of the given data point.
+ * This is a Java stub for python DecisionTreeModel.predict()
+ *
+ * @param featuresBytes Serialized feature vector for data point
+ * @return predicted label
+ */
+ def predictDecisionTreeModel(
+ model: DecisionTreeModel,
+ featuresBytes: Array[Byte]): Double = {
+ val features: Vector = deserializeDoubleVector(featuresBytes)
+ model.predict(features)
+ }
+
+ /**
+ * Predict the labels of the given data points.
+ * This is a Java stub for python DecisionTreeModel.predict()
+ *
+ * @param dataJRDD A JavaRDD with serialized feature vectors
+ * @return JavaRDD of serialized predictions
+ */
+ def predictDecisionTreeModel(
+ model: DecisionTreeModel,
+ dataJRDD: JavaRDD[Array[Byte]]): JavaRDD[Array[Byte]] = {
+ val data = dataJRDD.rdd.map(xBytes => deserializeDoubleVector(xBytes))
+ model.predict(data).map(serializeDouble)
+ }
+
+ /**
* Java stub for mllib Statistics.corr(X: RDD[Vector], method: String).
* Returns the correlation matrix serialized into a byte array understood by deserializers in
* pyspark.
@@ -597,4 +674,5 @@ class PythonMLLibAPI extends Serializable {
val s = getSeedOrDefault(seed)
RG.poissonVectorRDD(jsc.sc, mean, numRows, numCols, parts, s).map(serializeDoubleVector)
}
+
}
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/tree/configuration/Strategy.scala b/mllib/src/main/scala/org/apache/spark/mllib/tree/configuration/Strategy.scala
index 5c65b537b6..fdad4f029a 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/tree/configuration/Strategy.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/tree/configuration/Strategy.scala
@@ -56,7 +56,8 @@ class Strategy (
if (algo == Classification) {
require(numClassesForClassification >= 2)
}
- val isMulticlassClassification = numClassesForClassification > 2
+ val isMulticlassClassification =
+ algo == Classification && numClassesForClassification > 2
val isMulticlassWithCategoricalFeatures
= isMulticlassClassification && (categoricalFeaturesInfo.size > 0)
diff --git a/mllib/src/test/scala/org/apache/spark/mllib/tree/DecisionTreeSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/tree/DecisionTreeSuite.scala
index 546a132559..8665a00f3b 100644
--- a/mllib/src/test/scala/org/apache/spark/mllib/tree/DecisionTreeSuite.scala
+++ b/mllib/src/test/scala/org/apache/spark/mllib/tree/DecisionTreeSuite.scala
@@ -48,7 +48,8 @@ class DecisionTreeSuite extends FunSuite with LocalSparkContext {
requiredMSE: Double) {
val predictions = input.map(x => model.predict(x.features))
val squaredError = predictions.zip(input).map { case (prediction, expected) =>
- (prediction - expected.label) * (prediction - expected.label)
+ val err = prediction - expected.label
+ err * err
}.sum
val mse = squaredError / input.length
assert(mse <= requiredMSE)