Skip to content Skip to sidebar Skip to footer

Numpy Vectorize() Is Flattening The Whole Array

My input is a numpy array of tuples values = np.array([(4, 5, 2, 18), (4, 7, 3, 8)]) and my function is as follows: def outerFunc(values): print(values) def innerFunc(val

Solution 1:

In [1]: values= np.array([(4, 5, 2, 18), (4, 7, 3, 8)])                        
In [2]: valuesOut[2]: 
array([[ 4,  5,  2, 18],
       [ 4,  7,  3,  8]])
In [3]: values.shape                                                            
Out[3]: (2, 4)
In [4]: x=np.array([(4, 5, 2, 18), (4, 7, 3,)])                                 
In [5]: x                                                                       
Out[5]: array([(4, 5, 2, 18), (4, 7, 3)], dtype=object)
In [6]: x.shape                                                                 
Out[6]: (2,)

values is a 2d numeric array. np.vectorize passes each of the 8 elements, one at a time, to your inner function. It does not iterate by rows.

x is a 1d array with 2 elements (tuples). vectorize will pass each of those tuples to your inner.

Don't use vectorize when a simple iteration would work - it's slower and harder to use right.

And look at your arrays after you create them, making sure you understand the shape and dtype. Don't make assumptions.

Post a Comment for "Numpy Vectorize() Is Flattening The Whole Array"