Implementing the (polynomial) "kernel trick" in matlab

조회 수: 7 (최근 30일)
Ashwin Renganathan
Ashwin Renganathan 2017년 12월 12일
답변: Jaynik 2024년 11월 6일
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일
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에서 Polynomials에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by