PIXELBANKv8.2.1
Menu

Polynomial Kernel

Compute the polynomial kernel matrix between two sets of vectors.

The polynomial kernel is: K(x,y)=(xy+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:
  • First, we calculate the dot product of each vector in AA with each vector in BB:
    • A1B1=(15)+(26)=17A_1 \cdot B_1 = (1 \cdot 5) + (2 \cdot 6) = 17,
    • A1B2=(17)+(28)=23A_1 \cdot B_2 = (1 \cdot 7) + (2 \cdot 8) = 23,
    • A2B1=(35)+(46)=39A_2 \cdot B_1 = (3 \cdot 5) + (4 \cdot 6) = 39,
    • A2B2=(37)+(48)=53A_2 \cdot B_2 = (3 \cdot 7) + (4 \cdot 8) = 53
  • Then, we apply the polynomial kernel formula: K(x,y)=(xy+c)dK(x, y) = (x \cdot y + c)^d
    • K(A1,B1)=(17+1)2=182=324K(A_1, B_1) = (17 + 1)^2 = 18^2 = 324,
    • K(A1,B2)=(23+1)2=242=576K(A_1, B_2) = (23 + 1)^2 = 24^2 = 576,
    • K(A2,B1)=(39+1)2=402=1600K(A_2, B_1) = (39 + 1)^2 = 40^2 = 1600,
    • K(A2,B2)=(53+1)2=542=2916K(A_2, B_2) = (53 + 1)^2 = 54^2 = 2916
  • The final output is the m×nm \times n kernel matrix with the calculated values: [[324,576],[1600,2916]][[324, 576], [1600, 2916]]

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
Editor

Test Results

0/0
Run code to see test results.