Implementing the (polynomial) "kernel trick" in matlab
조회 수: 7 (최근 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]
댓글 수: 0
답변 (1개)
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!