-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-18476][SPARKR][ML]:SparkR Logistic Regression should should support output original label. #15910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-18476][SPARKR][ML]:SparkR Logistic Regression should should support output original label. #15910
Changes from all commits
0ff84f0
9a07aa1
9d19284
71f7de2
57cf430
de74906
b1f7b23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ package org.apache.spark | |
|
|
||
| import java.io._ | ||
| import java.lang.reflect.Constructor | ||
| import java.net.{MalformedURLException, URI} | ||
| import java.net.{URI} | ||
|
||
| import java.util.{Arrays, Locale, Properties, ServiceLoader, UUID} | ||
| import java.util.concurrent.{ConcurrentHashMap, ConcurrentMap} | ||
| import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger, AtomicReference} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,9 @@ import org.json4s.JsonDSL._ | |
| import org.json4s.jackson.JsonMethods._ | ||
|
|
||
| import org.apache.spark.ml.{Pipeline, PipelineModel} | ||
| import org.apache.spark.ml.attribute.AttributeGroup | ||
| import org.apache.spark.ml.classification.{BinaryLogisticRegressionSummary, LogisticRegression, LogisticRegressionModel} | ||
| import org.apache.spark.ml.feature.RFormula | ||
| import org.apache.spark.ml.feature.{IndexToString, RFormula} | ||
| import org.apache.spark.ml.r.RWrapperUtils._ | ||
| import org.apache.spark.ml.util._ | ||
| import org.apache.spark.sql.{DataFrame, Dataset} | ||
|
|
||
|
|
@@ -34,6 +34,8 @@ private[r] class LogisticRegressionWrapper private ( | |
| val features: Array[String], | ||
| val isLoaded: Boolean = false) extends MLWritable { | ||
|
|
||
| import LogisticRegressionWrapper._ | ||
|
|
||
| private val logisticRegressionModel: LogisticRegressionModel = | ||
| pipeline.stages(1).asInstanceOf[LogisticRegressionModel] | ||
|
|
||
|
|
@@ -57,7 +59,11 @@ private[r] class LogisticRegressionWrapper private ( | |
| lazy val recallByThreshold: DataFrame = blrSummary.recallByThreshold | ||
|
|
||
| def transform(dataset: Dataset[_]): DataFrame = { | ||
| pipeline.transform(dataset).drop(logisticRegressionModel.getFeaturesCol) | ||
| pipeline.transform(dataset) | ||
| .drop(PREDICTED_LABEL_INDEX_COL) | ||
| .drop(logisticRegressionModel.getFeaturesCol) | ||
| .drop(logisticRegressionModel.getLabelCol) | ||
|
|
||
| } | ||
|
|
||
| override def write: MLWriter = new LogisticRegressionWrapper.LogisticRegressionWrapperWriter(this) | ||
|
|
@@ -66,14 +72,16 @@ private[r] class LogisticRegressionWrapper private ( | |
| private[r] object LogisticRegressionWrapper | ||
| extends MLReadable[LogisticRegressionWrapper] { | ||
|
|
||
| val PREDICTED_LABEL_INDEX_COL = "pred_label_idx" | ||
| val PREDICTED_LABEL_COL = "prediction" | ||
|
|
||
| def fit( // scalastyle:ignore | ||
| data: DataFrame, | ||
| formula: String, | ||
| regParam: Double, | ||
| elasticNetParam: Double, | ||
| maxIter: Int, | ||
| tol: Double, | ||
| fitIntercept: Boolean, | ||
| family: String, | ||
| standardization: Boolean, | ||
| thresholds: Array[Double], | ||
|
|
@@ -84,14 +92,14 @@ private[r] object LogisticRegressionWrapper | |
|
|
||
| val rFormula = new RFormula() | ||
| .setFormula(formula) | ||
| RWrapperUtils.checkDataColumns(rFormula, data) | ||
| .setForceIndexLabel(true) | ||
| checkDataColumns(rFormula, data) | ||
| val rFormulaModel = rFormula.fit(data) | ||
|
|
||
| // get feature names from output schema | ||
| val schema = rFormulaModel.transform(data).schema | ||
| val featureAttrs = AttributeGroup.fromStructField(schema(rFormulaModel.getFeaturesCol)) | ||
| .attributes.get | ||
| val features = featureAttrs.map(_.name.get) | ||
| val fitIntercept = rFormula.hasIntercept | ||
|
|
||
| // get labels and feature names from output schema | ||
| val (features, labels) = getFeaturesAndLabels(rFormulaModel, data) | ||
|
|
||
| // assemble and fit the pipeline | ||
| val logisticRegression = new LogisticRegression() | ||
|
||
|
|
@@ -105,16 +113,23 @@ private[r] object LogisticRegressionWrapper | |
| .setWeightCol(weightCol) | ||
| .setAggregationDepth(aggregationDepth) | ||
| .setFeaturesCol(rFormula.getFeaturesCol) | ||
| .setLabelCol(rFormula.getLabelCol) | ||
| .setProbabilityCol(probability) | ||
| .setPredictionCol(PREDICTED_LABEL_INDEX_COL) | ||
|
|
||
| if (thresholds.length > 1) { | ||
| logisticRegression.setThresholds(thresholds) | ||
| } else { | ||
| logisticRegression.setThreshold(thresholds(0)) | ||
| } | ||
|
|
||
| val idxToStr = new IndexToString() | ||
| .setInputCol(PREDICTED_LABEL_INDEX_COL) | ||
| .setOutputCol(PREDICTED_LABEL_COL) | ||
| .setLabels(labels) | ||
|
|
||
| val pipeline = new Pipeline() | ||
| .setStages(Array(rFormulaModel, logisticRegression)) | ||
| .setStages(Array(rFormulaModel, logisticRegression, idxToStr)) | ||
| .fit(data) | ||
|
|
||
| new LogisticRegressionWrapper(pipeline, features) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how reliable is this test? the order of rows is not guaranteed unless it is enforced by a sort or something, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically, the order is not guaranteed. However, we did similar work from the first test case of mllib.R, but never had a problem until now. I'd like to enforce the tests here and other places, but may be in a separate work should be better since it involves lots of other tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good, separate JIRA then. If tests haven't been failing perhaps it is not huge problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try to create follow-up jira for this.