Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Kernel PCA and local binary patterns

Kernel PCA: a nonlinear extension of PCA using the kernel trick for dimensionality reduction; Local Binary Patterns (LBP): a simple local texture descriptor producing binary codes and histograms for image texture analysis.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Kernel PCA is used when data are not linearly separable in the original feature space: it finds nonlinear principal directions by implicitly mapping data to a high-dimensional space using a kernel. Engineers use Kernel PCA for nonlinear dimensionality reduction, denoising and visualization of complex features before feeding them to classifiers. Local Binary Patterns (LBP) is used in computer vision for fast texture description and face recognition; it is robust to monotonic gray-scale changes and becomes rotation-invariant only when a rotation-invariant mapping is applied. It converts local neighbourhood appearance into compact histograms suitable as features.

Key ideas

Start from linear PCA: given centered vectors x_i in R^d, Principal Components are orthonormal directions u that maximise projected variance u^T S u subject to u^T u = 1, where S is the data covariance matrix. Solve S u = λ u.

Kernel PCA: extend PCA to a nonlinear mapping φ: X -> F (feature space, possibly infinite-dimensional). We do not compute φ explicitly. Define the kernel function k(x, y) = ⟨φ(x), φ(y)⟩. We want principal components v in F solving the eigenproblem in F: C v = λ v where C = (1/n) Σ_i φ(x_i) φ(x_i)^T is the covariance operator in F. Any solution v lies in span{φ(x_1), ..., φ(x_n)}; therefore write v = Σ_j α_j φ(x_j). Substituting into the eigenproblem and taking inner product with φ(x_i) gives the dual eigenproblem on the n×n kernel matrix K with entries K_ij = k(x_i, x_j): K α = n λ α. Thus we compute eigenvectors α of the centered kernel matrix.

Because PCA requires centered data in feature space, we must center the kernel matrix. If K is the uncentered kernel matrix, let H = I_n - (1/n) 1_n 1_n^T be the centering matrix. Then the centered kernel matrix is K_c = H K H. Use K_c in the eigenproblem K_c α = n λ α.

Once eigenpairs (α, λ) are found, the principal component direction in feature space is v = Σ_j α_j φ(x_j). To get the coordinate (projection) of any (centered) sample x onto this component: compute the centered kernel vector with entries k_c(x_i, x) = k(x_i,x) - (1/n)Σ_j k(x_j,x) - (1/n)Σ_j k(x_i,x_j) + (1/n^2)Σ_{p,q} k(x_p,x_q), and take projection value y = v · φ(x) = Σ_j α_j k_c(x_j, x). For training points x_i this reduces to entries of K_c α (scaled by normalization). Normalise α so that v has unit norm in F, i.e. v·v = α^T K_c α = 1.

Remarks and assumptions:

  • Mercer condition: to be a valid kernel, k must be symmetric and positive semi-definite (PSD) on any finite sample. Many common kernels satisfy this (linear, polynomial, Gaussian/RBF; sigmoid only under certain parameter ranges).
  • Kernel PCA finds directions in F; reconstructing original-space approximations (pre-image) is non-trivial — this is the pre-image problem.
  • Choice of kernel and hyperparameters (RBF width, polynomial degree) is critical.

Local Binary Patterns (LBP): For each pixel, compare the intensity of P sampling points on a circle of radius R around the centre pixel g_c. Let the sampled neighbour values be g_p for p = 0..P-1. Define the sign function s(t) = 1 if t >= 0 else 0. Then the LBP code (standard) is LBP_{P,R} = Σ_{p=0}^{P-1} s(g_p - g_c) · 2^p. This gives an integer in [0, 2^P - 1]. Collect LBP codes over a region and build a histogram; this histogram is the texture descriptor. Variants: circular sampling with interpolation, rotation-invariant mapping, and the concept of uniform patterns (patterns with at most two transitions 0↔1 in the circular bit string) which reduces dimensionality and often improves robustness. For P=8, the 'u2' (uniform) mapping yields 59 histogram bins (58 uniform patterns plus one for all non-uniform); the rotation-invariant uniform mapping riu2 yields P+2 bins (e.g., 10 for P=8).

What to draw in an exam for Kernel PCA:

  • Draw a 2D dataset not linearly separable (e.g., a circle of points) in input space. Then draw a curved arrow to a higher-dimensional feature space where points become linearly separable. Draw a straight line (principal axis) in the feature space and show its projection back to input space (a nonlinear curve). Label: x, φ(x), kernel k(x,y), principal component v in F.

What to draw for LBP:

  • Draw a 3×3 pixel grid, label intensities, show centre g_c and eight neighbours g_0..g_7 in clockwise order, write comparisons (≥ or <), write binary pattern and decimal LBP code.

Formulas

