-
Notifications
You must be signed in to change notification settings - Fork 29k
[Spark-21854] Added LogisticRegressionTrainingSummary for MultinomialLogisticRegression in Python API #19185
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
Changes from 6 commits
50cfafe
60579d5
1a73e6c
53ac68e
a4755d7
eb8f6b4
6529fa6
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 |
|---|---|---|
|
|
@@ -1451,7 +1451,7 @@ def test_glr_summary(self): | |
| sameSummary = model.evaluate(df) | ||
| self.assertAlmostEqual(sameSummary.deviance, s.deviance) | ||
|
|
||
| def test_logistic_regression_summary(self): | ||
| def test_binary_logistic_regression_summary(self): | ||
| df = self.spark.createDataFrame([(1.0, 2.0, Vectors.dense(1.0)), | ||
| (0.0, 2.0, Vectors.sparse(1, [], []))], | ||
| ["label", "weight", "features"]) | ||
|
|
@@ -1464,20 +1464,79 @@ def test_logistic_regression_summary(self): | |
| self.assertEqual(s.probabilityCol, "probability") | ||
| self.assertEqual(s.labelCol, "label") | ||
| self.assertEqual(s.featuresCol, "features") | ||
| self.assertEqual(s.predictionCol, "prediction") | ||
| objHist = s.objectiveHistory | ||
| self.assertTrue(isinstance(objHist, list) and isinstance(objHist[0], float)) | ||
| self.assertGreater(s.totalIterations, 0) | ||
| self.assertTrue(isinstance(s.labels, list)) | ||
| self.assertTrue(isinstance(s.truePositiveRateByLabel, list)) | ||
| self.assertTrue(isinstance(s.falsePositiveRateByLabel, list)) | ||
| self.assertTrue(isinstance(s.precisionByLabel, list)) | ||
| self.assertTrue(isinstance(s.recallByLabel, list)) | ||
| self.assertTrue(isinstance(s.fMeasureByLabel(), list)) | ||
| self.assertTrue(isinstance(s.fMeasureByLabel(1.0), list)) | ||
| self.assertTrue(isinstance(s.roc, DataFrame)) | ||
| self.assertAlmostEqual(s.areaUnderROC, 1.0, 2) | ||
| self.assertTrue(isinstance(s.pr, DataFrame)) | ||
| self.assertTrue(isinstance(s.fMeasureByThreshold, DataFrame)) | ||
| self.assertTrue(isinstance(s.precisionByThreshold, DataFrame)) | ||
| self.assertTrue(isinstance(s.recallByThreshold, DataFrame)) | ||
| self.assertAlmostEqual(s.accuracy, 1.0, 2) | ||
| self.assertAlmostEqual(s.weightedTruePositiveRate, 1.0, 2) | ||
| self.assertAlmostEqual(s.weightedFalsePositiveRate, 0.0, 2) | ||
| self.assertAlmostEqual(s.weightedRecall, 1.0, 2) | ||
| self.assertAlmostEqual(s.weightedPrecision, 1.0, 2) | ||
| self.assertAlmostEqual(s.weightedFMeasure(), 1.0, 2) | ||
| self.assertAlmostEqual(s.weightedFMeasure(1.0), 1.0, 2) | ||
| # test evaluation (with training dataset) produces a summary with same values | ||
| # one check is enough to verify a summary is returned, Scala version runs full test | ||
| sameSummary = model.evaluate(df) | ||
| self.assertAlmostEqual(sameSummary.areaUnderROC, s.areaUnderROC) | ||
|
|
||
| def test_multiclass_logistic_regression_summary(self): | ||
| df = self.spark.createDataFrame([(1.0, 2.0, Vectors.dense(1.0)), | ||
| (0.0, 2.0, Vectors.sparse(1, [], [])), | ||
| (2.0, 2.0, Vectors.dense(2.0)), | ||
| (2.0, 2.0, Vectors.dense(1.9))], | ||
| ["label", "weight", "features"]) | ||
| lr = LogisticRegression(maxIter=5, regParam=0.01, weightCol="weight", fitIntercept=False) | ||
| model = lr.fit(df) | ||
| self.assertTrue(model.hasSummary) | ||
| s = model.summary | ||
| # test that api is callable and returns expected types | ||
| self.assertTrue(isinstance(s.predictions, DataFrame)) | ||
| self.assertEqual(s.probabilityCol, "probability") | ||
| self.assertEqual(s.labelCol, "label") | ||
| self.assertEqual(s.featuresCol, "features") | ||
| self.assertEqual(s.predictionCol, "prediction") | ||
| objHist = s.objectiveHistory | ||
| self.assertTrue(isinstance(objHist, list) and isinstance(objHist[0], float)) | ||
| self.assertGreater(s.totalIterations, 0) | ||
| self.assertTrue(isinstance(s.labels, list)) | ||
| self.assertTrue(isinstance(s.truePositiveRateByLabel, list)) | ||
| self.assertTrue(isinstance(s.falsePositiveRateByLabel, list)) | ||
| self.assertTrue(isinstance(s.precisionByLabel, list)) | ||
| self.assertTrue(isinstance(s.recallByLabel, list)) | ||
| self.assertTrue(isinstance(s.fMeasureByLabel(), list)) | ||
| self.assertTrue(isinstance(s.fMeasureByLabel(1.0), list)) | ||
| self.assertAlmostEqual(s.accuracy, 0.75, 2) | ||
| self.assertAlmostEqual(s.weightedTruePositiveRate, 0.75, 2) | ||
| self.assertAlmostEqual(s.weightedFalsePositiveRate, 0.25, 2) | ||
| self.assertAlmostEqual(s.weightedRecall, 0.75, 2) | ||
| self.assertAlmostEqual(s.weightedPrecision, 0.583, 2) | ||
| self.assertAlmostEqual(s.weightedFMeasure(), 0.65, 2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add |
||
| self.assertAlmostEqual(s.weightedFMeasure(1.0), 0.65, 2) | ||
| # test evaluation (with training dataset) produces a summary with same values | ||
| # one check is enough to verify a summary is returned, Scala version runs full test | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add test for evaluation like: |
||
| sameSummary = model.evaluate(df) | ||
| self.assertAlmostEqual(sameSummary.accuracy, s.accuracy) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Like mentioned in annotation, one check is enough to verify a summary is returned, let's remove others to simplify the test. Thanks. |
||
| self.assertAlmostEqual(sameSummary.weightedTruePositiveRate, s.weightedTruePositiveRate) | ||
| self.assertAlmostEqual(sameSummary.weightedFalsePositiveRate, s.weightedFalsePositiveRate) | ||
| self.assertAlmostEqual(sameSummary.weightedRecall, s.weightedRecall) | ||
| self.assertAlmostEqual(sameSummary.weightedPrecision, s.weightedPrecision) | ||
| self.assertAlmostEqual(sameSummary.weightedFMeasure(), s.weightedFMeasure()) | ||
| self.assertAlmostEqual(sameSummary.weightedFMeasure(1.0), s.weightedFMeasure(1.0)) | ||
|
|
||
| def test_gaussian_mixture_summary(self): | ||
| data = [(Vectors.dense(1.0),), (Vectors.dense(5.0),), (Vectors.dense(10.0),), | ||
| (Vectors.sparse(1, [], []),)] | ||
|
|
||
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.
care to add these to the scala unit test for binary summary as well?
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.
also nit, but should probably add tests for all the new attributes, like
falsePositiveRateByLabelas below.