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]
0 件のコメント
回答 (1 件)
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Performance Profiling についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!