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

    https://github.com/apache/spark/pull/6339#discussion_r35581918
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/feature/StringIndexer.scala ---
    @@ -151,4 +167,64 @@ 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 invert should only be needed if you do something beyond simply
    +   * applying the original transformation.
    +   */
    +  def invert(): StringIndexerInvertModel = {
    +    new StringIndexerInvertModel(uid, labels)
    +      .setInputCol(getOutputCol)
    +      .setOutputCol(getInputCol)
    +  }
    +}
    +
    +class StringIndexerInvertModel private[ml] (
    +    override val uid: String,
    +    labels: Array[String]) extends Model[StringIndexerInvertModel] with 
StringIndexerBase {
    +
    +  /** @group setParam */
    +  def setInputCol(value: String): this.type = set(inputCol, value)
    +
    +  /** @group setParam */
    +  def setOutputCol(value: String): this.type = set(outputCol, value)
    +
    +
    +  private val indexToLabel: OpenHashMap[Double, String] = {
    +    val n = labels.length
    +    val map = new OpenHashMap[Double, String](n)
    +    var i = 0
    +    while (i < n) {
    +      map.update(i, labels(i))
    +      i += 1
    +    }
    +    map
    +  }
    +
    +  override def transformSchema(schema: StructType): StructType = {
    +    invertSchema(schema)
    +  }
    +
    +  override def transform(dataset: DataFrame): DataFrame = {
    +    val indexer = udf { index: Double =>
    +      if (indexToLabel.contains(index)) {
    +        indexToLabel(index)
    +      } else {
    +        throw new SparkException(s"Unseen index: $index ??")
    +      }
    +    }
    +    val outputColName = $(outputCol)
    +    val metadata = NominalAttribute.defaultAttr
    +      .withName(outputColName).withValues(labels).toMetadata()
    +    dataset.select(col("*"),
    +      indexer(dataset($(inputCol)).cast(StringType)).as(outputColName, 
metadata))
    +  }
    +
    +  override def copy(extra: ParamMap): StringIndexerInvertModel = {
    +    val copied = new StringIndexerInvertModel(uid, labels)
    +    copyValues(copied, extra)
    --- End diff --
    
    call setParent, if a parent is available


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