Reputation: 6290
I want to calculate weighted kernels (for using in a SVM classifier) in Matlab but I'm currently compeletely confused.
I would like to implement the following weighted RBF and Sigmoid kernel:
x and y are vectors of size n, gamma and b are constants and w is a vector of size n with weights.
The problem now is that the fitcsvm method from Matlab need two matrices as input, i.e. K(X,Y). For example the not weighted RBF and sigmoid kernel can be computed as follows:
K_rbf = exp(-gamma .* pdist2(X,Y,'euclidean').^2)
K_sigmoid = tanh(gamma*X*Y' + b);
X and Y are matrices where the rows are the data points (vectors).
How can I compute the above weighted kernels efficiently in Matlab?
Upvotes: 1
Views: 516
Reputation: 66775
Simply scale your input by the weights before passing to the kernel equations. Lets assume you have a vector w
of weights (of size of the input problem), you have your data in rows of X
, and features are columns. Multiply it with broadcasting over rows (for example using bsxfun
) with w
. Thats all. Do not do the same to Y
though, just multiply one of the matrices. This is true for every such "weighted" kernel based on scalar product (like sigmoid); for distance based (like RBF) you want to scale both by sqrt of w
.
f(<wx, y>) = f(w<x, y>) (linearity of scalar product)
f(||sqrt(w)x - sqrt(w)y||^2) = f(SUM_i (sqrt(w_i)(x_i - y_i))^2)
= f(SUM_i w_i (x_i - y_i)^2)
Upvotes: 2