code: def KernelMatrix(
      X, k=dot
  ):  # XXX: generalized dot/inner product?  K(X,Y)?
      "inner product of X with self, using k as elementwise product function"
      # the following is tensordot(X,X,axes=(-1,-1)), with dot --> k
      # 3-clause BSD (see: v1.7.2 http://docs.scipy.org/doc/numpy/license.html)
      X = asarray(X)
      Xs = X.shape
      ndX = len(X.shape)
      nX = 1
      axes = [ndX - 1]
  
      # Move the axes to sum over to the end of "a"
      # and to the front of "b" in inner(a,b)
      notin = [_ for _ in range(ndX) if _ not in axes]
      newaxes_a = notin + axes
      N2 = 1
      for axis in axes:
          N2 *= Xs[axis]
      newshape_a = (-1, N2)
      olda = [Xs[axis] for axis in notin]
  
      newaxes_b = axes + notin
      N2 = 1
      for axis in axes:
          N2 *= Xs[axis]
      newshape_b = (N2, -1)
      oldb = [Xs[axis] for axis in notin]
  
      at = X.transpose(newaxes_a).reshape(newshape_a)
      bt = X.transpose(newaxes_b).reshape(newshape_b)
      return k(at, bt).reshape(olda + oldb)
  
imports: from numpy import multiply, asarray, dot, transpose, sum
label_desc: Use image node_id 1 for calling a global function with example usage: KernelMatrix(X, k) and returns: k