Github user dbtsai commented on a diff in the pull request: https://github.com/apache/spark/pull/7884#discussion_r36164446 --- Diff: mllib/src/main/scala/org/apache/spark/ml/classification/LogisticRegression.scala --- @@ -114,20 +114,40 @@ class LogisticRegression(override val uid: String) def setThreshold(value: Double): this.type = set(threshold, value) setDefault(threshold -> 0.5) + /** @group setParam */ + def setSampleWeightCol(value: String): this.type = set(sampleWeightCol, value) + + /** @group setParam */ + def setWeightedSample(value: Boolean): this.type = set(weightedSample, value) + 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 ($(weightedSample)) { + // TODO: Move `setWeightCol` and `extract weight column` code into Predictor class + // when we have more algorithms support this feature. + Right(dataset.select($(labelCol), $(sampleWeightCol), $(featuresCol)).map { + case Row(label: Double, sampleWeight: Double, features: Vector) => + (label, sampleWeight, features) + }) + } else { + Left(extractLabeledPoints(dataset).map { + case LabeledPoint(label: Double, features: Vector) => (label, features) + }) + } + val handlePersistence = dataset.rdd.getStorageLevel == StorageLevel.NONE - if (handlePersistence) instances.persist(StorageLevel.MEMORY_AND_DISK) + if (handlePersistence) instances.fold(identity, identity).persist(StorageLevel.MEMORY_AND_DISK) - val (summarizer, labelSummarizer) = instances.treeAggregate( + val (summarizer, labelSummarizer) = instances.fold(identity, identity).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)) + case ((summarizer: MultivariateOnlineSummarizer, labelSummarizer: MultiClassSummarizer), --- End diff -- BTW, in order to properly compute the mean and variance of weighted sample, I need to modify `MultivariateOnlineSummarizer` such that `override def count: Long = totalCnt` will have type of `Double`. I don't like this. I will rather to change `private var totalCnt: Long = 0` to type of `Double`, but still Long for `def count`. We can add another api called `def sampleWeightSum`. What do you think?
--- 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