Transform kernel function into matrix operations?

76 Views Asked by At

I have this kernel function that applies to matrix $X_{N,F}$ where N is the number of samples and F is the number of features. $w$ is a vector of weights with length of N. $$\phi(i,k) = \sum_{j=1}^{N} w(j)*K(X(i,k), X(j,k))$$

where $K(\cdot , \cdot)$ is any nonlinear kernel function that takes two $\mathbb{R}$ and output $\mathbb{R}$. Gaussian kernel Now I want to efficiently calculate this kernel matrix $\phi$ in sklearn and in numpy the best way is to transform this into matrix operations. But I haven't found a way to transform this element-based operation into a matrix-based operation. It is iterating through j rather than k(which will make the problem trivial). Any ideas?

1

There are 1 best solutions below

0
On

The operator $K$ is undefined. Does $K$ map $\mathbb{R}^{N\times F}\mapsto\mathbb{R}$?

If you actually want a product, then the operation reduces to a dot product $$ \sum_{j=1}^{N} w(j) \, \mathbf{X}_{r,c} \mathbf{X}_{j,c} = \mathbf{X}_{r,c} \sum_{j=1}^{N} w(j) \mathbf{X}_{j,c} = \mathbf{X}_{r,c} w(j) \, \mathbf{1} \cdot \mathbf{X}_{j,c} $$ where the vector $\mathbf{1}$ has length $N$.