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

    https://github.com/apache/spark/pull/14326#discussion_r71992412
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/regression/RobustRegression.scala ---
    @@ -0,0 +1,466 @@
    +/*
    + * 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, LBFGSB => BreezeLBFGSB}
    +
    +import org.apache.spark.SparkException
    +import org.apache.spark.annotation.Since
    +import org.apache.spark.internal.Logging
    +import org.apache.spark.ml.PredictorParams
    +import org.apache.spark.ml.feature.Instance
    +import org.apache.spark.ml.linalg.{Vector, Vectors}
    +import org.apache.spark.ml.linalg.BLAS._
    +import org.apache.spark.ml.param.{DoubleParam, ParamMap, ParamValidators}
    +import org.apache.spark.ml.param.shared._
    +import org.apache.spark.ml.util._
    +import org.apache.spark.mllib.linalg.VectorImplicits._
    +import org.apache.spark.mllib.stat.MultivariateOnlineSummarizer
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.sql.{Dataset, Row}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.storage.StorageLevel
    +
    +/**
    + * Params for robust regression.
    + */
    +private[regression] trait RobustRegressionParams extends PredictorParams 
with HasRegParam
    +  with HasMaxIter with HasTol with HasFitIntercept with HasStandardization 
with HasWeightCol {
    +
    +  /**
    +   * The shape parameter to control the amount of robustness. Must be > 
1.0.
    +   * At larger values of M, the huber criterion becomes more similar to 
least squares regression;
    +   * for small values of M, the criterion is more similar to L1 regression.
    +   * Default is 1.35 to get as much robustness as possible while retaining
    +   * 95% statistical efficiency for normally distributed data.
    +   */
    +  @Since("2.1.0")
    +  final val m = new DoubleParam(this, "m", "The shape parameter to control 
the amount of " +
    +    "robustness. Must be > 1.0.", ParamValidators.gt(1.0))
    +
    +  /** @group getParam */
    +  @Since("2.1.0")
    +  def getM: Double = $(m)
    +}
    +
    +/**
    + * Robust regression.
    + *
    + * The learning objective is to minimize the huber loss, with 
regularization.
    + *
    + * The robust regression optimizes the squared loss for the samples where
    + * {{{ |\frac{(y - X \beta)}{\sigma}|\leq M }}}
    + * and the absolute loss for the samples where
    + * {{{ |\frac{(y - X \beta)}{\sigma}|\geq M }}},
    + * where \beta and \sigma are parameters to be optimized.
    + *
    + * This supports two types of regularization: None and L2.
    + *
    + * This estimator is different from the R implementation of Robust 
Regression
    + * ([[http://www.ats.ucla.edu/stat/r/dae/rreg.htm]]) because the R 
implementation does a
    + * weighted least squares implementation with weights given to each sample 
on the basis
    + * of how much the residual is greater than a certain threshold.
    + */
    +@Since("2.1.0")
    +class RobustRegression @Since("2.1.0") (@Since("2.1.0") override val uid: 
String)
    +  extends Regressor[Vector, RobustRegression, RobustRegressionModel]
    +  with RobustRegressionParams with Logging {
    +
    +  @Since("2.1.0")
    +  def this() = this(Identifiable.randomUID("robReg"))
    +
    +  /**
    +   * Sets the value of param [[m]].
    +   * Default is 1.35.
    +   * @group setParam
    +   */
    +  @Since("2.1.0")
    +  def setM(value: Double): this.type = set(m, value)
    +  setDefault(m -> 1.35)
    +
    +  /**
    +   * Sets the regularization parameter.
    +   * Default is 0.0.
    +   * @group setParam
    +   */
    +  @Since("2.1.0")
    +  def setRegParam(value: Double): this.type = set(regParam, value)
    +  setDefault(regParam -> 0.0)
    +
    +  /**
    +   * Sets if we should fit the intercept.
    +   * Default is true.
    +   * @group setParam
    +   */
    +  @Since("2.1.0")
    +  def setFitIntercept(value: Boolean): this.type = set(fitIntercept, value)
    +  setDefault(fitIntercept -> true)
    +
    +  /**
    +   * Whether to standardize the training features before fitting the model.
    +   * The coefficients of models will be always returned on the original 
scale,
    +   * so it will be transparent for users. Note that with/without 
standardization,
    +   * the models should be always converged to the same solution when no 
regularization
    +   * is applied.
    +   * Default is true.
    +   * @group setParam
    +   */
    +  @Since("2.1.0")
    +  def setStandardization(value: Boolean): this.type = set(standardization, 
value)
    +  setDefault(standardization -> true)
    +
    +  /**
    +   * Sets the maximum number of iterations.
    +   * Default is 100.
    +   * @group setParam
    +   */
    +  @Since("2.1.0")
    +  def setMaxIter(value: Int): this.type = set(maxIter, value)
    +  setDefault(maxIter -> 100)
    +
    +  /**
    +   * Sets the convergence tolerance of iterations.
    +   * Smaller value will lead to higher accuracy with the cost of more 
iterations.
    +   * Default is 1E-6.
    +   * @group setParam
    +   */
    +  @Since("2.1.0")
    +  def setTol(value: Double): this.type = set(tol, value)
    +  setDefault(tol -> 1E-6)
    +
    +  /**
    +   * Whether to over-/under-sample training instances according to the 
given weights in weightCol.
    +   * If not set or empty, all instances are treated equally (weight 1.0).
    +   * Default is not set, so all instances have weight one.
    +   * @group setParam
    +   */
    +  @Since("2.1.0")
    +  def setWeightCol(value: String): this.type = set(weightCol, value)
    +
    +  override protected def train(dataset: Dataset[_]): RobustRegressionModel 
= {
    +    val numFeatures = 
dataset.select(col($(featuresCol))).first().getAs[Vector](0).size
    +    val w = if (!isDefined(weightCol) || $(weightCol).isEmpty) lit(1.0) 
else col($(weightCol))
    +
    +    val instances: RDD[Instance] =
    +      dataset.select(col($(labelCol)), w, col($(featuresCol))).rdd.map {
    --- End diff --
    
    Why don't you use `as[Instance]`?


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