Kernel Ridge Regression
Ridge regression gives us a way to control a fit, but its original features may not describe the relationship well. Kernel comparisons let us apply the same idea in a transformed feature space.
15 sections · 15 questions · 4 coding challenges
The arrow keys move between sections too.
Could We Keep Ridge Regression and Give It Richer Features?
BOTIE
Saunders, Gammerman and Vovk's 1998 work described ridge regression in terms of dual variables. This expresses the solution through relationships between training observations, opening the way to use a kernel in place of explicitly listed transformed features.
For a new observation, the prediction combines its kernel comparisons with the training examples. The fitted coefficients determine how those comparisons contribute, and the ridge penalty still restrains the solution. This lets us fit a nonlinear relationship in the original inputs while retaining the regularisation idea. It also changes the cost: working with comparisons between every pair can become expensive as the dataset grows.
Sources for this history
Build a Curve from Similarities to the Training Points
A straight regression line cannot follow a relationship that rises and falls. Richer features can help, but there may be too many transformed coordinates to build explicitly.
Kernel ridge compares observations in a richer representation, then restrains the fitted combination so it cannot chase the sample freely.
Start with a shape a line cannot follow
The observations form an arc. A straight candidate leaves systematic errors, even if we move or tilt it. We need a different family of prediction shapes.
Vertical: Target
Horizontal: Input
The kernel supplies relationships; ridge fits the combination
The worked calculation builds a training comparison table and solves for coefficients without explicitly constructing the corresponding feature columns.
The lesson distinguishes the SDK's offset convention from other formulations and evaluates actual predictions.
Explore the full example
Compare the ordinary and kernel-based fits. Vary the penalty and watch how closely the prediction follows individual training points.
Part 1. The Line Written Two Ways
1. Ridge as the penalty page left it
The ridge page fits a line to the thrown ball and to the crowd by choosing one weight per feature, and it chooses the weights by solving a system as wide as there are features. On the fifteen throws there is one feature, the time, and the line that comes out scores an R² of 0.003, since the ball goes up and comes down and a line can do only one of those.
On the fifteen people the line scores 0.890 with a slope of 0.826 kilograms per centimetre. Nothing on this page changes what ridge minimises.
minimise ‖y − Xw‖² + λ‖w‖²w = (XᵀX + λI)⁻¹ Xᵀ yThe matrix being inverted is p by p, where p counts the features. That is the fact to hold onto, because the whole page is about finding a second way to write w whose matrix is n by n, where n counts the rows, and then noticing what the second way lets us do that the first cannot.
Keep in mind
2. The same line written as a sum over rows
Ridge’s answer can be pushed through to a second form. Instead of one weight per feature it becomes one weight per training row, and the fitted w is the sum of the rows, each multiplied by its own weight. The two forms describe the same line, and the derivation is three lines of algebra with one honest condition on it, which section 17 returns to.
w = Xᵀ (XXᵀ + λI)⁻¹ y = Xᵀ aa = (K + λI)⁻¹ y, K = XXᵀThe vector a is called the dual weights, and it has one entry per row. The matrix K is n by n and holds the inner product of every training row with every other, and it is called the Gram matrix. Nothing else about the rows survives into the solve. A prediction is then an inner product too, the new row against each training row, weighted by a.
prediction(x) = xᵀw = Σᵢ aᵢ (x · xᵢ) + ȳ›Why the two forms agree
| step | why |
|---|---|
| Xᵀ(XXᵀ + λI) = (XᵀX + λI)Xᵀ | multiply out both sides; each is XᵀXXᵀ + λXᵀ, because matrix multiplication associates |
| (XᵀX + λI)⁻¹ Xᵀ(XXᵀ + λI)(XXᵀ + λI)⁻¹ = Xᵀ(XXᵀ + λI)⁻¹ | multiply the left side by the first inverse on the left and the second on the right |
| (XᵀX + λI)⁻¹ Xᵀ = Xᵀ (XXᵀ + λI)⁻¹ | the same two multiplications on the right side; both inverses exist for any λ above zero |
The condition is that X is what the derivation says it is, a matrix of centred columns with no intercept column, since the intercept is exempt from the penalty on the left and there is no way to exempt one row of K on the right. Centring both the columns and the target does the intercept’s job instead.
Keep in mind
3. Where the features went
Take the three people at 169, 170 and 171 centimetres, weighing 64, 70 and 76 kilograms. Centred on their mean of 170 the heights are −1, 0 and 1, and the Gram matrix is the three by three table of their products, which is the only thing the solve ever reads about them. The middle row and column are all zero, because the middle person is at the mean and has no centred height to multiply.
…
Switch to the radial basis kernel and the same three people produce a different table, with ones down the diagonal, 0.368 for the two pairs a centimetre apart and 0.018 for the pair two centimetres apart. The rows have not changed. What changed is the function that turns two rows into one number, and that function is what the rest of the page varies.
The fifteen throws make a fifteen by fifteen table, and drawn as a heat map the three kernels look nothing alike. The linear kernel’s table is the outer product of one column with itself, bright in the corners and zero along the middle where the centred time passes through nought; the radial one is a band along the diagonal, each throw similar to its neighbours in time and to nothing far away.
…
The eigenvalues under each table say how many directions the table can tell apart. Under the linear kernel fourteen of the fifteen are zero to within rounding, the largest at about 4e−15, and one is 22.834; a rank-one table can fit exactly one direction, which is a line. The squared polynomial kernel has three nonzero eigenvalues and the radial kernel has fifteen, its smallest 8.1e−9.
Keep in mind
4. The identity holds on the throw, to the number
A derivation is a claim, and the fifteen throws are where it is checked. Fit ridge regression feature by feature and the dual form row by row under the plain inner product, at the same penalty, and compare everything that can be compared.
At a penalty of one, ridge reports a slope of 0.261135 and the dual weights, added up through the centred times, give 0.261135. Sampled at 81 times across the throw the two lines part by at most 3.7e−14, both score 0.002588, and both answer 12.76 metres at two seconds, which is the mean height because two seconds is the mean time. Drag the penalty through six orders of magnitude and the two columns stay equal to the last printed digit.
What the control is for
Under the radial basis kernel there is no independent right answer to compare a kernel fit against, so a bug that produced a plausible curve would go unseen. Under the linear kernel there is one, and it is ridge exactly. Section 17 shows the bug that this control found, which came within two percent of the right slope and was wrong for a reason.
Part 2. One Number Per Row
5. The dual weights on three people
The three centred heights are minus one, zero and one; their centred weights are minus six, zero and six. With a ridge penalty of one, the primal calculation is short.
slope = Sxy / (Sxx + λ) = 12 / (2 + 1) = 4The dual system must recover the same slope through three coefficients, one per training person.
(K + λI) a = y_centredK + I = [[2, 0, −1], [0, 1, 0], [−1, 0, 2]]The three dual weights
The middle equation fixes the middle coefficient at zero. The outer two equations can then be solved together.
a₂ = 0
2a₁ − a₃ = −6
−a₁ + 2a₃ = 6
solution: a₁ = −2, a₂ = 0, a₃ = 2The fit returns those three coefficients. Recover the ordinary slope by multiplying each coefficient by its training person’s centred height and adding.
(−2)(−1) + (0)(0) + (2)(1) = 4Use the recovered slope to predict half a centimetre above the mean height.
prediction = mean weight + slope × centred height
= 70 + 4 × 0.5 = 72 kgBoth formulations give this result.
The person at the mean carries a weight of zero and contributes nothing to any prediction, and the two outer people carry equal and opposite weights. A row at the centre of the data tells a straight fit nothing about its slope, and the dual form says so directly where the per-feature form never mentions individual rows at all.
6. A dual weight is a residual divided by the penalty
There is a plainer reading of what each weight is, and it holds under every kernel. Rearrange the system that defines the weights and the left side becomes the fitted values, so the weights are the misses of the fit, each divided by the penalty.
K a + λ a = y_centred ⇒ λ a = y_centred − K a = residualsaᵢ = residualᵢ / λOn the three people at a penalty of one the fitted weights are 66, 70 and 74 kilograms, the residuals are −2, 0 and 2, and dividing by one gives the dual weights back. On the fifteen throws the ledger lists every row.
Under the linear kernel at a penalty of one the last two columns agree to 3.2e−14, and under the radial kernel to 2.7e−15. Drag the penalty down and something worth noticing happens under the linear kernel. The line barely moves, since its score is 0.002593 at a thousandth and 0.002588 at one, while the largest dual weight goes from 12.08 to 12,105, growing as one over the penalty because the residuals stay where they are and the divisor shrinks.
Keep in mind
7. What the model keeps instead of coefficients
Ridge regression on the crowd keeps three numbers, a weight for height, an intercept, and nothing else, and “0.826 kilograms per centimetre” is a sentence about height. The dual model keeps every training row and a weight for each, so on fifteen people it keeps fifteen heights and fifteen weights, and a prediction is a sum over all of them.
There is no per-feature number to report. It is possible to add the weights up through the centred heights and recover the slope, as section 5 did, only because under the linear kernel there is a slope to recover; under the radial kernel there is not, and the playground says so rather than inventing one.
the model = the n training rows + the n dual weights + ȳOn the fifteen throws
A ridge fit keeps two numbers. The kernel fit keeps the fifteen times, fifteen dual weights and the mean height, 31 numbers, and every prediction touches all fifteen rows. Part 5 measures what that costs as the rows grow.
Keep in mind
Check yourself
Questions on Parts 1 and 2
Question 1 of 5
What separates the dual form from ridge’s per-feature form?
Part 3. Swapping the Inner Product for a Kernel
8. The swap, and what stays the same
Everything in Part 1 reads the rows through one function, the inner product of two of them. The kernel trick page argues that any function which is an inner product in some expanded space, whether or not that space is ever built, can stand in for it, and this page takes that as given. Replace the inner product wherever it appears, in the Gram matrix and in the prediction, and nothing else about the fit changes.
K[i, j] = k(xᵢ, xⱼ)a = (K + λI)⁻¹ y_centredprediction(x) = Σᵢ aᵢ k(x, xᵢ) + ȳThe solve is the same solve, the penalty is the same penalty on the same diagonal, and the dual weights are still the residuals over the penalty. What the swap buys is a curve, drawn without a single manufactured column, in the original picture of time against height.
| kernel | what it computes on centred rows | on the throw, penalty 1 |
|---|---|---|
| linear | a · b | R² 0.003, a line |
| polynomial, degree d | (a · b + 1)ᵈ | R² 0.981 at degree 2 |
| radial basis, gamma γ | exp(−γ ‖a − b‖²) | R² 0.873 at gamma 1 |
| sigmoid, gamma γ, constant c | tanh(γ a · b + c) | not always a kernel; section 22 |
The four kernels the playground offers, each read from the fit endpoint on the fifteen throws.
Keep in mind
9. Polynomial kernels and the degree, on the throw
The polynomial kernel raises the inner product plus one to a power, and the plus one is what lets every lower power ride along with the highest. On the fifteen throws the degree does what the polynomial page’s manufactured columns did, except that no column is ever built.
…
Degree 2 follows the arc at 0.981, degree 3 reaches 0.985, and degrees 4, 5 and 6 come out at 0.981, 0.982 and 0.980, so past the square the extra powers buy nothing on this throw at this penalty. What they cost is visible in the condition number of the system, which goes 23.8, 72.4, 262, 1061, 4597 and 20,781 as the degree rises, since the largest entries of the Gram matrix are centred times raised to the degree and the smallest stay near zero.
Keep in mind
10. Degree one with a constant is the linear kernel here
The textbook reading is that the polynomial kernel at degree one is not the linear kernel, because the plus one adds a constant to every entry of the Gram matrix and a constant entry is a feature that is always one. I measured it and found the two fits identical here. On the throw at a penalty of one the two curves part by 2.0e−14, and on the three people by 3.9e−12, which is the arithmetic of a solve and not a difference of model.
K_degree1 = K_linear + 11ᵀ›Why centring makes the constant vanish
The fit centres the rows, so every column of X sums to zero and the Gram matrix K sends the all-ones vector to zero. The fit also centres the target, so y_centred is perpendicular to the all-ones vector. The added 11ᵀ acts only along that vector, and the solve never leaves the space perpendicular to it, so the added term is never touched. Without the centring the textbook reading would hold and the constant would act as a penalised intercept.
Keep in mind
11. The radial basis kernel and gamma, on the throw
The radial basis kernel reads two rows as similar when they are close, one at zero distance and falling toward zero as the distance grows, with gamma setting how fast. The row of the Gram matrix for the throw at two seconds reads 1.0 at itself, then 0.919, 0.723, 0.477, 0.273, 0.129, 0.054 and 0.018 for the throws stepping outward at gamma 1, so each throw is compared mainly with its neighbours in time.
…
At gamma 0.01 every throw looks like every other and the fit is a curve that is nearly straight at 0.012. At 0.1 it reaches 0.450, at 0.3 it follows the arc at 0.797, at 1 it scores 0.873, at 3 it peaks at 0.880, and by 10 each throw is similar only to itself, the smallest Gram eigenvalue has risen from 8.1e−9 at gamma 1 to 0.202, and the curve dips toward the mean between the throws for a score of 0.844.
At a hundred the table is the identity to four decimals and the fit is the mean plus a bump at each throw.
Keep in mind
12. Gamma is in the feature's own units, on the crowd
Gamma multiplies a squared distance, so it carries the inverse square of whatever the feature is measured in. The gamma of 1 that suited times in seconds is a reach of one second; applied to heights in centimetres it is a reach of one centimetre, and the people are three apart.
…
At gamma 1 on the crowd the Gram matrix is the identity to four decimals, the smallest eigenvalue 0.99976, so every dual weight is half that person’s centred weight and the curve visits each person and returns to the mean of 72 between them. Asked about the person at 173 centimetres it answers 74.0, halfway from the mean to their 76 kilograms, and asked about 174.5, between two people, it answers 72.16.
The score is 0.750, below the line’s 0.890. Widen the reach to a hundredth and the score is 0.835; at a thousandth 0.824; at a ten-thousandth the kernel can no longer tell the tallest from the shortest and the score falls to 0.481. Nothing here beats the line, because the crowd was drawn along one.
Keep in mind
13. Three people under three kernels
The three people are small enough that every kernel’s answer can be read whole. Each fit is asked the same question, what a person of 170.5 centimetres weighs, and each answers from its own three dual weights.
| kernel, penalty 1 | dual weights | at 170.5 cm | R² |
|---|---|---|---|
| linear | −2, 0, 2 | 72.000 | 0.889 |
| polynomial, degree 2 | −1.2, 0, 1.2 | 72.400 | 0.960 |
| radial basis, gamma 1 | −3.028, 0, 3.028 | 72.039 | 0.745 |
Every number is read from the fit endpoint on the three people; the middle weight is zero under all three because the middle person is at both means.
The middle weight is zero under every kernel, for the reason section 6 gave. The middle person weighs the mean, every one of these fits passes through the mean, so their residual is zero and so is their weight. The outer weights differ because the kernels differ about how much the outer two people resemble each other, 1 against −1 under the linear kernel and 0.018 under the radial one.
Part 4. The Penalty in the Dual
14. Every diagonal entry takes the penalty
On the ridge page the penalty matrix has a zero in its first slot so the intercept is not shrunk, and the page records the bug that came from zeroing that slot when there was no intercept column to exempt. The dual system has no columns at all. Its matrix is indexed by training rows on both sides, and no row is an intercept, so the penalty is added to every entry of the diagonal with no exemption.
K + λI, every diagonal entry, no exemptionExempting row zero would leave one arbitrary person unpenalised, a fit that still runs, still draws a curve, and is quietly wrong. What does the intercept’s job instead is the centring. Both the rows and the target have their means removed before the solve and the target mean is added back to every prediction, which is why every fit on this page passes through the mean point and why the throw answers exactly 12.76 at two seconds under any kernel.
Keep in mind
15. A small penalty interpolates and ill-conditions
The ridge page describes the penalty as a trade of fit against restraint. In the dual there is a second thing it does that has no counterpart on that page, since the Gram matrix of a radial kernel on distinct rows is positive definite and only barely, its smallest eigenvalue on the throw at gamma 1 being 8.1e−9, and adding the penalty to the diagonal is what makes the solve possible at all.
…
At a penalty of a billionth the radial curve scores 0.999998 on the throws and swings 122 metres from the mean between them, the largest dual weight is 1.6e7 and the condition number of the system is 6.2e8, which is nine digits of accuracy lost from sixteen. At a millionth the score is still 0.99955 with a condition number of 5.6e6 and a swing of 17.8; at a tenth it is 0.987 with a condition number of 57.5 and the curve stays within 11 metres of the mean; at one, 0.873 and 6.65.
›Why the condition number is what it is
Adding a positive penalty to the diagonal shifts every eigenvalue by that amount. The condition number compares the largest shifted eigenvalue with the smallest.
condition number = (largest eigenvalue + λ) / (smallest eigenvalue + λ)On the throw with gamma one, the largest eigenvalue is about 5.650 and the smallest about 8.1 billionths.
at λ = 1: condition number ≈ 6.650 / 1 ≈ 6.65
at λ = 10⁻⁹: condition number ≈ 5.650 / (8.1 × 10⁻⁹ + 10⁻⁹)
≈ 6.2 × 10⁸The linear kernel has fourteen zero eigenvalues and a condition number around 23 billion at the smaller penalty. Its fitted curve still changes very little here because the target lies in the one direction its Gram matrix represents.
Keep in mind
16. A large penalty flattens onto the mean
At the other end the penalty dominates and the curve goes flat. With the dual weights being residuals over the penalty, a large penalty makes every weight small whatever the residuals are, and a prediction is the mean plus a sum of small numbers.
| penalty | R², radial gamma 1 | largest weight | farthest from the mean |
|---|---|---|---|
| 1 | 0.873 | 5.640 | 6.554 |
| 10 | 0.353 | 1.047 | 2.254 |
| 100 | 0.049 | 0.119 | 0.302 |
| 1000 | 0.005 | 0.012 | 0.031 |
The upper end of the sweep on the throws; the largest weight falls by a factor of ten per step, which is the residuals dividing by the penalty.
By a thousand the curve is within three centimetres of the mean everywhere across the throw and the score is 0.005. Under the linear kernel the same thing happens more slowly, its score going from 0.002588 at one to 0.000114 at a thousand, because there was almost nothing to flatten.
Keep in mind
17. Centring both sides, and the control that caught it
The derivation in section 2 holds on centred data. Both the target and the rows have to have their means removed, and when this model was first built here only the target was. The result is the most instructive kind of bug, a fit that still ran, still drew a plausible line and agreed with ridge to within two percent, 4.2841 against 4.3580 on the fixture it was first measured on.
On the crowd the mistake is not subtle at all. The heights are a hundred and seventy centimetres from the origin, so the uncentred Gram matrix is dominated by that offset, and the half-centred fit reports a slope of 0.0046 kilograms per centimetre where both right fits report 0.8259, and answers 72.80 at the mean height where the right lines answer exactly 72.
Drop the penalty to a thousandth and the right fits rise to 0.8262 while the wrong one stays at 0.0046. On the throw, where the times are near the origin, the wrong slope is 0.0742 against 0.2611, which is the closer-looking version of the same failure.
Keep in mind
Check yourself
Questions on Parts 3 and 4
Question 1 of 5
The polynomial kernel at degree one is not the linear kernel, because the plus one adds a constant to every entry of the Gram matrix.
Part 5. What the Fit Costs
18. The cost grows with the rows, timed
The trade the two forms set up is a matrix as wide as the features against a matrix as wide as the rows, and it is a claim about time, so it was timed. Both fits run on drawn data, first with the rows doubling at two features and then with the features doubling at two hundred rows, each timing the median of five runs through the whole fit, boundary checks included.
… timing the fits, which takes a moment the first time
On the machine I measured on, the kernel fit took 0.09 milliseconds at fifty rows and 121 milliseconds at sixteen hundred, roughly a thousandfold for a thirty-twofold increase in rows, which is the n-cubed solve of an n by n system, while ridge stayed between 0.02 and 0.06 milliseconds throughout. Growing the features instead, from two to sixty-four at two hundred rows, took ridge from 0.04 to 0.30 milliseconds and the kernel fit from 1.2 to 3.1, the kernel’s share coming from building the Gram matrix, which touches every feature once per pair of rows.
ridge: solve p × p, kernel ridge: solve n × nKeep in mind
19. The model is the training set
Prediction has the same shape. A ridge prediction multiplies p weights; a kernel prediction computes the kernel between the query and every training row and multiplies n weights, so predicting a hundred rows took 0.095 milliseconds against fifty training rows and 2.85 against sixteen hundred, where ridge stayed near 0.015. What the fitted model has to keep grows the same way, the rows times the features plus one weight per row, 150 numbers at fifty rows and 4800 at sixteen hundred, against ridge’s three.
What that means for saving a fitted model
A saved kernel ridge model is its training rows. There is no smaller description of the fit, since every prediction needs the kernel against each row, and a document that dropped them would silently lose the ability to predict. The support vector machine on the kernel-trick page keeps only the rows that touch its margin, which is the one respect in which it is the smaller model.
Part 6. What the Model Cannot Do
20. No extrapolation beyond the radial reach
A radial kernel is a bump around each training row, and a query far from every row is similar to none of them, so every term in its prediction is near zero and what is left is the mean. The throw was measured for four seconds. Ask the radial fit what the ball is doing at six.
At gamma 1 and a penalty of a tenth the curve answers 7.47 metres at five seconds, already off the last throw, 12.46 at six, 12.758 at seven and 12.759998 at eight, against a mean of 12.76. Two seconds past the data the fit has forgotten the ball entirely and is answering the average height of the throw.
The squared polynomial kernel does the opposite and keeps its shape, answering −19.5 at five seconds, −49.8 at six and −136.5 at eight, which is the polynomial page’s runaway drawn without columns. The line answers 14.33 at eight seconds and would keep the same slope forever.
Keep in mind
21. No coefficient per feature, no intercept to report
Two things ridge reports that this model cannot. A coefficient per feature, for the reason section 7 gave, and an intercept, because there was never an intercept column. What there is instead is the target mean, added back to every prediction, and on the crowd it is 72 kilograms. Under the linear kernel a slope and an intercept can be reconstructed from the weights and the mean, and the centring widget does exactly that to compare against ridge; under any other kernel the reconstruction has nothing to reconstruct.
Keep in mind
22. An indefinite kernel is refused by the solver
Not every function of two rows is a kernel. A kernel’s Gram matrix has no negative eigenvalue on any set of rows, which is Mercer’s condition, and the sigmoid kernel fails it for many settings. The solve uses a Cholesky factorisation, which succeeds exactly when the matrix it is given is positive definite, so the factorisation is also the test, and the fit is refused at the first pivot that is not positive.
On the throws at gamma 1 and a constant of −0.5 the smallest eigenvalue of the Gram matrix is −4.313. At every penalty up to 4.3 the sum with the smallest eigenvalue is negative and the fit is refused, in a message naming Mercer’s condition. At 4.4 the sum is positive, the factorisation goes through, and the fit is accepted with a score of −228, which is a curve far worse than answering the mean; at 5 it scores −4.49, at 10 −0.069, and at 100 it is 0.006, the flat line the penalty forces.
Even at a constant of zero the smallest eigenvalue is −0.878, hidden at a penalty of one, and the accepted fit scores 0.0023.
Keep in mind
23. A zero penalty has no answer on a singular Gram matrix
Under the linear kernel on the fifteen throws the Gram matrix has one nonzero eigenvalue and fourteen at zero, because fifteen rows of one centred feature span one direction. Without the penalty the system to be solved is that rank-one table, which has no unique solution, and under the squared polynomial kernel twelve of the fifteen eigenvalues are zero for the same reason. The radial kernel’s table has no exact zero, its smallest at 8.1e−9, and section 15 showed what solving near it costs.
λ = 0: K a = y_centred has no unique a when K is singularA penalty of zero is refused when the model is constructed, before any data is seen, and this page refuses it a layer earlier still. The sweep in section 15 stops at a billionth for the same reason, since that is where the solve still completes and the condition number is already 6.2e8.
Keep in mind
Part 7. Implementation and Failure Contracts
24. What a complete implementation must specify
A complete implementation states which kernel and which of its parameters, whether it centres the rows and the target and where the target mean is put back, that the penalty is added to every diagonal entry, which solver factorises the system and what it does when the factorisation fails, whether the dual weights are exposed and that they are not coefficients, that the training rows are kept and are part of the saved model, how the query features are matched to the fitted ones, and the smallest penalty it will accept.
The model here centres both, adds the penalty everywhere, solves by Cholesky, keeps the rows, matches features by name, exposes the dual weights and the training rows as copies, and refuses a penalty that is not positive at construction. Every row of the table below was run against it.
25. The edges, probed
| the edge | the behaviour |
|---|---|
| empty data, or a missing or non-finite value | refused at the boundary, in the same words every feature here is refused with; this page refuses fewer than two points a layer earlier. |
| one row | accepted by the model; the centred row is zero, its dual weight is zero, and every prediction is the one target value. The page refuses it a layer earlier, before the fit. |
| two rows | accepted; on the first two throws the radial fit scores 0.144 with weights of ±2.082. |
| a constant column | accepted by the kernel model, whose Gram matrix is all zeros so the weights are the centred targets over the penalty and every prediction is the mean; this page refuses it, because the ridge control fitted beside it refuses a zero-variance column. |
| a constant target | fits, with every dual weight exactly zero and every prediction the constant; the score is refused as undefined, since R² divides by the target's variance. |
| two rows at the same input | accepted; the Gram matrix has two equal rows and a zero eigenvalue at 2.4e−17, and the penalty makes the system solvable. |
| a penalty of zero or below, a gamma of zero, a negative polynomial constant | refused at construction, by the model's own field bounds, before any data is seen. |
| a sigmoid kernel whose Gram matrix is indefinite | refused at the solve, when the Cholesky factorisation meets a non-positive pivot, with a message naming Mercer's condition; a penalty larger than the negative eigenvalue lets it through, documented in section 22. |
| a polynomial kernel overflowing on large raw inputs | at inputs near 1e5 and degree 6 the table loses positive definiteness to rounding and is refused with the Mercer message, which names the wrong cause; only at absurd magnitudes does the non-finite guard fire and name overflow. Documented rather than defended. |
| unfitted use | reading the dual weights, the training rows or a prediction before fit is refused as not fitted, in a sentence rather than as an attribute error. |
| a query with a feature missing, renamed or added | refused; prediction demands exactly the fitted feature names, in any order. |
| more than a hundred points, or a curve range that runs backward | refused with the limit named, a layer before any fit. |
The constant column deserves the extra sentence, because the two models on this page treat it oppositely. The kernel model alone accepts it and answers the mean everywhere, since with no spread in the column there is nothing for a kernel to compare; the ridge control fitted beside it for the comparison refuses, so the page’s endpoint refuses, and the refusal belongs to ridge rather than to the dual.
Check yourself
Questions on Parts 5 to 7
Question 1 of 5
What did the timing find as the rows doubled at two features?
Try it with Botie
Build with Botie
Write and run Python