1

I have learnt inf from: How can I represent an infinite number in Python?

I wish to find the minimum value of an array with 'inf' included

In [330]: residuals
Out[330]: 
array([[ 2272.35651718,  1387.71126686,  1115.48728484],
       [  695.08009848,            inf,            inf],
       [  601.44997093,            inf,            inf]])

In [331]: min(residuals)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-331-69b09e4201cf> in <module>()
----> 1 min(residuals)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

However it seems like 'inf' is ambiguous? Is there a smart way to find the minimum value rather than run a loop on every value of this array?

Community
  • 1
  • 1
Jim
  • 319
  • 1
  • 3
  • 8

3 Answers3

5

This problem is not related to inf, it's a byproduct of using python's built-in min function with a numpy array that has more than one dimension.

numpy.min( residuals ) should do what you want, assuming you're looking for the smallest element in any of the sub-arrays.

loopbackbee
  • 21,962
  • 10
  • 62
  • 97
2

Try -

residuals.min()

Example -

In [6]: import numpy as np

In [7]: a = np.array([[1,2,3,4],[np.inf,4,5,6],[7,8,9,10]])

In [8]: a
Out[8]:
array([[  1.,   2.,   3.,   4.],
       [ inf,   4.,   5.,   6.],
       [  7.,   8.,   9.,  10.]])

In [9]: a.min()
Out[9]: 1.0
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

You have a matrix and min is not smart enough to iterate though it properly. You can use numpy.min or something like:

min([x for x in y for y in residuals])
Sorin
  • 11,863
  • 22
  • 26