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

    https://github.com/apache/spark/pull/5830#discussion_r30092209
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/reduction/OneVsRest.scala ---
    @@ -0,0 +1,194 @@
    +/*
    + * 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.reduction
    +
    +import java.util.UUID
    +
    +import scala.language.existentials
    +
    +import org.apache.spark.annotation.{AlphaComponent, Experimental}
    +import org.apache.spark.ml._
    +import org.apache.spark.ml.attribute.BinaryAttribute
    +import org.apache.spark.ml.classification.{ClassificationModel, Classifier}
    +import org.apache.spark.ml.param.Param
    +import org.apache.spark.ml.util.MetadataUtils
    +import org.apache.spark.mllib.linalg.Vector
    +import org.apache.spark.sql.{DataFrame, Row}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.types._
    +import org.apache.spark.storage.StorageLevel
    +
    +/**
    + * Params for [[OneVsRest]].
    + */
    +private[ml] trait OneVsRestParams extends PredictorParams {
    +
    +  type ClassifierType = Classifier[F, E, M] forSome {
    +    type F ;
    +    type M <: ClassificationModel[F,M];
    +    type E <:  Classifier[F, E,M]
    +  }
    +
    +  /**
    +   * param for the base classifier that we reduce multiclass 
classification into.
    +   * @group param
    +   */
    +  val classifier: Param[ClassifierType]  =
    +    new Param(this, "classifier", "base binary classifier ")
    +
    +  /** @group getParam */
    +  def getClassifier: ClassifierType = $(classifier)
    +
    +}
    +
    +/**
    + * Model produced by [[OneVsRest]].
    + *
    + * @param parent
    + * @param models the binary classification models for reduction.
    + */
    +@AlphaComponent
    +class OneVsRestModel(
    +      override val parent: OneVsRest,
    +      val models: Array[_ <: ClassificationModel[_,_]])
    +  extends Model[OneVsRestModel] with OneVsRestParams {
    +
    +  override def transformSchema(schema: StructType): StructType = {
    +    validateAndTransformSchema(schema, fitting = false, 
getClassifier.featuresDataType)
    +  }
    +
    +  override def transform(dataset: DataFrame): DataFrame = {
    +    // Check schema
    +    transformSchema(dataset.schema, logging = true)
    +
    +    // determine the input columns: these need to be passed through
    +    val origCols = dataset.schema.map(f => col(f.name))
    +
    +    // add an accumulator column to store predictions of all the models
    +    val accColName = "mbc$acc" + UUID.randomUUID().toString
    +    val init: () => Map[Int, Double] = () => {Map()}
    +    val mapType = MapType(IntegerType, DoubleType, false)
    +    val newDataset = dataset.withColumn(accColName,callUDF(init, mapType))
    +
    +    // persist if underlying dataset is not persistent.
    +    val handlePersistence = dataset.rdd.getStorageLevel == 
StorageLevel.NONE
    +    if (handlePersistence) {
    +      newDataset.persist(StorageLevel.MEMORY_AND_DISK)
    +    }
    +
    +    // update the accumulator column with the result of prediction of 
models
    +    val aggregatedDataset = 
models.zipWithIndex.foldLeft[DataFrame](newDataset) {
    +      case (df, (model, index)) => {
    +        val rawPredictionCol = model.getRawPredictionCol
    +        val columns = origCols ++ List(col(rawPredictionCol), 
col(accColName))
    +
    +        // add temporary column to store intermediate scores and update
    +        val tmpColName = "mbc$tmp" + UUID.randomUUID().toString
    +        val update: (Map[Int, Double], Vector) => Map[Int, Double]  =
    +          (predictions: Map[Int, Double], prediction: Vector) => {
    +            predictions + ((index, prediction(1)))
    +        }
    +        val updateUdf = callUDF(update, mapType, col(accColName), 
col(rawPredictionCol))
    +        val transformedDataset = model.transform(df).select(columns:_*)
    +        val updatedDataset = transformedDataset.withColumn(tmpColName, 
updateUdf)
    +        val newColumns = origCols ++ List(col(tmpColName))
    +
    +        // switch out the intermediate column with the accumulator column
    +        updatedDataset.select(newColumns:_*).withColumnRenamed(tmpColName, 
accColName)
    +      }
    +    }
    +
    +    if (handlePersistence) {
    +      newDataset.unpersist()
    +    }
    +
    +    // output the index of the classifier with highest confidence as 
prediction
    +    val label: Map[Int, Double] => Double = (predictions: Map[Int, 
Double]) => {
    +      predictions.maxBy(_._2)._1.toDouble
    +    }
    +    aggregatedDataset.withColumn($(predictionCol), callUDF(label, 
DoubleType, col(accColName)))
    --- End diff --
    
    It would be nice if this output predictionCol metadata, created from the 
labelCol metadata if available, or set to NominalAttribute with the correct 
numValues otherwise.


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