k(x,y) = ⟨φ(x), φ(y)⟩ — k: kernel function; unitless; holds when a feature map φ exists (Mercer condition). K_{ij} = k(x_i, x_j) — kernel matrix entries; unitless; for i,j = 1..n. H = I_n - (1/n)·1_n·1_n^T — centering matrix; I_n is n×n identity, 1_n is n×1 vector of ones. K_c = H · K · H — centered kernel matrix; unitless; use in dual eigenproblem. K_c α = n λ α — dual eigenproblem; α is n×1 vector, λ is eigenvalue in feature-space PCA; n is number of samples. v = Σ_{j=1}^n α_j φ(x_j) — principal direction in feature space; unitless vector in F. v·v = α^T K_c α = 1 — normalization condition; unitless. projection y(x) = v·φ(x) = Σ_{j=1}^n α_j k_c(x_j, x) — projection of x onto component; unitless. LBP_{P,R}(x_c) = Σ_{p=0}^{P-1} s(g_p - g_c) · 2^p where s(t)=1 if t>=0 else 0; yields integer in [0,2^P-1]. H_{region}(b) = count(LBP codes == b) for b in 0..2^P-1 — histogram bin counts forming the descriptor; dimension 2^P (or smaller after mapping to uniform bins). Histograms are often normalized (L1 or L2) before classification.

Worked examples

Example 1 — Kernel PCA (small numeric): Data: n=2 samples in 1D: x1 = -1, x2 = +1. Use polynomial kernel k(x,y) = (x·y + 1)^2.

  1. Compute K: k(-1,-1) = (1+1)^2 = 4; k(-1,1) = (-1+1)^2 = 0; k(1,1) = (1+1)^2 = 4. So K = [[4,0],[0,4]].
  2. Center K: n=2, H = I_2 - (1/2)·1·1^T = [[1,0],[0,1]] - [[0.5,0.5],[0.5,0.5]] = [[0.5,-0.5],[-0.5,0.5]]. Compute K_c = H K H = [[2,-2],[-2,2]]. (One may also use row/col means formula.)
  3. Solve eigenproblem K_c α = n λ α. First find eigenvalues θ of K_c: eigenvalues θ are 4 and 0 with eigenvectors α_raw1=[1,-1]^T and α_raw2=[1,1]^T respectively. Then λ = θ / n => λ1 = 4/2 = 2, λ2 = 0.
  4. Normalise α for unit-norm of v: compute α^T K_c α. For α_raw1, α_raw1^T K_c α_raw1 = 8, so choose α = α_raw1 / sqrt(8) = [1/√8, -1/√8].
  5. Projection of training samples onto first component: p = K_c α = [4,-4] / √8 = [1.4142136, -1.4142136]. So projection of x1 = +1.4142136, x2 = -1.4142136. (All values unitless; final answers rounded to 7 decimal places.)

This shows kernel PCA separates the two symmetric points along a nonlinear principal axis in feature space.

Example 2 — LBP (3×3 window): Window intensities (clockwise starting top-left): g0=45, g1=52, g2=55, g3=53, g4=49, g5=47, g6=42, g7=48; center g_c = 50. Compute s(g_p - g_c): compare each with 50: [0,1,1,1,0,0,0,0]. Using bit weights p=0..7 where p=0 is top-left and proceed clockwise, decimal LBP = 0·2^0 + 1·2^1 + 1·2^2 + 1·2^3 + 0·2^4 + 0·2^5 + 0·2^6 + 0·2^7 = 2 + 4 + 8 = 14. Check uniformity: circular bit-string [0,1,1,1,0,0,0,0] has two transitions (0→1 and 1→0), so it is a uniform pattern (U ≤ 2). In many LBP mappings uniform patterns are given separate bins and all non-uniform patterns share one bin — this reduces descriptor dimensionality and improves robustness.

Common mistakes

  • Incorrect normalisation of α: you must enforce v·v = α^T K_c α = 1 before computing projections.
  • For LBP: wrong neighbour ordering or wrong bit-weighting convention; always state your ordering (clockwise/counterclockwise and starting index) in an exam.
  • Using k(x,x') instead of centered k_c(x,x') when projecting new points; always subtract training means in feature-space kernel evaluations (use k_c(x_i, x) = k(x_i,x) - (1/n)Σ_j k(x_j,x) - (1/n)Σ_j k(x_i,x_j) + (1/n^2)Σ_{p,q} k(x_p,x_q)).
  • Confusing kernel with distance: a kernel measures similarity (inner product in F), not a metric distance.

For your exam

Question types you will see: definitions (kernel, Mercer condition, pre-image), short notes (advantages/disadvantages of kernel PCA, LBP variants), derivations (derive dual eigenproblem K_c α = n λ α starting from C v = λ v), numericals (small K construction, centering, eigen-decomposition and projection), and diagrams (feature map illustration, 3×3 LBP window). Full-marks answers include: explicit centering steps, correct eigenproblem with factor n, normalization step for α, and clear statement of projection formula; for LBP include neighbour ordering, bit mapping and histogram formation; mention uniform and rotation-invariant variants when asked.

Quick check

  1. State the formula to centre an n×n kernel matrix K.
  2. What is the dual eigenproblem solved in Kernel PCA (use K_c and n, λ, α)?
  3. Give the definition formula for LBP_{P,R} for a pixel.

Answers: 1) K_c = H K H where H = I_n - (1/n)1_n1_n^T. 2) K_c α = n λ α. 3) LBP_{P,R} = Σ_{p=0}^{P-1} s(g_p - g_c)·2^p with s(t)=1 if t≥0 else 0.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?