📘
Polynomial Kernel
MediumSupport Vector Machines
Compute the polynomial kernel matrix between two sets of vectors.
The polynomial kernel is: K(x,y)=(x⋅y+c)d
Given two sets of vectors A (m points) and B (n points), compute the m×n kernel matrix where entry (i,j)=K(Ai,Bj).
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 A with each vector in B:
- A1⋅B1=(1⋅5)+(2⋅6)=17,
- A1⋅B2=(1⋅7)+(2⋅8)=23,
- A2⋅B1=(3⋅5)+(4⋅6)=39,
- A2⋅B2=(3⋅7)+(4⋅8)=53
- Then, we apply the polynomial kernel formula: K(x,y)=(x⋅y+c)d
- K(A1,B1)=(17+1)2=182=324,
- K(A1,B2)=(23+1)2=242=576,
- K(A2,B1)=(39+1)2=402=1600,
- K(A2,B2)=(53+1)2=542=2916
- The final output is the m×n kernel matrix with the calculated values: [[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
Python 3.13.1
Test Results
0/0Run code to see test results.