Skip to content
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

Adding unit test to cover ties/duplicate x values in Isotonic Regression... #4185

83 changes: 83 additions & 0 deletions sklearn/tests/test_isotonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,89 @@ def test_isotonic_regression():
assert_array_equal(ir.fit_transform(np.ones(len(x)), y), y)


def test_isotonic_regression_ties_min():
# Setup examples with ties on minimum
x = [0, 1, 1, 2, 3, 4, 5]
y = [0, 1, 2, 3, 4, 5, 6]

# Check that we get identical results for fit/transform and fit_transform
ir = IsotonicRegression()
ir.fit(x, y)
assert_array_equal(ir.fit(x, y).transform(x), ir.fit_transform(x, y))


def test_isotonic_regression_ties_max():
# Setup examples with ties on maximum
x = [1, 2, 3, 4, 5, 5]
y = [1, 2, 3, 4, 5, 6]

# Check that we get identical results for fit/transform and fit_transform
ir = IsotonicRegression()
ir.fit(x, y)
assert_array_equal(ir.fit(x, y).transform(x), ir.fit_transform(x, y))


def test_isotonic_regression_ties_primary_fit():
"""
Test isotonic regression fit, transform against the "primary" ties method
and "pituitary" data from R "isotone" package, as detailed in
J. d. Leeuw, K. Hornik, P. Mair,
Isotone Optimization in R: Pool-Adjacent-Violators Algorithm
(PAVA) and Active Set Methods
"""

"""
Set values based on pituitiary example and
the following R command detailed in the paper above:
> library("isotone")
> data("pituitary")
> res1 <- gpava(pituitary$age, pituitary$size, ties="primary")
> res1$x

`isotone` version: 1.0-2, 2014-09-07
R version: R version 3.1.1 (2014-07-10)
"""
x = [8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14]
y = [21, 23.5, 23, 24, 21, 25, 21.5, 22, 19, 23.5, 25]
y_true = [21, 22.375, 22.375, 22.375, 22.375, 22.375, 22.375,
22.375, 22.375, 23.5, 25]

# Check fit, transform
ir = IsotonicRegression()
ir.fit(x, y)
assert_array_equal(ir.transform(x), y_true)


def test_isotonic_regression_ties_primary_fit_transform():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put the two in the same test, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

"""
Test isotonic regression fit_transform against the "primary" ties method
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 spaces after transform but besides LGTM if travis is happy :)

thanks @mjbommar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, fixed now, thanks.

travis was happy other than these 3 failures:

======================================================================
FAIL: sklearn.tests.test_isotonic.test_isotonic_regression_ties_min
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/data/workspace/scikit-learn/sklearn/tests/test_isotonic.py", line 92, in test_isotonic_regression_ties_min
    assert_array_equal(ir.fit(x, y).transform(x), ir.fit_transform(x, y))
  File "/usr/local/lib/python2.7/dist-packages/numpy/testing/utils.py", line 739, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File "/usr/local/lib/python2.7/dist-packages/numpy/testing/utils.py", line 665, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not equal

(mismatch 28.5714285714%)
 x: array([ 0.,  0.,  0.,  3.,  4.,  5.,  6.])
 y: array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.])

======================================================================
FAIL: sklearn.tests.test_isotonic.test_isotonic_regression_ties_max
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/data/workspace/scikit-learn/sklearn/tests/test_isotonic.py", line 103, in test_isotonic_regression_ties_max
    assert_array_equal(ir.fit(x, y).transform(x), ir.fit_transform(x, y))
  File "/usr/local/lib/python2.7/dist-packages/numpy/testing/utils.py", line 739, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File "/usr/local/lib/python2.7/dist-packages/numpy/testing/utils.py", line 665, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not equal

(mismatch 33.3333333333%)
 x: array([ 1.,  2.,  3.,  4.,  0.,  0.])
 y: array([ 1.,  2.,  3.,  4.,  5.,  6.])

======================================================================
FAIL: Test isotonic regression fit, transform against the "primary" ties method
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/data/workspace/scikit-learn/sklearn/tests/test_isotonic.py", line 134, in test_isotonic_regression_ties_primary_fit
    assert_array_equal(ir.transform(x), y_true)
  File "/usr/local/lib/python2.7/dist-packages/numpy/testing/utils.py", line 739, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File "/usr/local/lib/python2.7/dist-packages/numpy/testing/utils.py", line 665, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not equal

(mismatch 100.0%)
 x: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
 y: array([ 21.   ,  22.375,  22.375,  22.375,  22.375,  22.375,  22.375,
        22.375,  22.375,  23.5  ,  25.   ])

----------------------------------------------------------------------
Ran 24 tests in 0.041s

FAILED (failures=3)

and "pituitary" data from R "isotone" package, as detailed in
J. d. Leeuw, K. Hornik, P. Mair,
Isotone Optimization in R: Pool-Adjacent-Violators Algorithm
(PAVA) and Active Set Methods
"""

"""
Set values based on pituitiary example and
the following R command detailed in the paper above:
> library("isotone")
> data("pituitary")
> res1 <- gpava(pituitary$age, pituitary$size, ties="primary")
> res1$x

`isotone` version: 1.0-2, 2014-09-07
R version: R version 3.1.1 (2014-07-10)
"""
x = [8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14]
y = [21, 23.5, 23, 24, 21, 25, 21.5, 22, 19, 23.5, 25]
y_true = [21, 22.375, 22.375, 22.375, 22.375, 22.375, 22.375,
22.375, 22.375, 23.5, 25]

# Check fit_transform against y_true
ir = IsotonicRegression()
assert_array_equal(ir.fit_transform(x, y), y_true)


def test_isotonic_regression_reversed():
y = np.array([10, 9, 10, 7, 6, 6.1, 5])
y_ = IsotonicRegression(increasing=False).fit_transform(
Expand Down