0

let K(x, z) be (x_transpose*z + p_constant)**2.

I want to compute the n*n matrix K, where K_ij = k(X_i, X_j)

X is a n by d matrix, and X_i is the transpose of the ith row of X.

Does anyone know of a quick way to compute this? I'm using python.


Wait a second, is K just XX^T?

Jobs
  • 3,317
  • 6
  • 26
  • 52

2 Answers2

0
import numpy as np
def K(x,z, p_constant=1.0):
  return (np.dot(x.T,z)+p_constant)**2
#...
x=np.arange(100).reshape((10,10))
np.fromfunction(np.vectorize(lambda i,j: K(x[i],x[:,j])), x.shape, dtype=x.dtype)

I had some trouble with np.fromfunction's misleading documentation.

Community
  • 1
  • 1
Felipe Lema
  • 2,700
  • 12
  • 19
0

Yes, the answer on question "How to compute linear kernel matrix" is

np.dot(X , np.transpose(X). 
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158