v9.1.1
Menu

Polynomial Kernel

mediumFREESupport Vector MachinesMachine Learning 1

Compute the polynomial kernel matrix between two sets of vectors.

The polynomial kernel is: K(x,y)=(x⋅y+c)dK(x, y) = (x \cdot y + c)^d

Given two sets of vectors AA (m points) and BB (n points), compute the m×nm \times n kernel matrix where entry (i,j)=K(Ai,Bj)(i,j) = K(A_i, B_j).

Return the matrix with values rounded to 4 decimal places.

Example

Input
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
c = 1, d = 2
Output
[[324, 576], [1600, 2916]]
Reasoning
  • Inputs: AA holds m=2m = 2 points and BB holds n=2n = 2 points, each 2-dimensional; c=1c = 1 is the shift and d=2d = 2 the degree. The result is a 2×22 \times 2 grid: row ii for AiA_i, column jj for BjB_j.
  • Dot products: A1⋅B1=1⋅5+2⋅6=17A_1 \cdot B_1 = 1 \cdot 5 + 2 \cdot 6 = 17, A1⋅B2=1⋅7+2⋅8=23A_1 \cdot B_2 = 1 \cdot 7 + 2 \cdot 8 = 23, A2⋅B1=3⋅5+4⋅6=39A_2 \cdot B_1 = 3 \cdot 5 + 4 \cdot 6 = 39, A2⋅B2=3⋅7+4⋅8=53A_2 \cdot B_2 = 3 \cdot 7 + 4 \cdot 8 = 53.
  • Shift and raise: (17+1)2=324(17 + 1)^2 = 324, (23+1)2=576(23 + 1)^2 = 576, (39+1)2=1600(39 + 1)^2 = 1600, (53+1)2=2916(53 + 1)^2 = 2916.
  • Output: [[324, 576], [1600, 2916]]. All inputs are integers, so rounding to 4 decimal places changes nothing here.
  • Notice that KK is not symmetric here because A≠BA \neq B. Squaring stretches gaps: dot products 17 and 53 differ by about 3×3\times, kernel values by 9×9\times, so a higher dd emphasises strong similarities.

Constraints

  • A: 2D list (m x features), B: 2D list (n x features)
  • c: constant term (float), d: degree (integer)
  • Return 2D list (m x n) kernel matrix
  • Round each value to 4 decimal places

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.