Suppose I have two 2D NumPy arrays A
and B
, I would like to compute the matrix C
whose entries are C[i, j] = f(A[i, :], B[:, j])
, where f
is some function that takes two 1D arrays and returns a number.
For instance, if def f(x, y): return np.sum(x * y)
then I would simply have C = np.dot(A, B)
. However, for a general function f
, are there NumPy/SciPy utilities I could exploit that are more efficient than doing a double for-loop?
For example, take def f(x, y): return np.sum(x != y) / len(x)
, where x
and y
are not simply 0/1-bit vectors.