Github user dbtsai commented on a diff in the pull request:
https://github.com/apache/spark/pull/7884#discussion_r36226698
--- 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 --
I probably will have `override def count: Long = totalCnt` as really count,
and another `def weightedCount: Double` for weightedCount. The mean and
variance will always be computed by `weightedCount`, but users can still know
what's real total count by keeping the old behavior.
---
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]