You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by sethah <gi...@git.apache.org> on 2017/01/04 19:04:20 UTC

[GitHub] spark pull request #15435: [SPARK-17139][ML] Add model summary for Multinomi...

Github user sethah commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15435#discussion_r94621426
  
    --- Diff: mllib/src/test/scala/org/apache/spark/ml/classification/LogisticRegressionSuite.scala ---
    @@ -1762,51 +1781,101 @@ class LogisticRegressionSuite
       }
     
       test("evaluate on test set") {
    -    // TODO: add for multiclass when model summary becomes available
         // Evaluate on test set should be same as that of the transformed training data.
    -    val lr = new LogisticRegression()
    +    val blor = new LogisticRegression()
           .setMaxIter(10)
           .setRegParam(1.0)
           .setThreshold(0.6)
    -    val model = lr.fit(smallBinaryDataset)
    -    val summary = model.summary.asInstanceOf[BinaryLogisticRegressionSummary]
    -
    -    val sameSummary =
    -      model.evaluate(smallBinaryDataset).asInstanceOf[BinaryLogisticRegressionSummary]
    -    assert(summary.areaUnderROC === sameSummary.areaUnderROC)
    -    assert(summary.roc.collect() === sameSummary.roc.collect())
    -    assert(summary.pr.collect === sameSummary.pr.collect())
    +    val blorModel = blor.fit(smallBinaryDataset)
    +    val blorSummary = blorModel.binarySummary
    +
    +    val sameBlorSummary =
    +      blorModel.evaluate(smallBinaryDataset).asInstanceOf[BinaryLogisticRegressionSummary]
    +    assert(blorSummary.areaUnderROC === sameBlorSummary.areaUnderROC)
    +    assert(blorSummary.roc.collect() === sameBlorSummary.roc.collect())
    +    assert(blorSummary.pr.collect === sameBlorSummary.pr.collect())
         assert(
    -      summary.fMeasureByThreshold.collect() === sameSummary.fMeasureByThreshold.collect())
    -    assert(summary.recallByThreshold.collect() === sameSummary.recallByThreshold.collect())
    +      blorSummary.fMeasureByThreshold.collect() === sameBlorSummary.fMeasureByThreshold.collect())
         assert(
    -      summary.precisionByThreshold.collect() === sameSummary.precisionByThreshold.collect())
    +      blorSummary.recallByThreshold.collect() === sameBlorSummary.recallByThreshold.collect())
    +    assert(
    +      blorSummary.precisionByThreshold.collect()
    +        === sameBlorSummary.precisionByThreshold.collect())
    +
    +    val mlor = new LogisticRegression()
    +      .setMaxIter(10)
    +      .setRegParam(1.0)
    +      .setFamily("multinomial")
    +    val mlorModel = blor.fit(smallMultinomialDataset)
    +    val mlorSummary = mlorModel.summary
    +
    +    val mlorSameSummary = mlorModel.evaluate(smallMultinomialDataset)
    +      .asInstanceOf[MultinomialLogisticRegressionSummary]
    +
    +    assert(mlorSummary.falsePositiveRateByLabel === mlorSameSummary.falsePositiveRateByLabel)
    +    assert(mlorSummary.precisionByLabel === mlorSameSummary.precisionByLabel)
    +    assert(mlorSummary.recallByLabel === mlorSameSummary.recallByLabel)
    +    assert(mlorSummary.fMeasureByLabel === mlorSameSummary.fMeasureByLabel)
    +    assert(mlorSummary.accuracy === mlorSameSummary.accuracy)
    +    assert(mlorSummary.weightedFalsePositiveRate === mlorSameSummary.weightedFalsePositiveRate)
    +    assert(mlorSummary.weightedPrecision === mlorSameSummary.weightedPrecision)
    +    assert(mlorSummary.weightedRecall === mlorSameSummary.weightedRecall)
    +    assert(mlorSummary.weightedFMeasure === mlorSameSummary.weightedFMeasure)
       }
     
       test("evaluate with labels that are not doubles") {
         // Evaluate a test set with Label that is a numeric type other than Double
    -    val lr = new LogisticRegression()
    +    val blor = new LogisticRegression()
           .setMaxIter(1)
           .setRegParam(1.0)
    -    val model = lr.fit(smallBinaryDataset)
    -    val summary = model.evaluate(smallBinaryDataset).asInstanceOf[BinaryLogisticRegressionSummary]
    +    val blorModel = blor.fit(smallBinaryDataset)
    +    val blorSummary = blorModel.evaluate(smallBinaryDataset)
    +      .asInstanceOf[BinaryLogisticRegressionSummary]
     
    -    val longLabelData = smallBinaryDataset.select(col(model.getLabelCol).cast(LongType),
    -      col(model.getFeaturesCol))
    -    val longSummary = model.evaluate(longLabelData).asInstanceOf[BinaryLogisticRegressionSummary]
    +    val blorLongLabelData = smallBinaryDataset.select(col(blorModel.getLabelCol).cast(LongType),
    +      col(blorModel.getFeaturesCol))
    +    val blorLongSummary = blorModel.evaluate(blorLongLabelData)
    +      .asInstanceOf[BinaryLogisticRegressionSummary]
     
    -    assert(summary.areaUnderROC === longSummary.areaUnderROC)
    +    assert(blorSummary.areaUnderROC === blorLongSummary.areaUnderROC)
    +
    +    val mlor = new LogisticRegression()
    +      .setMaxIter(1)
    +      .setRegParam(1.0)
    +      .setFamily("multinomial")
    +    val mlorModel = mlor.fit(smallMultinomialDataset)
    +    val mlorSummary = mlorModel.evaluate(smallMultinomialDataset)
    +      .asInstanceOf[MultinomialLogisticRegressionSummary]
    +
    +    val mlorLongLabelData = smallMultinomialDataset.select(
    +      col(mlorModel.getLabelCol).cast(LongType),
    +      col(mlorModel.getFeaturesCol))
    +    val mlorLongSummary = mlorModel.evaluate(mlorLongLabelData)
    +      .asInstanceOf[MultinomialLogisticRegressionSummary]
    +
    +    assert(mlorSummary.accuracy === mlorLongSummary.accuracy)
       }
     
       test("statistics on training data") {
         // Test that loss is monotonically decreasing.
    -    val lr = new LogisticRegression()
    +    val blor = new LogisticRegression()
           .setMaxIter(10)
           .setRegParam(1.0)
           .setThreshold(0.6)
    --- End diff --
    
    let's remove this since it is not relevant to the test. Also add `.setFamily("binomial")`


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org