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

    https://github.com/apache/spark/pull/6339#discussion_r35796647
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/feature/StringIndexer.scala ---
    @@ -151,4 +153,77 @@ class StringIndexerModel private[ml] (
         val copied = new StringIndexerModel(uid, labels)
         copyValues(copied, extra)
       }
    +
    +  /**
    +   * Return a model to perform the inverse transformation.
    +   * Note: By default we keep the original columns during this 
transformation, so the inverse
    +   * should only be used on new columns such as predicted labels.
    +   */
    +  def invert(inputCol: String, outputCol: String): 
StringIndexerInverseTransformer = {
    +    val labelsCol: String = $(this.outputCol)
    +    new StringIndexerInverseTransformer(labelsCol)
    +      .setInputCol(inputCol)
    +      .setOutputCol(outputCol)
    +  }
    +}
    +
    +/**
    + * :: Experimental ::
    + * Transform a provided column back to the original input types using the 
metadata on the
    + * labelsCol. Note: By default we keep the original columns during this 
transformation,
    + * so the inverse should only be used on new columns such as predicted 
labels.
    + */
    +@Experimental
    +class StringIndexerInverseTransformer private[ml] (
    +  override val uid: String,
    +  val labelsCol: String) extends Transformer
    +    with HasInputCol with HasOutputCol {
    +
    +  def this(labelsCol: String) = this(Identifiable.randomUID("strIdxInv"), 
labelsCol)
    +
    +  /** @group setParam */
    +  def setInputCol(value: String): this.type = set(inputCol, value)
    +
    +  /** @group setParam */
    +  def setOutputCol(value: String): this.type = set(outputCol, value)
    +
    +  /** Transform the schema for the inverse transformation */
    +  override def transformSchema(schema: StructType): StructType = {
    +    val inputColName = $(inputCol)
    +    val inputDataType = schema(inputColName).dataType
    +    require(inputDataType.isInstanceOf[NumericType],
    +      s"The input column $inputColName must be a numeric type, " +
    +        s"but got $inputDataType.")
    +    val inputFields = schema.fields
    +    val outputColName = $(outputCol)
    +    require(inputFields.forall(_.name != outputColName),
    +      s"Output column $outputColName already exists.")
    +    val attr = NominalAttribute.defaultAttr.withName($(outputCol))
    +    val outputFields = inputFields :+ attr.toStructField()
    +    StructType(outputFields)
    +  }
    +
    +  override def transform(dataset: DataFrame): DataFrame = {
    +    val inputColSchema = dataset.schema($(inputCol))
    +    val attr = Attribute.fromStructField(inputColSchema)
    +      .asInstanceOf[NominalAttribute]
    +    val values = attr.values.get
    +    val indexer = udf { index: Double =>
    --- End diff --
    
    Can all NumericTypes be cast to Double like this?  Either test this in a 
unit test, or just switch to supporting DoubleType only for now.


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