Implementing the (polynomial) "kernel trick" in matlab

14 次查看(过去 30 天)
Given p = 2 vectors x in R^n, y in R^n, how to compute the kernel of order m > 2, which is of the form
A = [ones(n,1), x, y, x.*y, x.^2, y.^2, x.^2 *y, ..., x.^m, y.^m]
q = nchoosek(p+m, m)
A in R^(nxq)
For example, given
x=[1 2]'
y=[2 1]'
m = 3;
Output:
A = [1 1 2 1 4 1 8;1 2 1 4 1 8 1]

回答(1 个)

Jaynik
Jaynik 2024-11-6,9:39
Hi Ashwin,
You are on the right path to implement the polynomial kernel trick. Here is a sample code that can be further enhanced to code the solution:
function A = polynomial_kernel(x, y, m)
n = length(x);
p = 2; % Since we have two vectors x and y
q = nchoosek(p + m, m);
A = ones(n, q);
col = 2;
for i = 1:m
for j = 0:i
A(:, col) = (x.^(i-j)) .* (y.^j);
col = col + 1;
end
end
end
For each combination of degrees,we need coumpute the term and fill in the matrix A. i represents the total degree of the polynomial term and j represents the degree of y in the term while (i - j) is the degree of x.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Performance Profiling 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by