Reputation: 652
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(values):
print(values)
mean = np.mean(values)
result = 0
for i in range(len(values)):
result += math.pow(values[i] - mean, 2)
return result
if isinstance(values, np.ndarray):
return np.vectorize(innerFunc)(values)
else:
return innerFunc(values)
Although I want to vectorize over 1 dimension, i.e., one tuple is executed inside the innerFunc, but my output is as follows:
[[ 4 5 2 18]
[ 4 7 3 8]]
4
...
Which means the vectorize function is vectorizing over 2 dimensions, and I am getting the following error:
for i in range(len(values)):
TypeError: object of type 'numpy.int64' has no len()
What changes to make so that the output is:
[[ 4 5 2 18]
[ 4 7 3 8]]
[4 5 2 18]
...
something like this
Thank you.
EDIT
It is working as accepted when the tuples are different length, can anyone explain this,
e.g., my input is
np.array([(4, 5, 2, 18), (4, 7, 3,)])
and the function prints
[(4, 5, 2, 18) (4, 7, 3)]
(4, 5, 2, 18)
(4, 7, 3)
and the returned value is
[158.75 8.66666667]
So, only when all the tuples are the same length, the function treats them as numbers.
Thank you.
Upvotes: 2
Views: 1930
Reputation: 231500
In [1]: values = np.array([(4, 5, 2, 18), (4, 7, 3, 8)])
In [2]: values
Out[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.
Upvotes: 3