Github user feynmanliang commented on a diff in the pull request: https://github.com/apache/spark/pull/7884#discussion_r38057903 --- Diff: mllib/src/main/scala/org/apache/spark/ml/classification/LogisticRegression.scala --- @@ -218,31 +217,59 @@ class LogisticRegression(override val uid: String) override def getThreshold: Double = super.getThreshold + /** + * Whether to over-/undersamples each of training sample according to the given + * weight in `weightCol`. If empty, all samples are supposed to have weights as 1.0. + * Default is empty, so all samples have weight one. + * @group setParam + */ + def setWeightCol(value: String): this.type = set(weightCol, value) + setDefault(weightCol -> "") + override def setThresholds(value: Array[Double]): this.type = super.setThresholds(value) override def getThresholds: Array[Double] = super.getThresholds override protected def train(dataset: DataFrame): LogisticRegressionModel = { // Extract columns from data. If dataset is persisted, do not persist oldDataset. - val instances = extractLabeledPoints(dataset).map { - case LabeledPoint(label: Double, features: Vector) => (label, features) - } + val instances: Either[RDD[(Double, Vector)], RDD[(Double, Double, Vector)]] = + if ($(weightCol).isEmpty) { + Left(dataset.select($(labelCol), $(featuresCol)).map { + case Row(label: Double, features: Vector) => (label, features) + }) + } else { + Right(dataset.select($(labelCol), $(weightCol), $(featuresCol)).map { + case Row(label: Double, weight: Double, features: Vector) => + (label, weight, features) + }) + } + val handlePersistence = dataset.rdd.getStorageLevel == StorageLevel.NONE - if (handlePersistence) instances.persist(StorageLevel.MEMORY_AND_DISK) - - val (summarizer, labelSummarizer) = instances.treeAggregate( - (new MultivariateOnlineSummarizer, new MultiClassSummarizer))( - seqOp = (c, v) => (c, v) match { - case ((summarizer: MultivariateOnlineSummarizer, labelSummarizer: MultiClassSummarizer), - (label: Double, features: Vector)) => - (summarizer.add(features), labelSummarizer.add(label)) - }, - combOp = (c1, c2) => (c1, c2) match { - case ((summarizer1: MultivariateOnlineSummarizer, - classSummarizer1: MultiClassSummarizer), (summarizer2: MultivariateOnlineSummarizer, - classSummarizer2: MultiClassSummarizer)) => - (summarizer1.merge(summarizer2), classSummarizer1.merge(classSummarizer2)) - }) + if (handlePersistence) instances.fold(identity, identity).persist(StorageLevel.MEMORY_AND_DISK) + + val (summarizer, labelSummarizer) = { + val combOp = (c1: (MultivariateOnlineSummarizer, MultiClassSummarizer), + c2: (MultivariateOnlineSummarizer, MultiClassSummarizer)) => + (c1._1.merge(c2._1), c1._2.merge(c2._2)) + + instances match { --- End diff -- Oh, I meant something like ```scala val seqOP = instances match { case Left(instances: RDD[(Double, Vector)]) => (c: (MultivariateOnlineSummarizer, MultiClassSummarizer), v: (Double, Vector)) => (c._1.add(v._2), c._2.add(v._1)) case Right(instances: RDD[(Double, Double, Vector)]) => (c: (MultivariateOnlineSummarizer, MultiClassSummarizer), v: (Double, Double, Vector)) => { val weight = v._2 (c._1.add(v._3, weight), c._2.add(v._1, weight)) } } instances.treeAggregate( new MultivariateOnlineSummarizer, new MultiClassSummarizer)(seqOp, combOp) ```
--- 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