1

I start with a numpy array of numpy arrays, where each of the inner numpy arrays can have different lengths. An example is given below:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5])
c = np.array([a, b])

print c
[[1 2 3] [4 5]]

I'd like to be able to perform a boolean operation on every element in every element in array c, but when I try to I get the following value error:

print c > 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. 
Use a.any() or a.all()

I'd like to be able to get the result:

[[True True True] [True True]]

Without using a for loop or iterating on the outer array. Is this possible, and if so, how can I accomplish it?

Wasalski
  • 111
  • 1
  • 1
    What's wrong with outer for loop? Numpy is good at homogeneous arrays of numbers with fixed dimensions. Check [this SO discussion](http://stackoverflow.com/q/3386259/155813). – mg007 Jun 05 '13 at 15:24

1 Answers1

1

I can think of two broad approaches, either pad your arrays so that you can a single 2d array instead of nested arrays, or treat your nested arrays as a list of arrays. The first would look something like this:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5, -99])
c = np.array([a, b])

print c.shape
# (2, 3)
print c > 0
# [[ True  True  True]
#  [ True  True False]]

Or do something like:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5])
c = np.array([a, b])

out = [i > 0 for i in c]
print out
# [array([ True,  True,  True], dtype=bool), array([ True,  True], dtype=bool)]

If padding is not an option, you might in fact find that lists of arrays are better behaved than arrays of arrays.

Bi Rico
  • 25,283
  • 3
  • 52
  • 75