Github user feynmanliang commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7266#discussion_r34204476
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/feature/StringIndexerSuite.scala ---
    @@ -49,6 +50,38 @@ class StringIndexerSuite extends SparkFunSuite with 
MLlibTestSparkContext {
         assert(output === expected)
       }
     
    +  test("StringIndexerUnessen") {
    +    val data = sc.parallelize(Seq((0, "a"), (1, "b")), 2)
    +    val data2 = sc.parallelize(Seq((0, "a"), (1, "b"), (2, "c")), 2)
    +    val df = sqlContext.createDataFrame(data).toDF("id", "label")
    +    val df2 = sqlContext.createDataFrame(data2).toDF("id", "label")
    +    val indexer = new StringIndexer()
    +      .setInputCol("label")
    +      .setOutputCol("labelIndex")
    +      .fit(df)
    +    // Verify we throw by default with unseen values
    +    intercept[SparkException] {
    +      indexer.transform(df2).collect()
    +    }
    +    val indexerSkipInvalid = new StringIndexer()
    +      .setInputCol("label")
    +      .setOutputCol("labelIndex")
    +      .setSkipInvalid(true)
    +      .fit(df)
    +    // Verify that we skip the c record
    +    val transformed = indexerSkipInvalid.transform(df2)
    +    val attr = Attribute.fromStructField(transformed.schema("labelIndex"))
    +      .asInstanceOf[NominalAttribute]
    +    assert(attr.values.get === Array("b", "a"))
    +    val output = transformed.select("id", "labelIndex").map { r =>
    +      (r.getInt(0), r.getDouble(1))
    +    }.collect().toSet
    +    // a -> 1, b -> 0
    +    val expected = Set((0, 1.0), (1, 0.0))
    +    assert(output === expected)
    +  }
    +
    +
    --- End diff --
    
    remove newline


---
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]

Reply via email to