Github user yanboliang commented on a diff in the pull request:
https://github.com/apache/spark/pull/18613#discussion_r127260481
--- Diff:
mllib/src/test/scala/org/apache/spark/ml/feature/RFormulaSuite.scala ---
@@ -501,4 +501,51 @@ class RFormulaSuite extends SparkFunSuite with
MLlibTestSparkContext with Defaul
assert(expected.resolvedFormula.hasIntercept ===
actual.resolvedFormula.hasIntercept)
}
}
+
+ test("handle unseen features or labels") {
+ val df1 = Seq((1, "foo", "zq"), (2, "bar", "zq"), (3, "bar",
"zz")).toDF("id", "a", "b")
+ val df2 = Seq((1, "foo", "zq"), (2, "bar", "zq"), (3, "bar",
"zy")).toDF("id", "a", "b")
+
+ // Handle unseen features.
+ val formula1 = new RFormula().setFormula("id ~ a + b")
+ intercept[SparkException] {
+ formula1.fit(df1).transform(df2).collect()
+ }
+ val result1 = formula1.setHandleInvalid("skip").fit(df1).transform(df2)
+ val result2 = formula1.setHandleInvalid("keep").fit(df1).transform(df2)
+
+ val expected1 = Seq(
+ (1, "foo", "zq", Vectors.dense(0.0, 1.0), 1.0),
+ (2, "bar", "zq", Vectors.dense(1.0, 1.0), 2.0)
+ ).toDF("id", "a", "b", "features", "label")
+ val expected2 = Seq(
+ (1, "foo", "zq", Vectors.dense(0.0, 1.0, 1.0, 0.0), 1.0),
+ (2, "bar", "zq", Vectors.dense(1.0, 0.0, 1.0, 0.0), 2.0),
+ (3, "bar", "zy", Vectors.dense(1.0, 0.0, 0.0, 0.0), 3.0)
+ ).toDF("id", "a", "b", "features", "label")
+
+ assert(result1.collect() === expected1.collect())
+ assert(result2.collect() === expected2.collect())
+
+ // Handle unseen labels.
--- End diff --
The following test cases is failed before this PR.
---
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]