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

    https://github.com/apache/spark/pull/11601#discussion_r61365096
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/feature/ImputerSuite.scala ---
    @@ -0,0 +1,141 @@
    +/*
    + * 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.ml.feature
    +
    +import org.apache.spark.SparkFunSuite
    +import org.apache.spark.ml.util.{DefaultReadWriteTest}
    +import org.apache.spark.mllib.util.MLlibTestSparkContext
    +import org.apache.spark.mllib.util.TestingUtils._
    +import org.apache.spark.sql.Row
    +
    +class ImputerSuite extends SparkFunSuite with MLlibTestSparkContext with 
DefaultReadWriteTest {
    +
    +  test("Imputer for Double with default missing Value NaN") {
    +    val df = sqlContext.createDataFrame( Seq(
    +      (0, 1.0, 1.0, 1.0),
    +      (1, 1.0, 1.0, 1.0),
    +      (2, 3.0, 3.0, 3.0),
    +      (3, 4.0, 4.0, 4.0),
    +      (4, Double.NaN, 2.25, 1.0)
    +    )).toDF("id", "value", "exp_mean", "exp_median")
    +    Seq("mean", "median").foreach { strategy =>
    +      val imputer = new 
Imputer().setInputCol("value").setOutputCol("out").setStrategy(strategy)
    +      val model = imputer.fit(df)
    +      model.transform(df).select("exp_" + strategy, 
"out").collect().foreach {
    +       case Row(exp: Double, out: Double) =>
    +          assert(exp ~== out absTol 1e-5, s"Imputed values differ. 
Expected: $exp, actual: $out")
    +      }
    +    }
    +  }
    +
    +  test("Imputer for Double with missing Value -1.0") {
    +    val df = sqlContext.createDataFrame( Seq(
    +      (0, 1.0, 1.0, 1.0),
    +      (1, 1.0, 1.0, 1.0),
    +      (2, 3.0, 3.0, 3.0),
    +      (3, 4.0, 4.0, 4.0),
    +      (4, -1.0, 2.25, 1.0)
    +    )).toDF("id", "value", "exp_mean", "exp_median")
    +    Seq("mean", "median").foreach { strategy =>
    +      val imputer = new 
Imputer().setInputCol("value").setOutputCol("out").setStrategy(strategy)
    +        .setMissingValue(-1.0)
    +      val model = imputer.fit(df)
    +      model.transform(df).select("exp_" + strategy, 
"out").collect().foreach {
    +        case Row(exp: Double, out: Double) =>
    +          assert(exp ~== out absTol 1e-5, s"Impute($strategy) error. 
Expected: $exp, actual: $out")
    +      }
    +    }
    +  }
    +
    +  test("Imputer for Double with missing Value -1.0 and contains NaN") {
    +    val df = sqlContext.createDataFrame( Seq(
    +      (0, 1.0, 1.0, 1.0),
    +      (1, 3.0, 3.0, 3.0),
    +      (2, Double.NaN, Double.NaN, Double.NaN),
    +      (3, -1.0, 2.0, 3.0)
    --- End diff --
    
    Actually, it always choose the smaller one of the two middle values as I 
saw in some tests.
    In this test case, the median is computed from [1, 3, Double.NaN]. And 
Double.NaN is treated as it's greater than Double.MaValue.


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