The dot product of two lifted points can often be computed in the original space, at a fraction of the cost. Derive it for the quadratic map, meet the polynomial and RBF kernels and their Gram matrices, and learn the test that tells a true kernel from an impostor.
ML02.8.2L2 Intermediate25 minIn review (what this means)
The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.
Derive that the quadratic map satisfies phi(x) . phi(z) = (x . z)^2 and check it with numbers.
Define a kernel and its Gram matrix, and compute Gram matrices for the polynomial and RBF kernels by hand.
Explain the RBF kernel as a similarity with a length scale, and write out its infinite feature map for one feature.
Test a candidate kernel by the eigenvalues of its Gram matrices, and prove that a sum of kernels is a kernel.
The question
The last lesson lifted the data to make a straight cut work, and found the bill: 5,456 columns for a cubic lift of 30 features, 324,632 for degree 5. Yet the SVM’s dual (ML02.7.2) never looks at a single feature. It only asks for dot products between pairs of points. Could we hand it the dot products of the lifted points without ever building the lifted points?
From the last lesson
1. To what heights does the lift \({z = x_1^2 + x_2^2}\) send the rings, and which plane splits them?
Show answer
The inner points go to height 1 and the outer points to 8 or 9, so a level plane between them, such as \(z = 4.5\), splits them. Back in the plane it is the circle of radius 2.12.
2. How many columns does a polynomial lift of degree \(p\) on \(d\) features have?
Show answer
\({\binom{d + p}{p}}\): 5,456 for \(d = 30\) and \(p = 3\).
Intuition
Take two points, \({\vect{x} = (1, 2)}\) and \({\vect{z} = (3, 1)}\), and the quadratic map \({\varphi(\vect{x}) = (x_1^2, \sqrt{2}\,x_1 x_2, x_2^2)}\) of the last lesson. There are two ways to get the dot product of the lifted points.
ML02.8.2-F1
Two routes to one number. The long route builds both lifted points and multiplies them coordinate by coordinate. The short route takes the dot product in the plane and squares it.
The long route needs the lifted points. The short route never leaves the plane, and it gives exactly the same answer. A function that hands back the dot product in a lifted space, while working only in the original one, is called a kernel. Using one in place of the lift is the kernel trick.
Build it up
Step 1. The long route. Lift each point: \({\varphi(1, 2) = (1,\; 2\sqrt{2},\; 4)}\) and \({\varphi(3, 1) = (9,\; 3\sqrt{2},\; 1)}\). Multiply coordinate by coordinate and add: \({1 \times 9 = 9}\), \({2\sqrt{2} \times 3\sqrt{2} = 12}\) and \({4 \times 1 = 4}\), so the dot product is \(25\).
Step 2. The short route.\({\vect{x} \cdot \vect{z} = 1 \times 3 + 2 \times 1 = 5}\), and \({5^2 = 25}\).
Step 3. Why the \(\sqrt{2}\) is there. Square \({(x_1 z_1 + x_2 z_2)}\) and the cross term comes out twice: \({2\,x_1 x_2 z_1 z_2}\). The lifted dot product has the middle term \({(\sqrt{2}\,x_1 x_2)(\sqrt{2}\,z_1 z_2)}\), which is the same thing. Without the \(\sqrt{2}\) the two routes would disagree.
Step 4. The saving grows with \(d\). With \(d\) features the quadratic map has one coordinate for each square and one for each pair, \({d + d(d - 1)/2 = d(d + 1)/2}\) in all: 465 for \(d = 30\). The short route still costs \(d\) multiplications and one squaring.
Step 5. A kernel is a similarity. The dot product is large when two lifted points point the same way. That suggests designing the similarity directly. The most used choice scores two points by their distance, high when they are close and near zero when they are far apart.
ML02.8.2-F2
Left: the RBF kernel falls from 1 as two points move apart; a larger \(\gamma\) makes it fall faster. Right: the kernel of every pair of ring points, a table of similarities called the Gram matrix.
The formal statement
NoteDefinition: kernel and Gram matrix
A kernel is a function \({k(\vect{x}, \vect{z})}\) for which some feature map \(\varphi\) gives
for all \(\vect{x}\) and \(\vect{z}\). For points \({\vect{x}_1, \dots, \vect{x}_n}\), the Gram matrix (or kernel matrix) \(\mat{K}\) is the \(n \times n\) matrix with entries \({K_{ij} = k(\vect{x}_i, \vect{x}_j)}\).
The quadratic kernel. For \(\vect{x}\) and \(\vect{z}\) in \(\R^d\), expand the square of a sum as a double sum:
That is \({\varphi(\vect{x}) \cdot \varphi(\vect{z})}\) for the map that lists every square \(x_i^2\) and every product \({\sqrt{2}\,x_i x_j}\). For \(d = 2\) it is the map of the last lesson.
Polynomial kernels. Adding a constant \({c \ge 0}\) and raising to a power \(p\) gives the polynomial kernel
For \({c > 0}\) its feature map holds every monomial of degree up to \(p\), each with a fixed positive weight: the \({\binom{d + p}{p}}\) columns of the last lesson, for the price of one dot product. For \({c = 0}\) only the monomials of degree exactly \(p\) remain, as in the quadratic kernel.
The RBF kernel. The radial basis function (or Gaussian) kernel is
It is 1 when the points coincide and falls towards 0 as they move apart. Writing \({\gamma = 1/(2\ell^2)}\) shows it as a bell curve in the distance with width \(\ell\), the length scale: a large \(\gamma\) is a short length scale, and only close neighbours count as similar.
Go deeper: the RBF kernel's feature map has infinitely many coordinates
Take one feature. Expand the square in the exponent, \({(x - z)^2 = x^2 - 2xz + z^2}\), and split the exponential:
So the RBF kernel is a dot product, but in a space with one coordinate for every power of \(x\): infinitely many. No computer could build that lift; the kernel computes its dot products with one subtraction, one square and one exponential.
ML02.8.2-F3
The series of the deeper block for \({x = 1}\), \({z = 2}\), \({\gamma = 0.5}\). Eight terms already give 0.6059 of the exact 0.6065.
NoteResult: every Gram matrix of a kernel is positive semidefinite
If \({k(\vect{x}, \vect{z}) = \varphi(\vect{x}) \cdot \varphi(\vect{z})}\), then for any points and any numbers \({c_1, \dots, c_n}\), \({\vect{c}\T \mat{K} \vect{c} \ge 0}\).
Proof. Write the quadratic form as a double sum and use the feature map:
The second line pulls the sums apart, which the dot product allows because it is linear in each side. A symmetric matrix with this property is positive semidefinite, and (M03.4) that is the same as having no negative eigenvalue.
The converse also holds. Mercer’s theorem, in the form the CS229 notes give it: a symmetric function \(k\) is a kernel exactly when, for every finite set of points, its Gram matrix is positive semidefinite. So a candidate can be checked without finding its feature map: build Gram matrices and look at their eigenvalues. One negative eigenvalue, on one set of points, rules it out.
Building new kernels. If \(k_1\) and \(k_2\) are kernels, so is \({k_1 + k_2}\).
Proof. Let \({\varphi_1}\) and \({\varphi_2}\) be their maps, and stack them into one longer list, \({\varphi(\vect{x}) = (\varphi_1(\vect{x}), \varphi_2(\vect{x}))}\). A dot product of stacked lists is the sum of the dot products of the parts:
The same works through the Gram matrices: \({\vect{c}\T(\mat{K}_1 + \mat{K}_2)\vect{c}}\) is a sum of two numbers that are each at least 0. A positive multiple \({a\,k_1}\) with \({a > 0}\) is a kernel with the map \({\sqrt{a}\,\varphi_1}\), and a product \({k_1 k_2}\) is a kernel too (exercise 3). These rules build \({(\vect{x} \cdot \vect{z} + c)^p}\) from the plain dot product and a constant.
Worked example
The Gram matrices of three ring points, \((1, 0)\), \((0, 1)\) and \((2, 2)\).
Quadratic kernel. The dot products are \({(1, 0) \cdot (0, 1) = 0}\), \({(1, 0) \cdot (2, 2) = 2}\), \({(0, 1) \cdot (2, 2) = 2}\) and \({(2, 2) \cdot (2, 2) = 8}\), with 1 for each of the first two points with itself. Squaring each:
the product of the eigenvalues. NumPy gives 0.496, 1 and 64.504, which add to 66, multiply to 32, and are all positive.
RBF kernel with \({\gamma = 0.5}\). The squared distances are 2 between the first two points and \({1 + 4 = 5}\) from each of them to \((2, 2)\). So the entries off the diagonal are \({e^{-1} \approx 0.368}\) and \({e^{-2.5} \approx 0.082}\), with 1 on the diagonal. One eigenvector can be read off: the matrix sends \({(1, -1, 0)}\) to (the two entries \(0.082\) in its last row cancel)
Now the twelve ring points under four candidates: the quadratic kernel, the polynomial kernel with \({c = 1}\), the RBF kernel and, as an impostor, the squared distance \({\norm{\vect{x} - \vect{z}}^2}\). scikit-learn’s pairwise module computes the first three.
from sklearn.metrics.pairwise import polynomial_kernel, rbf_kernelR = np.array([(1, 0), (-1, 0), (0, 1), (0, -1), (3, 0), (-3, 0), (0, 3), (0, -3), (2, 2), (2, -2), (-2, 2), (-2, -2)], dtype=float)D2 = ((R[:, None, :] - R[None, :, :]) **2).sum(axis=-1)candidates = {"(x.z)^2": polynomial_kernel(R, degree=2, gamma=1, coef0=0),"(x.z + 1)^2": polynomial_kernel(R, degree=2, gamma=1, coef0=1),"RBF, gamma 0.5": rbf_kernel(R, gamma=0.5),"squared distance": D2,}for name, K in candidates.items(): ev = np.linalg.eigvalsh(K) smallest =round(ev.min(), 3) +0.0# + 0.0 prints a rounding error of -1e-15 as 0.000print(f"{name:17s} smallest eigenvalue {smallest:8.3f} rank {np.sum(np.abs(ev) >1e-9)}")terms = [math.exp(-0.5*1-0.5*4) *1.0** k / math.factorial(k) *2.0** k for k inrange(8)]print("eight terms of the RBF series:", round(sum(terms), 4), " exact:", round(math.exp(-0.5), 4))
The ranks tell a story. The quadratic kernel’s Gram matrix has rank 3, because its lifted points live in three dimensions. With \({c = 1}\) there are six monomials, and rank 6. The RBF kernel’s matrix has full rank 12: its lifted space has room for every point to point its own way. The squared distance has a smallest eigenvalue of \(-72\), so it is no kernel at all.
Explore
This module’s interactive, the kernel lift, lives in lesson 3. Its second half puts an RBF kernel to work: slide \(\gamma\) and watch the boundary follow the length scale.
Pitfalls
ML02.8.2-F5
Wrong beside right. Left: the squared distance looks like a sensible score, but its Gram matrix has negative eigenvalues. Right: the RBF kernel, built from the same distance, passes.
“Any sensible similarity score can be used as a kernel.”
Only one whose Gram matrices are all positive semidefinite. The squared distance fails on the rings, and on even less: for two points at distance \(r\) its Gram matrix is
whose eigenvalues are \(r^2\) and \({-r^2}\). An SVM given such a function is no longer solving a convex problem, and its solver can fail or return nonsense.
“A negative entry in the Gram matrix means the function is not a kernel.”
Entries may be negative: the plain dot product of \((1, 0)\) and \((-1, 0)\) is \(-1\). The test is about the eigenvalues of the whole matrix, not the signs of its entries.
“A bigger \(\gamma\) makes the RBF kernel wider.”
It is the other way round. \(\gamma\) multiplies the squared distance, so a bigger \(\gamma\) makes the kernel fall faster: \({\gamma = 1/(2\ell^2)}\) is an inverse squared length scale. In scikit-learn, gamma sets this same number.
Check yourself
Recall
1. For the quadratic map, what is \({\varphi(\vect{x}) \cdot \varphi(\vect{z})}\) in terms of \(\vect{x}\) and \(\vect{z}\)?
Show answer
\({(\vect{x} \cdot \vect{z})^2}\). The \(\sqrt{2}\) on the cross term supplies the factor 2 that squaring a sum produces.
2. What is a Gram matrix?
Show answer
For points \({\vect{x}_1, \dots, \vect{x}_n}\), the \(n \times n\) matrix of kernel values \({K_{ij} = k(\vect{x}_i, \vect{x}_j)}\): the dot products of all pairs of lifted points.
3. What is the RBF kernel of a point with itself, what happens far away, and what does a larger \(\gamma\) do?
Show answer
It is 1 for a point with itself and falls towards 0 as points move apart. A larger \(\gamma\) makes it fall faster: a shorter length scale.
4. How can you test whether a function is a kernel without finding its feature map?
Show answer
Build its Gram matrix on sets of points and look at the eigenvalues. If one set gives a negative eigenvalue, it is not a kernel. Mercer’s theorem says that if every Gram matrix is positive semidefinite, it is.
5. Why is the sum of two kernels a kernel?
Show answer
Stack the two feature maps into one list; the dot product of stacked lists is the sum of the two dot products. Equivalently, \({\vect{c}\T(\mat{K}_1 + \mat{K}_2)\vect{c}}\) is a sum of two numbers that are each at least 0.
Exercises
1.★ Compute \({(\vect{x} \cdot \vect{z} + 1)^2}\) for \({\vect{x} = (1, 2)}\) and \({\vect{z} = (3, 1)}\). Then check it with the feature map
\({\vect{x} \cdot \vect{z} = 5}\), so the kernel is \({6^2 = 36}\). The lifted points are \({\varphi(\vect{x}) = (1, \sqrt{2}, 2\sqrt{2}, 1, 2\sqrt{2}, 4)}\) and \({\varphi(\vect{z}) = (1, 3\sqrt{2}, \sqrt{2}, 9, 3\sqrt{2}, 1)}\). The products of matching coordinates are 1, 6, 4, 9, 12 and 4, which add up to 36.
2.★★ Show that \({k(\vect{x}, \vect{z}) = \vect{x} \cdot \vect{z} - 1}\) is not a kernel.
Show solution
Take the single point \({\vect{x}_1 = \vect{0}}\). Its Gram matrix is the \(1 \times 1\) matrix \({(k(\vect{0}, \vect{0})) = (-1)}\), whose only eigenvalue is \(-1\). With \({c_1 = 1}\), \({\vect{c}\T \mat{K} \vect{c} = -1 < 0}\). A true kernel would give \({\norm{\varphi(\vect{0})}^2 \ge 0}\) there, so no feature map exists.
3.★★★ Let \(k_1\) and \(k_2\) be kernels with feature maps \({\varphi_1}\) (with \(D_1\) coordinates) and \({\varphi_2}\) (with \(D_2\)). Show that \({k_1 k_2}\) is a kernel by writing down a feature map for it.
Show solution
Write \(u_a\) for the coordinates of \({\varphi_1}\) and \(v_b\) for those of \({\varphi_2}\). Let \(\varphi\) list all \({D_1 D_2}\) products \({u_a(\vect{x})\,v_b(\vect{x})}\), one for each pair \((a, b)\). Then
The second line groups the factors with index \(a\) and those with index \(b\); a double sum of products that split this way is the product of two single sums.
Summary card
A kernel is the dot product of a lift, computed without the lift.
Polynomial.\({(\vect{x} \cdot \vect{z} + c)^p}\), all monomials up to degree \(p\).
RBF.\({e^{-\gamma \norm{\vect{x} - \vect{z}}^2}}\), an infinite lift; \({\gamma = 1/(2\ell^2)}\).
The test. Every Gram matrix positive semidefinite (Mercer); sums, products and positive multiples of kernels are kernels.
Sources and next steps
Sources. Each was opened and checked against this lesson on 15 November 2026.
Tengyu Ma and Andrew Ng, CS229 Lecture Notes, Stanford University, Sections 5.3 and 5.4. Defines the kernel as the inner product of a feature map (equation 5.10), shows that \({(\vect{x}\T\vect{z})^2}\) is the kernel of the map of all products \({x_i x_j}\) and costs \(O(d)\) instead of \(O(d^2)\), treats \({(\vect{x}\T\vect{z} + c)^2}\) and the Gaussian kernel with its infinite-dimensional feature map, proves that a kernel matrix is positive semidefinite, and states Mercer’s theorem in the finite form used here.
Kilian Weinberger, Lecture 14: Kernels continued, Cornell CS4780 (Fall 2018). Lists the rules that build well-defined kernels from others, including positive multiples, sums, products and the exponential of a kernel.
scikit-learn 1.9 User Guide, Section 1.4.6, Kernel functions. Gives the linear, polynomial \({(\gamma\langle x, x'\rangle + r)^d}\), RBF \({\exp(-\gamma\norm{x - x'}^2)}\) and sigmoid kernels with their parameter names gamma, coef0 and degree.
Next.ML02.8.3 Kernel SVMs swaps the dot product in the SVM’s dual for a kernel and lets the boundary bend.