Search code examples
pythonscipycurve-fittinginfinity

Discern infinite value from array of floats


I'm using the function scipy.optimize.curve_fit to fit some data. If the fit converges normally, curve_fit will return two arrays popt and pcov that will look something like this for a function of two parameters:

print popt
[  2.97591488e-03   3.08947359e+01]
print pcov
[[  2.44224139e-06  -1.67832708e-02]
 [ -1.67832708e-02   1.16808802e+02]]

The issue is that occasionally the function will fail to obtain a valid pcov resulting in:

print pcov
inf

I need to be able to tell when pcov is a "valid" list/array of floats and when it's an inf "invalid" value.

I've tried:

if np.isfinite(np.array(pcov).any()):
    print 'valid'
else:
    print 'invalid'

but this does not give good results.


Solution

  • Nevermind, just a temporary mind block from 11+ hours of writing code. The correct way is:

    if np.isfinite(pcov).all():
        print 'valid'
    else:
        print 'invalid'