[Numpy-discussion] numpy.testing.asserts and masked array

josef.pktd@gmai... josef.pktd@gmai...
Fri Jan 16 09:51:19 CST 2009


I have a regression result with masked arrays that produces a masked
array output, estm5.yhat, and I want to test equality to the benchmark
case, estm1.yhat, with the asserts in numpy.testing, but I am getting
strange results.

checking for equality directly returns True, but the asserts in
numpy.testing think that they are not equal. It seems the asserts use
one of the masked (random) values in the comparison.

Whats the trick to assert_almost_equal for masked arrays?

Josef

>>> np.all(estm5.yhat == estm1.yhat)
True

and they look the same:
>>> estm5.yhat.T
masked_array(data =
 [[-- 3.72175928422 4.44050187015 3.86191008624 7.54173762712 3.52929761101
  5.00953694359 5.19214323965 4.71500145194 8.22336216275 7.8884375775
  5.24556560163 6.81814135969 7.65416592877 3.7608331097]],
      mask =
 [[ True False False False False False False False False False False False
  False False False]],
      fill_value=1e+020)

>>> estm1.yhat.T
masked_array(data =
 [[-- 3.72175928422 4.44050187015 3.86191008624 7.54173762712 3.52929761101
  5.00953694359 5.19214323965 4.71500145194 8.22336216275 7.8884375775
  5.24556560163 6.81814135969 7.65416592877 3.7608331097]],
      mask =
 [[ True False False False False False False False False False False False
  False False False]],
      fill_value=1e+020)




using any np.testing.assert returns False

>>> np.testing.assert_array_almost_equal(estm5.yhat, estm1.yhat)
Traceback (most recent call last):
  File "<pyshell#253>", line 1, in <module>
    np.testing.assert_array_almost_equal(estm5.yhat, estm1.yhat)
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 310, in assert_array_almost_equal
    header='Arrays are not almost equal')
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 299, in assert_array_compare
    raise ValueError(msg)
ValueError:
Arrays are not almost equal
 x: array([[ 6.1104881 ],
       [ 3.72175928],
       [ 4.44050187],...
 y: array([[        NaN],
       [ 3.72175928],
       [ 4.44050187],...
>>> np.testing.assert_almost_equal(estm5.yhat, estm1.yhat)
Traceback (most recent call last):
  File "<pyshell#254>", line 1, in <module>
    np.testing.assert_almost_equal(estm5.yhat, estm1.yhat)
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 208, in assert_almost_equal
    return assert_array_almost_equal(actual, desired, decimal, err_msg)
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 310, in assert_array_almost_equal
    header='Arrays are not almost equal')
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 299, in assert_array_compare
    raise ValueError(msg)
ValueError:
Arrays are not almost equal
 x: array([[ 6.1104881 ],
       [ 3.72175928],
       [ 4.44050187],...
 y: array([[        NaN],
       [ 3.72175928],
       [ 4.44050187],...
>>> np.testing.assert_equal(estm5.yhat, estm1.yhat)
Traceback (most recent call last):
  File "<pyshell#255>", line 1, in <module>
    np.testing.assert_equal(estm5.yhat, estm1.yhat)
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 180, in assert_equal
    return assert_array_equal(actual, desired, err_msg, verbose)
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 303, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 295, in assert_array_compare
    raise AssertionError(msg)
AssertionError:
Arrays are not equal

(mismatch 100.0%)
 x: array([[ 6.1104881 ],
       [ 3.72175928],
       [ 4.44050187],...
 y: array([[        NaN],
       [ 3.72175928],
       [ 4.44050187],...
>>> estm5.yhat == estm1.yhat
masked_array(data =
 [[--]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]
 [True]],
      mask =
 [[ True]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]],
      fill_value=1e+020)


More information about the Numpy-discussion mailing list