Github user jkbradley commented on a diff in the pull request:
https://github.com/apache/spark/pull/10355#discussion_r57643358
--- Diff:
mllib/src/test/scala/org/apache/spark/ml/util/MLTestingUtils.scala ---
@@ -17,14 +17,116 @@
package org.apache.spark.ml.util
-import org.apache.spark.ml.Model
+import org.apache.spark.SparkFunSuite
+import org.apache.spark.ml.{Estimator, Model, PredictionModel, Predictor}
import org.apache.spark.ml.param.ParamMap
+import org.apache.spark.ml.regression.Regressor
+import org.apache.spark.ml.tree.impl.TreeTests
+import org.apache.spark.mllib.linalg.Vectors
+import org.apache.spark.sql.{DataFrame, SQLContext}
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types._
-object MLTestingUtils {
+object MLTestingUtils extends SparkFunSuite {
def checkCopy(model: Model[_]): Unit = {
val copied = model.copy(ParamMap.empty)
.asInstanceOf[Model[_]]
assert(copied.parent.uid == model.parent.uid)
assert(copied.parent == model.parent)
}
+
+ def checkPredictorAcceptAllNumericTypes[M <: PredictionModel[_, M], T <:
Predictor[_, _, M]](
+ predictor: T, sqlContext: SQLContext)(check: (M, M) => Unit): Unit =
{
+ val dfs = if (predictor.isInstanceOf[Regressor[_, _, _]]) {
+ genRegressionDFWithNumericLabelCol(sqlContext)
+ } else {
+ genClassifDFWithNumericLabelCol(sqlContext)
+ }
+ val expected = predictor.fit(dfs(DoubleType))
+ val actuals = dfs.keys.filter(_ != DoubleType).map(t =>
predictor.fit(dfs(t)))
+ actuals.foreach(actual => check(expected, actual))
+ }
+
+ def checkPredictorRejectNotNumericTypes(
+ predictor: Predictor[_, _, _], sqlContext: SQLContext): Unit = {
+ val dfWithStringLabels = generateDFWithStringLabelCol(sqlContext)
+ val thrown = intercept[IllegalArgumentException] {
+ predictor.fit(dfWithStringLabels)
+ }
+ assert(thrown.getMessage contains
+ "Column label must be of type NumericType but was actually of type
StringType")
+ }
+
+ def checkEstimatorAcceptAllNumericTypes[M <: Model[M], T <:
Estimator[M]](
+ estimator: T, sqlContext: SQLContext)(check: (M, M) => Unit): Unit =
{
+ val dfs = genRegressionDFWithNumericLabelCol(sqlContext)
+ val expected = estimator.fit(dfs(DoubleType))
+ val actuals = dfs.keys.filter(_ != DoubleType).map(t =>
estimator.fit(dfs(t)))
+ actuals.foreach(actual => check(expected, actual))
+ }
+
+ def checkEstimatorRejectNotNumericTypes(
+ predictor: Estimator[_], sqlContext: SQLContext): Unit = {
+ val dfWithStringLabels = generateDFWithStringLabelCol(sqlContext)
+ val thrown = intercept[IllegalArgumentException] {
+ predictor.fit(dfWithStringLabels)
+ }
+ assert(thrown.getMessage contains
+ "Column label must be of type NumericType but was actually of type
StringType")
+ }
+
+ def genClassifDFWithNumericLabelCol(
+ sqlContext: SQLContext,
+ labelColName: String = "label",
+ featuresColName: String = "features"): Map[NumericType, DataFrame] =
{
+ val df = sqlContext.createDataFrame(Seq(
+ (0, Vectors.dense(0, 2, 3)),
+ (1, Vectors.dense(0, 3, 1)),
+ (0, Vectors.dense(0, 2, 2)),
+ (1, Vectors.dense(0, 3, 9)),
+ (0, Vectors.dense(0, 2, 6))
+ )).toDF(labelColName, featuresColName)
+
+ val types =
+ Seq(ShortType, LongType, IntegerType, FloatType, ByteType,
DoubleType, DecimalType(10, 0))
+ types.map(t => t -> df.select(col(labelColName).cast(t),
col(featuresColName)))
+ .map { case (t, d) => t -> TreeTests.setMetadata(d, 2, labelColName)
}
+ .toMap
+ }
+
+ def genRegressionDFWithNumericLabelCol(
+ sqlContext: SQLContext,
+ labelColName: String = "label",
+ featuresColName: String = "features",
+ censorColName: String = "censor"): Map[NumericType, DataFrame] = {
+ val df = sqlContext.createDataFrame(Seq(
+ (0, Vectors.dense(0)),
+ (1, Vectors.dense(1)),
+ (2, Vectors.dense(2)),
+ (3, Vectors.dense(3)),
+ (4, Vectors.dense(4))
+ )).toDF(labelColName, featuresColName)
+
+ val types =
+ Seq(ShortType, LongType, IntegerType, FloatType, ByteType,
DoubleType, DecimalType(10, 0))
+ types
+ .map(t => t -> df.select(col(labelColName).cast(t),
col(featuresColName)))
+ .map { case (t, d) =>
+ t -> TreeTests.setMetadata(d, 2,
labelColName).withColumn(censorColName, lit(0.0))
--- End diff --
setMetadata: classes should be 0, not 2
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]