Skip to content Skip to sidebar Skip to footer

How To Perform A Numpy Function With A Numpy Array Of Numpy Array Objects?

Let say we have a function for creating numpy arrays of numpy array objects: randarr = lambda shape: np.random.randint(0, 10, shape) get_numpy_array_obj = lambda shapes: \ np.a

Solution 1:

In [99]: arr = np.array([np.ones(ij) for ij in [(1,3),(2,4),(3,2)]])
In [100]: arr
Out[100]: 
array([array([[1., 1., 1.]]),
       array([[1., 1., 1., 1.],
       [1., 1., 1., 1.]]),
       array([[1., 1.],
       [1., 1.],
       [1., 1.]])], dtype=object)

Operators like '+' work because arrays have the corresponding methods:

In [101]: arr[0].__add__
Out[101]: <method-wrapper '__add__' of numpy.ndarray object at 0xb64897a0>
In [102]: arr+arr
Out[102]: 
array([array([[2., 2., 2.]]),
       array([[2., 2., 2., 2.],
       [2., 2., 2., 2.]]),
       array([[2., 2.],
       [2., 2.],
       [2., 2.]])], dtype=object)

But functions like np.tanh don't have array methods.

frompyfunc passes elements of an array to your function and returns a object dtype array. Usually that's a pain, but in this case it's just what we want:

In [103]: np.frompyfunc(np.tan,1,1)(arr)
Out[103]: 
array([array([[1.55740772, 1.55740772, 1.55740772]]),
       array([[1.55740772, 1.55740772, 1.55740772, 1.55740772],
       [1.55740772, 1.55740772, 1.55740772, 1.55740772]]),
       array([[1.55740772, 1.55740772],
       [1.55740772, 1.55740772],
       [1.55740772, 1.55740772]])], dtype=object)

vectorize also uses frompyfunc but tries to convert the results to a numeric array. We can skip that by specifying an otypes:

In [104]: np.vectorize(np.tan,otypes='O')(arr)
Out[104]: 
array([array([[1.55740772, 1.55740772, 1.55740772]]),
       array([[1.55740772, 1.55740772, 1.55740772, 1.55740772],
       [1.55740772, 1.55740772, 1.55740772, 1.55740772]]),
       array([[1.55740772, 1.55740772],
       [1.55740772, 1.55740772],
       [1.55740772, 1.55740772]])], dtype=object)

Iteration on object arrays is between list iteration and numeric array iteration in speed. It has some of the array overhead, but not all. The elements are pointers, just as in lists.

frompyfunc can be a bit faster (up to 2x) than explicit iteration. vectorize is a bit slower because it has some overhead. The big advantage of these functions is that they handle broadcasting. Do your own timings to see if they help.


Post a Comment for "How To Perform A Numpy Function With A Numpy Array Of Numpy Array Objects?"