[MRG] Fix MultinomialDeviance not using average logloss (#10055)#10081
[MRG] Fix MultinomialDeviance not using average logloss (#10055)#10081rempfler wants to merge 9 commits intoscikit-learn:masterfrom
Conversation
|
Please add a test. Re the codecov failure: It seems we're never running these losses with |
|
not sure if need to change the computation of gradient also, I did not check the detail, but most like should update the gradient also. |
using |
This wouldnt take the mean in the unweighted case, since
Good idea. Having looked at [1, Sect. 10.6], I actually believe the call with weights was not correct so far, since the sample_weights are not multiplied with the [1] Friedman, Jerome, Trevor Hastie, and Robert Tibshirani. The elements of statistical learning. Vol. 1. New York: Springer series in statistics, 2001. |
Codecov Report
@@ Coverage Diff @@
## master #10081 +/- ##
==========================================
+ Coverage 96.19% 96.21% +0.01%
==========================================
Files 336 337 +1
Lines 62739 62899 +160
==========================================
+ Hits 60353 60518 +165
+ Misses 2386 2381 -5
Continue to review full report at Codecov.
|
|
good catch!
Actually the test is just checking that something is computed. Can we find an example where we know the expected output for a given input? Something that serves as a regression test for the previous wrong computation. In other words, a wrong computation shows that we were missing this thest. |
|
I don't think your edited comment came through on email, @massich, requesting a better test... |
|
@massich: technically, the previously wrong computation would have been caught by where the Nonetheless, i added one more small test with a manually pre-computed input-output pair. |
| loss_w_sw = loss(y, p, 0.5 * np.ones(p.shape[0], dtype=np.float32)) | ||
| assert_almost_equal(loss_wo_sw, loss_w_sw) | ||
|
|
||
| # second check |
There was a problem hiding this comment.
I would make this its own test
def test_loss_computation(pred=np.array([[1.0, 0, 0],
 [0, 0.5, 0.5]]),
y=np.array([0, 1]),
weights=np.array([1, 3]),
expected_loss=0.85637):
assert_almost_equal(loss(y, pred, weights), expected_loss, decimal=4)Maybe even add a test for weights=None.
There was a problem hiding this comment.
I would also split the rest of the test. I think that testing that the error raising should go on its own, etc..
|
|
|
|
||
|
|
||
| def test_mdl_exception(): | ||
| # Check that MultinomialDeviance throws when n_classes <= 2 |
|
|
||
| def test_mdl_exception(): | ||
| # Check that MultinomialDeviance throws when n_classes <= 2 | ||
| assert_raises(ValueError, MultinomialDeviance, 2) |
There was a problem hiding this comment.
Use assert_raises_regex and check which message is raise. We tend to check the error message rather than only checking that something is wrong ;)
There was a problem hiding this comment.
Shall we use the context manager here?
There was a problem hiding this comment.
I would not since sklearn already have helper function. If we want to use the context manager, we need to change all other asser_raises_*
| assert_almost_equal(loss_wo_sw, loss_w_sw) | ||
|
|
||
|
|
||
| def test_mdl_computation_unweighted(pred=np.array([[1.0, 0, 0], |
There was a problem hiding this comment.
Could you factorize this test and the next one using pytest.mark.parametrize?
There was a problem hiding this comment.
@glemaitre I proposed such parametrization to avoid import pytest, since I'm not sure if @lesteve was trying to avoid the pytest dependency.
| assert deviance_wo_w == deviance_w_w | ||
|
|
||
|
|
||
| def test_multinomial_deviance(): |
There was a problem hiding this comment.
I think that you can use pytest.mark.parametrize as well here.
|
I thin this is only in common test. We have added some in columntransformer and transformedtargetregressor
|
|
Ok that changes things ! |
For reference, I think we do not want pytest dependency in our modules but it is fine to use pytest in About whether we should import directly pytest in |
|
I don't think there is any good reason to apprehend a future in which we
stop using pytest. by all means, import it in test files, use its built-in
fixtures, etc.
|
Thanks this is what I felt as well, but this is always appreciated to have a second opinion. |
|
@glemaitre, @massich: your thoughts on the latest changes? |
|
I would personally use |
|
@glemaitre, @amueller, @lesteve, is the needed decision related to import or not pytest in the test? I suppose this is no longer a question... |
Yeah |
Fixes #10055
Changes
Changed MultinomialDeviance from total logloss to average logloss.