I'm doing a little experiment with numpy arrays and I've come across the following problem. I'm trying to find a way to map a function that takes as input arrays over matrices, so that the function is applied to pair-wise elements of two or more matrices where those elements are arrays.
import numpy as np
x = np.random.random_integers(100, size=(5,4))
y = np.random.random_integers(100, size=(5,4))
print(x); print(y)
[[17 84 60 56]
[58 71 50 90]
[80 25 43 55]
[18 25 77 25]
[62 49 42 11]]
[[ 9 51 83 58]
[34 63 26 32]
[27 54 63 80]
[29 42 10 6]
[53 52 45 87]]
# np.dot(x,y) fails
v = np.vectorize(np.dot)
z = v(x,y)
print(z)
[[ 153 4284 4980 3248]
[1972 4473 1300 2880]
[2160 1350 2709 4400]
[ 522 1050 770 150]
[3286 2548 1890 957]] # this is wrong
np.sum(z[0]) == np.dot(x[0], y[0]) # prints True
# the vectorized dot function was applied over individual elements at the "bottom-most"
# (second) dimension when instead it should be applied to the array elements
# at the first dimension
# I could instead use list comprehension
z = [np.dot(a, b) for a, b in zip(x,y)]
print(z)
[12665, 10625, 10619, 2492, 8681]
# this would be a correct mapping of the dot function over the matrices x and y
The problem with list comprehensions is that I'm afraid that they're inefficient since they're a Python feature and not a Numpy feature.
vectorizeis a Python level loop; also it feeds scalars to your function, not arrays. I know it's tempting to use it without reading its docs, but it'll save you some false starts.