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

    https://github.com/apache/spark/pull/18902#discussion_r133649896
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/feature/Imputer.scala ---
    @@ -133,23 +133,45 @@ class Imputer @Since("2.2.0") (@Since("2.2.0") 
override val uid: String)
       override def fit(dataset: Dataset[_]): ImputerModel = {
         transformSchema(dataset.schema, logging = true)
         val spark = dataset.sparkSession
    -    import spark.implicits._
    -    val surrogates = $(inputCols).map { inputCol =>
    -      val ic = col(inputCol)
    -      val filtered = dataset.select(ic.cast(DoubleType))
    -        .filter(ic.isNotNull && ic =!= $(missingValue) && !ic.isNaN)
    -      if(filtered.take(1).length == 0) {
    -        throw new SparkException(s"surrogate cannot be computed. " +
    -          s"All the values in $inputCol are Null, Nan or 
missingValue(${$(missingValue)})")
    -      }
    -      val surrogate = $(strategy) match {
    -        case Imputer.mean => 
filtered.select(avg(inputCol)).as[Double].first()
    -        case Imputer.median => filtered.stat.approxQuantile(inputCol, 
Array(0.5), 0.001).head
    -      }
    -      surrogate
    +
    +    val cols = $(inputCols).map { inputCol =>
    +      when(col(inputCol).equalTo($(missingValue)), null)
    +        .when(col(inputCol).isNaN, null)
    +        .otherwise(col(inputCol))
    +        .cast("double")
    +        .as(inputCol)
    +    }
    +
    +    val results = $(strategy) match {
    +      case Imputer.mean =>
    +        val row = dataset.select(cols.map(avg): _*).head()
    +        Array.range(0, $(inputCols).length).map { i =>
    +          if (row.isNullAt(i)) {
    +            Double.NaN
    +          } else {
    +            row.getDouble(i)
    +          }
    +        }
    +
    +      case Imputer.median =>
    +        dataset.select(cols: _*).stat.approxQuantile($(inputCols), 
Array(0.5), 0.001)
    --- End diff --
    
    BTW, add annotation here to clarify _approxQuantile_ function will ignore 
_null_ automatically.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to