[Numpy-discussion] The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Help! I'm having a problem in searching through the *elements* if a 2d array. I have a loop over a numpy array: n,m = G.shape print n,m for i in xrange(n): for j in xrange(m): print type(G), type(G[i,j]), type(float(G[i,j])) g = float(abs(G[i,j])) if g < cut: print i,j,G[i,j] However, I'm getting the error message: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() despite the fact that I'm not computing a truth value of an array. I'd like to just compare to G[i,j] or abs(G[i,j]), which should be a *single* value, and *not* an array. I even call 'float' here to make sure that I cast this to a normal python float. But I still get this error message. At this point, I suspect that I'm doing something dumb, rather than discovering some unique bug in numpy. Can anyone help me out? -- Rick Muller rpmul...@gmail.com 505-750-7557 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
On Wed, Oct 27, 2010 at 15:58, Rick Muller wrote: > Help! I'm having a problem in searching through the *elements* if a 2d > array. I have a loop over a numpy array: > > n,m = G.shape > print n,m > for i in xrange(n): > for j in xrange(m): > print type(G), type(G[i,j]), type(float(G[i,j])) > g = float(abs(G[i,j])) > if g < cut: > print i,j,G[i,j] > > However, I'm getting the error message: > > The truth value of an array with more than one element is ambiguous. Use > a.any() or a.all() > > despite the fact that I'm not computing a truth value of an array. I'd like > to just compare to G[i,j] or abs(G[i,j]), which should be a *single* value, > and *not* an array. I even call 'float' here to make sure that I cast this > to a normal python float. But I still get this error message. > > At this point, I suspect that I'm doing something dumb, rather than > discovering some unique bug in numpy. Can anyone help me out? Can you provide a complete, self-contained example that does not work? What kind of object is cut? -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
> Help! I'm having a problem in searching through the *elements* if a > 2d array. I have a loop over a numpy array: > > n,m = G.shape > print n,m > for i in xrange(n): > for j in xrange(m): > print type(G), type(G[i,j]), type(float(G[i,j])) > g = float(abs(G[i,j])) > if g < cut: > print i,j,G[i,j] > > However, I'm getting the error message: > > The truth value of an array with more than one element is > ambiguous. Use a.any() or a.all() > > despite the fact that I'm not computing a truth value of an array. > I'd like to just compare to G[i,j] or abs(G[i,j]), which should be a > *single* value, and *not* an array. I even call 'float' here to make > sure that I cast this to a normal python float. But I still get this > error message. Which line is raising the error? My guess is it's the only truth-value testing line: "if g < cut". (I'm not sure what else could error here in that way...) It looks like g is probably a scalar, but your code isn't showing where cut comes from, nor have you printed out it's type... is it an array? Zach ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Robert, Zachary, Thanks for the quick help. I tried to write a little self-contained example of the crash, but it wasn't working. (I'm sure you've already figured out how this ends.) When I tracked down why it wasn't working, I found my bug. Turned out that cut, which I assumed was just a small scalar value, was actually a matrix, since I was passing in the wrong number of arguments to the function. Hence the confusing error. Thanks again, and sorry for wasting your time. Rick On Wed, Oct 27, 2010 at 3:04 PM, Zachary Pincus wrote: > > Help! I'm having a problem in searching through the *elements* if a > > 2d array. I have a loop over a numpy array: > > > > n,m = G.shape > > print n,m > > for i in xrange(n): > > for j in xrange(m): > > print type(G), type(G[i,j]), type(float(G[i,j])) > > g = float(abs(G[i,j])) > > if g < cut: > > print i,j,G[i,j] > > > > However, I'm getting the error message: > > > > The truth value of an array with more than one element is > > ambiguous. Use a.any() or a.all() > > > > despite the fact that I'm not computing a truth value of an array. > > I'd like to just compare to G[i,j] or abs(G[i,j]), which should be a > > *single* value, and *not* an array. I even call 'float' here to make > > sure that I cast this to a normal python float. But I still get this > > error message. > > Which line is raising the error? > My guess is it's the only truth-value testing line: "if g < cut". (I'm > not sure what else could error here in that way...) It looks like g is > probably a scalar, but your code isn't showing where cut comes from, > nor have you printed out it's type... is it an array? > > Zach > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > -- Rick Muller rpmul...@gmail.com 505-750-7557 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion