Polynomial Kernel SVM: Principles, Examples, and Python Implementation

SVM à Noyau Polynomial : Guide Complet — Principes, Exemples et Implémentation Python

Polynomial Kernel SVM: Complete Guide — Principles, Examples, and Python Implementation

The polynomial kernel SVM generalizes the linear SVM by projecting data into a polynomial feature space of controlled degree. Unlike the RBF kernel which creates local bubbles, the polynomial draws global curved boundaries — ideal when the relationship between classes follows an algebraic geometry.

This guide explores the polynomial kernel SVM in depth: from the theory of the polynomial kernel to practical implementation with scikit-learn.


Mathematical Principle

The Polynomial Kernel

The polynomial kernel is defined by the function:

K(x, x') = (γ · x·x' + r)^d

where:
– x·x’ is the dot product between vectors (linear similarity)
– γ (gamma) is a scaling coefficient (default 1/n_features)
– r (coef0) is the constant term — controls the relative importance of higher-degree vs lower-degree terms
– d (degree) is the polynomial degree — determines boundary complexity

The Kernel Trick

Under explicit projection into a polynomial feature space of degree d, the number of features explodes. For example, with 100 variables and d=3, you would get C(103,3) = 176,851 features. The kernel trick computes similarities directly in the original space — the computation remains O(n), not O(C(n,d)).

Dual Formulation

The optimization problem is identical to the linear SVM but with the polynomial kernel:

max Σαᵢ - ½ ΣΣ αᵢαⱼyᵢyⱼK(xᵢ, xⱼ)

subject to: 0 ≤ αᵢ ≤ C and Σαᵢyᵢ = 0. The decision function becomes:

f(x) = sign(Σ αᵢyᵢK(xᵢ, x) + b)

The Role of the Degree d

  • d = 1: the linear SVM (K = γ·x·x’ + r)
  • d = 2: quadratic boundaries (parabolas, ellipses, hyperbolas)
  • d = 3: cubic curves — increased flexibility, risk of overfitting
  • d ≥ 4: very high capacity — often excessive except for very structured patterns

The coef0 Parameter (r)

  • r = 0: homogeneous polynomial kernel
  • r > 0: non-homogeneous kernel — adds a bias that gives more weight to lower-degree terms when ||x·x’|| is small

Intuition — How to Understand It?

The linear SVM can only draw a straight line. The polynomial kernel draws algebraic curves whose shape is controlled by the degree:

  • Degree 2: parabolas, circles, ellipses — often sufficient for XOR-type problems
  • Degree 3: curves can invert, create S-shapes — more freedom but watch for overfitting
  • Role of gamma: a high gamma makes boundaries tighter, a low gamma smoothes them
  • Role of coef0: vertical shift of the polynomial — with r=0, a point at the origin is always on the same side

Quick comparison:
– Linear: straight line — simple, fast, but limited
– Polynomial: algebraic curve of fixed degree — controllable, interpretable
– RBF: infinitely complex local bubbles — powerful but opaque

Python Implementation — Complete Example

Example 1: Classification on Circle Data

import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.datasets import make_circles
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from sklearn.preprocessing import StandardScaler

# Non-linearly separable data
X, y = make_circles(n_samples=500, factor=0.3, noise=0.1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Standardization essential for SVMs
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Polynomial SVM degree 3
svc_poly = SVC(kernel='poly', degree=3, gamma='scale', coef0=1, C=1.0)
svc_poly.fit(X_train, y_train)

y_pred = svc_poly.predict(X_test)
print(f"Accuracy (poly d=3) : {accuracy_score(y_test, y_pred):.4f}")
print(classification_report(y_test, y_pred, target_names=["Interior", "Exterior"]))

# Comparison of 3 kernels
for name, model in [("Linear", SVC(kernel='linear', C=1)),
                    ("RBF", SVC(kernel='rbf', gamma='scale', C=1)),
                    ("Poly d=3", svc_poly)]:
    model.fit(X_train, y_train)
    acc = accuracy_score(y_test, model.predict(X_test))
    print(f"{name:15s} -> {acc:.4f}")

Example 2: Impact of Degree on the Boundary

degrees = [1, 2, 3, 4, 5, 6]
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
axes = axes.flatten()

for i, d in enumerate(degrees):
    svc = SVC(kernel='poly', degree=d, gamma='scale', coef0=1, C=1.0)
    svc.fit(X_train, y_train)

    x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
    y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200),
                         np.linspace(y_min, y_max, 200))
    Z = svc.decision_function(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)

    axes[i].contourf(xx, yy, Z, levels=20, cmap='RdBu', alpha=0.6)
    axes[i].scatter(X_train[:, 0], X_train[:, 1], c=y_train,
                    cmap='coolwarm', edgecolors='k', s=30)
    tr = accuracy_score(y_train, svc.predict(X_train))
    te = accuracy_score(y_test, svc.predict(X_test))
    axes[i].set_title(f"Degree {d} (train={tr:.0%}, test={te:.0%})")
    axes[i].set_aspect('equal')

plt.suptitle("Impact of degree - Polynomial SVM")
plt.tight_layout()
plt.show()

Example 3: Grid Search and Overfitting

from sklearn.model_selection import GridSearchCV

param_grid = {
    'C': [0.1, 1, 10],
    'degree': [2, 3, 4],
    'gamma': ['scale', 0.1, 1],
    'coef0': [0, 1, 2]
}

grid = GridSearchCV(SVC(kernel='poly'), param_grid, cv=5, n_jobs=-1)
grid.fit(X_train, y_train)
print(f"Best params: {grid.best_params_}")
print(f"CV accuracy: {grid.best_score_:.4f}")
print(f"Test accuracy: {grid.score(X_test, y_test):.4f}")

# Overfitting curve by degree
for d in range(1, 11):
    svc = SVC(kernel='poly', degree=d, gamma='scale', coef0=1, C=1.0)
    svc.fit(X_train, y_train)
    tr = accuracy_score(y_train, svc.predict(X_train))
    te = accuracy_score(y_test, svc.predict(X_test))
    print(f"Degree {d:2d} -> train={tr:.4f}, test={te:.4f}")

Hyperparameters

Hyperparameter Role Typical Values Impact
C Inverse regularization 0.01, 0.1, 1, 10, 100 Small C = wide and tolerant margin. Large C = penalizes errors (overfitting).
degree Polynomial degree d 2 (default), 3, 4, 5 Controls curvature. d=1 = linear. The higher d, the more complex the model.
gamma Kernel scale ‘scale’ (default), ‘auto’, 0.01-10 gamma = 1/(n_features × variance) if ‘scale’. High gamma = complex boundaries.
coef0 Constant term r 0 (default), 0.5, 1, 2 r=0 = homogeneous. r>0 gives weight to lower-degree terms.
kernel Kernel type ‘poly’ For polynomial: SVC(kernel=’poly’, degree=d).
max_iter Iteration limit -1 (unlimited), 1000 Increase if non-convergence for high degrees.

Degree selection guide:
– d = 2: starting point, handles XOR and quadratic separations
– d = 3: inversions and complex curvatures
– d ≥ 4: rarely useful, prefer RBF

Advantages and Limitations

Advantages

  • Controlled and interpretable non-linear boundaries
  • Kernel trick avoids feature explosion
  • Good alternative to RBF for regular geometric structures
  • Less prone to overfitting than RBF for small degrees
  • Degree and coef0 offer intuitive control

Limitations

  • Difficult degree choice — grid search required
  • Slow convergence for d ≥ 4
  • Less flexible than RBF (constrained to algebraic geometry)
  • Standardization mandatory
  • Computationally expensive: O(n²) — practical limit around 100,000 points

Use Cases

1. Geometric Shape Recognition

When data follows regular patterns (ellipses, parabolas), the degree-2 polynomial kernel SVM is optimal and interpretable.

2. Image Classification

Interactions between descriptors (color × texture) are naturally polynomial. A degree 2-3 captures them without explicit computation.

3. Bioinformatics — Protein-Ligand Interactions

Molecular interactions have polynomial dependencies between structural descriptors.

4. Finance — Non-linear Credit Scoring

When risk increases quadratically with the debt-to-income ratio, degree 2 captures this non-linearity better than a linear model.

Best Practices

  • Always standardize (StandardScaler mandatory)
  • Start with d=2 — handles most cases
  • Grid Search on all 4 parameters (C, degree, gamma, coef0)
  • Monitor the train/test gap for high degrees
  • Systematically compare with RBF

See Also

In-Depth Comparison: Polynomial vs RBF vs Linear

Choosing the kernel is one of the most important decisions when using an SVM. Here is a detailed comparative guide to help you choose correctly between the different options available in scikit-learn.

When to Use Each Kernel

Linear kernel (kernel=’linear’):
– Linearly separable or nearly linearly separable data
– High-dimensional text classification (thousands of features)
– When coefficient interpretability is crucial
– Large data volumes (more than 50,000 samples)

Polynomial kernel (kernel=’poly’):
– Boundaries with regular geometry (curves, circles, ellipses)
– When you want to control complexity via the degree
– Feature interactions that you know a priori
– Problems where degree interpretability has business meaning

RBF kernel (kernel=’rbf’):
– Complex and irregular boundaries
– No a priori knowledge of geometric shape
– When precision takes priority over understanding
– Moderate-sized data (less than 50,000 samples)

Performance Comparison Table

On the Make_circles dataset (non-linearly separable data):

Kernel Train Test Speed Interpretability
Linear 52% 50% Fast Excellent
Poly d=2 95% 94% Moderate Good
Poly d=3 97% 95% Moderate Average
Poly d=6 100% 89% Slow Low
RBF 99% 96% Moderate Low

We observe that the polynomial kernel of degree 2 or 3 rivals the RBF while remaining interpretable, whereas degrees that are too high (d greater than or equal to 6) clearly overfit.

Algorithmic Complexity

  • Training: O(n squared multiplied by d) for the polynomial kernel, where d is the degree and n is the number of samples
  • Prediction: O(n_sv multiplied by d) where n_sv is the number of support vectors
  • Memory: O(n squared) for the kernel matrix — practical limit around 100,000 samples

Best Practices for the Polynomial Kernel SVM

  1. Always standardize your data with StandardScaler before training a polynomial kernel SVM. Non-standardized features distort the underlying dot product.
  2. Always start with d=2: this is the most useful degree in practice. It handles most simple non-linear separations.
  3. Perform a mandatory Grid Search on the 4 hyperparameters (C, degree, gamma, coef0). Their interaction is complex and counter-intuitive.
  4. Monitor overfitting: the gap between train and test scores explodes for high degrees with little data. Use cross-validation.
  5. Systematically compare with the RBF: always include SVC(kernel=’rbf’) in your benchmark. The RBF will often win, but the polynomial kernel can be preferable for its interpretability.
  6. Limit degree to a maximum of 5: beyond that, convergence becomes slow and overfitting is almost certain. If you need more complexity, switch to the RBF.