Efficient element-wise function computation in Python
问题 I have the following optimization problem. Given two np.arrays X , Y and a function K I would like to compute as fast as possible the matrix incidence gram_matrix where the (i,j)-th element is computed as K(X[i],Y[j]) . Here there an implementation using nested for-loops, which are acknowledged to be the slowest to solve these kind of problems. def proxy_kernel(X,Y,K): gram_matrix = np.zeros((X.shape[0], Y.shape[0])) for i, x in enumerate(X): for j, y in enumerate(Y): gram_matrix[i, j] = K(x,