Github user feynmanliang commented on a diff in the pull request:
https://github.com/apache/spark/pull/7884#discussion_r38122369
--- 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 --
OK I see what's going on; `fold` on the either expects two functions into
the same type so type inference is inferring an upper bound for `RDD[(Double,
Vector)]` and `RDD[(Double, Double, Vector)]` whereas in the earlier code
`instances` was bound by the concrete types within the `Either`.
We can leave as is or remove the `Either`s and use `RDD[(Double, 1.0,
Vector)]` for the unweighted instances; I am a fan of removing the `Either`s
since that will reduce pattern matching code but both approaches are acceptable
to me.
---
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]