Implementing the (polynomial) "kernel trick" in matlab

14 Ansichten (letzte 30 Tage)
Ashwin Renganathan
Ashwin Renganathan am 12 Dez. 2017
Beantwortet: Jaynik am 6 Nov. 2024 um 9:39
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]

Antworten (1)

Jaynik
Jaynik am 6 Nov. 2024 um 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!

Kategorien

Mehr zu Performance Profiling finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by