srowen commented on a change in pull request #26064: [SPARK-23578][ML][PYSPARK] Binarizer support multi-column URL: https://github.com/apache/spark/pull/26064#discussion_r333737235
########## File path: mllib/src/main/scala/org/apache/spark/ml/feature/Binarizer.scala ########## @@ -69,66 +83,117 @@ final class Binarizer @Since("1.4.0") (@Since("1.4.0") override val uid: String) @Since("1.4.0") def setOutputCol(value: String): this.type = set(outputCol, value) + /** @group setParam */ + @Since("3.0.0") + def setInputCols(value: Array[String]): this.type = set(inputCols, value) + + /** @group setParam */ + @Since("3.0.0") + def setOutputCols(value: Array[String]): this.type = set(outputCols, value) + @Since("2.0.0") override def transform(dataset: Dataset[_]): DataFrame = { val outputSchema = transformSchema(dataset.schema, logging = true) - val schema = dataset.schema - val inputType = schema($(inputCol)).dataType - val td = $(threshold) - val metadata = outputSchema($(outputCol)).metadata - - val binarizerUDF = inputType match { - case DoubleType => - udf { in: Double => if (in > td) 1.0 else 0.0 } - - case _: VectorUDT if td >= 0 => - udf { vector: Vector => - val indices = ArrayBuilder.make[Int] - val values = ArrayBuilder.make[Double] - vector.foreachActive { (index, value) => - if (value > td) { - indices += index - values += 1.0 + + val (inputColNames, outputColNames, tds) = + if (isSet(inputCols)) { + if (isSet(thresholds)) { + ($(inputCols).toSeq, $(outputCols).toSeq, $(thresholds).toSeq) + } else { + ($(inputCols).toSeq, $(outputCols).toSeq, Seq.fill($(inputCols).length)($(threshold))) + } + } else { + (Seq($(inputCol)), Seq($(outputCol)), Seq($(threshold))) + } + + val ouputCols = inputColNames.zip(tds).map { case (inputColName, td) => + val binarizerUDF = dataset.schema(inputColName).dataType match { + case DoubleType => + udf { in: Double => if (in > td) 1.0 else 0.0 } + + case _: VectorUDT if td >= 0 => + udf { vector: Vector => + val indices = ArrayBuilder.make[Int] + val values = ArrayBuilder.make[Double] + vector.foreachActive { (index, value) => + if (value > td) { + indices += index + values += 1.0 + } } + Vectors.sparse(vector.size, indices.result(), values.result()).compressed } - Vectors.sparse(vector.size, indices.result(), values.result()).compressed - } - case _: VectorUDT if td < 0 => - this.logWarning(s"Binarization operations on sparse dataset with negative threshold " + - s"$td will build a dense output, so take care when applying to sparse input.") - udf { vector: Vector => - val values = Array.fill(vector.size)(1.0) - vector.foreachActive { (index, value) => - if (value <= td) { - values(index) = 0.0 + case _: VectorUDT if td < 0 => + this.logWarning(s"Binarization operations on sparse dataset with negative threshold " + + s"$td will build a dense output, so take care when applying to sparse input.") + udf { vector: Vector => + val values = Array.fill(vector.size)(1.0) Review comment: Nit, while we're here. Is it maybe faster to start with an array of 0, and flip the ones that exceed the threshold? just because the array is already initialized to 0. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org