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

    https://github.com/apache/spark/pull/8611#discussion_r39067652
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/regression/AFTRegression.scala ---
    @@ -0,0 +1,308 @@
    +/*
    + * 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.regression
    +
    +import scala.collection.mutable
    +
    +import breeze.linalg.{DenseVector => BDV}
    +import breeze.optimize.{CachedDiffFunction, DiffFunction, LBFGS => 
BreezeLBFGS}
    +
    +import org.apache.spark.{SparkException, Logging}
    +import org.apache.spark.annotation.Experimental
    +import org.apache.spark.ml.{Model, Estimator}
    +import org.apache.spark.ml.param._
    +import org.apache.spark.ml.param.shared._
    +import org.apache.spark.ml.util.{SchemaUtils, Identifiable}
    +import org.apache.spark.mllib.linalg.{DenseVector, Vector, Vectors, 
VectorUDT}
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.sql.{Row, DataFrame}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.types.{DoubleType, StructType}
    +import org.apache.spark.storage.StorageLevel
    +
    +/**
    + * Params for Accelerated Failure Time regression.
    + */
    +private[regression] trait AFTRegressionParams extends Params
    +  with HasFeaturesCol with HasLabelCol with HasPredictionCol with 
HasMaxIter
    +  with HasTol with HasFitIntercept {
    +
    +  /**
    +   * Param for censored column name.
    +   * @group param
    +   */
    +  final val censorCol: Param[String] = new Param[String](this, 
"censorCol", "censored column name")
    +
    +  /** @group getParam */
    +  final def getCensorCol: String = $(censorCol)
    +
    +  /**
    +   * Param for quantile column name.
    +   * @group param
    +   */
    +  final val quantileCol: Param[String] = new Param[String](this,
    +    "quantileCol", "quantile column name")
    +
    +  /** @group getParam */
    +  final def getQuantileCol: String = $(quantileCol)
    +
    +  /**
    +   * Validates and transforms the input schema with the provided param map.
    +   * @param schema input schema
    +   * @param fitting whether this is in fitting or prediction
    +   * @return output schema
    +   */
    +  protected def validateAndTransformSchema(
    +      schema: StructType,
    +      fitting: Boolean): StructType = {
    +    SchemaUtils.checkColumnType(schema, $(featuresCol), new VectorUDT)
    +    if (fitting) {
    +      SchemaUtils.checkColumnType(schema, $(censorCol), DoubleType)
    +      SchemaUtils.checkColumnType(schema, $(labelCol), DoubleType)
    +    } else {
    +      SchemaUtils.checkColumnType(schema, $(quantileCol), new VectorUDT)
    +    }
    +    SchemaUtils.appendColumn(schema, $(predictionCol), DoubleType)
    +  }
    +}
    +
    +@Experimental
    +class AFTRegression(override val uid: String)
    +  extends Estimator[AFTRegressionModel] with AFTRegressionParams with 
Logging {
    +
    +  def this() = this(Identifiable.randomUID("aftReg"))
    +
    +  /** @group setParam */
    +  def setFeaturesCol(value: String): this.type = set(featuresCol, value)
    +
    +  /** @group setParam */
    +  def setLabelCol(value: String): this.type = set(labelCol, value)
    +
    +  /** @group setParam */
    +  def setCensorCol(value: String): this.type = set(censorCol, value)
    +  setDefault(censorCol -> "censored")
    +
    +  /** @group setParam */
    +  def setPredictionCol(value: String): this.type = set(predictionCol, 
value)
    +
    +  /**
    +   * Set if we should fit the intercept
    +   * Default is true.
    +   * @group setParam
    +   */
    +  def setFitIntercept(value: Boolean): this.type = set(fitIntercept, value)
    +  setDefault(fitIntercept -> true)
    +
    +  /**
    +   * Set the maximum number of iterations.
    +   * Default is 100.
    +   * @group setParam
    +   */
    +  def setMaxIter(value: Int): this.type = set(maxIter, value)
    +  setDefault(maxIter -> 100)
    +
    +  /**
    +   * Set the convergence tolerance of iterations.
    +   * Smaller value will lead to higher accuracy with the cost of more 
iterations.
    +   * Default is 1E-6.
    +   * @group setParam
    +   */
    +  def setTol(value: Double): this.type = set(tol, value)
    +  setDefault(tol -> 1E-6)
    +
    +  /**
    +   * Extracts (feature, label, censored) from input dataset.
    +   */
    +  protected[ml] def extractCensoredLabeledPoints(
    +      dataset: DataFrame): RDD[(Vector, Double, Double)] = {
    +    dataset.select($(featuresCol), $(labelCol), $(censorCol))
    +      .map { case Row(feature: Vector, label: Double, censored: Double) =>
    +      (feature, label, censored)
    +    }
    +  }
    +
    +  override def fit(dataset: DataFrame): AFTRegressionModel = {
    +    validateAndTransformSchema(dataset.schema, fitting = true)
    +    val instances = extractCensoredLabeledPoints(dataset)
    +    val handlePersistence = dataset.rdd.getStorageLevel == 
StorageLevel.NONE
    +    if (handlePersistence) instances.persist(StorageLevel.MEMORY_AND_DISK)
    +
    +    val costFun = new AFTCostFun(instances, $(fitIntercept))
    +    val optimizer = new BreezeLBFGS[BDV[Double]]($(maxIter), 10, $(tol))
    +
    +    val numFeatures = 
dataset.select($(featuresCol)).take(1)(0).getAs[Vector](0).size
    +    /*
    +       The weights vector has three parts:
    +       the first element: Double, log(sigma), the log of scale parameter
    +       the second element: Double, intercept of the beta parameter
    +       the third to the end elements: Doubles, weights vector of the beta 
parameter
    +     */
    +    val initialWeights = new DenseVector(Array.fill(numFeatures + 2)(1.0))
    --- End diff --
    
    I think we need to check if `fitIntercept == true`.


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