Pattern representation and feature extraction
Representation and feature extraction: how patterns are described numerically (raw, statistical, structural, transform domains) and how to reduce and extract useful features (PCA, LDA, moments, gradients) for classifiers.
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
Feature representation and extraction convert raw sensor or image data into compact numeric descriptions that a pattern recognition system uses. An engineer uses these methods when designing recognition modules (face, fingerprint, speech, OCR) to improve accuracy, reduce computation and avoid overfitting before applying classifiers such as distance-based classifiers, Bayesian or tree classifiers.
Key ideas
Start from first principles and definitions.
Feature — a measurable property or attribute of a pattern used for classification. Example: pixel intensity, gradient magnitude, area. (Features are numeric vectors.)
Feature vector — a column vector x = [x1, x2, ..., xd]^T representing d features for one sample.
Feature extraction — the process of computing new features (often lower-dimensional) from raw data, typically by transforms or projections (e.g. PCA, DCT). Extraction can be linear or nonlinear.
Feature selection — the process of choosing a subset of existing features without creating new ones (e.g. using mutual information, forward selection). Different from extraction: selection keeps original features, extraction creates new features.
Dimensionality reduction — reducing d to k (k < d) to lower computation and avoid the curse of dimensionality. Two main types: feature selection and feature extraction.
Representation types
- Raw (pixel) representation: vectorize raw sensor values. Simple but high-dimensional and sensitive to noise and translation.
- Statistical (global) features: histograms, mean, variance, central moments, co-occurrence matrices. Good for texture and global descriptors.
- Structural (symbolic) features: primitives (lines, curves) and their relations (graphs), useful for shapes and syntactic methods.
- Transform-domain features: features computed after DFT/DCT/Wavelet transforms. Good for capturing frequency or scale information.
- Local features / descriptors: SIFT, HOG, SURF — compute local gradient patterns and aggregate.
Covariance and scatter
- The covariance matrix of a zero-mean random vector x measures linear relationships between features. It is symmetric and positive semi-definite.
- For classes, within-class scatter S_w and between-class scatter S_b are used in discriminant analysis: S_w sums variability inside each class; S_b measures separation of class means.
Principal Component Analysis (PCA) — linear projection that maximizes projected variance. Given centered data, find orthonormal directions (principal components) which are eigenvectors of the covariance matrix. Assumptions: linear relationships dominate, noise Gaussian is common assumption for interpretation. Steps commonly asked in exams:
- Center data by subtracting mean vector m.
- Compute covariance matrix C = (1/n) Σ (x_i - m)(x_i - m)^T (or use 1/(n-1) depending on sample convention).
- Solve eigenvalue problem C·u = λ·u; sort eigenvalues λ1 ≥ λ2 ≥ ... and select top k eigenvectors.
- Project data: y = U_k^T (x - m).
When asked for a diagram, draw a 2D scatter of samples, mark the mean, draw two orthogonal axes (principal components) as long arrows through the mean with lengths proportional to sqrt(eigenvalues). Label axes u1 and u2.
Linear Discriminant Analysis (LDA / Fisher) — linear projection that maximizes class separability by maximizing the ratio of between-class variance to within-class variance. For two classes with means m1, m2 and within-class scatter S_w, the optimal direction (for two-class LDA) is proportional to:
w = S_w^{-1} (m1 - m2)
Assumptions: classes are roughly Gaussian with equal covariances; LDA is supervised (uses labels). In general (multiclass), one maximizes J(W) = |W^T S_b W| / |W^T S_w W|, leading to the generalized eigenproblem S_b v = λ S_w v; for c classes, at most c-1 discriminant directions are obtained.
Moments and invariant descriptors — central moments provide translation invariance; normalized central moments provide scale invariance; Hu moment invariants are invariant to translation, scale, and rotation (note some change sign under reflection) for shape recognition. Derivations of moments from integrals over the object are standard exam derivations.
Normalization and scaling — Z-score normalization (zero mean, unit variance) and min-max scaling are commonly required before distance-based classifiers; always define and apply normalization consistently. For PCA with heterogeneous feature units, either standardize features (z-scores) or equivalently perform PCA on the correlation matrix.
Formulas
m = (1/n) Σ_{i=1..n} x_i — mean vector; x_i in R^d; units: same as feature units; applies before centering.
C = (1/n) Σ_{i=1..n} (x_i - m)(x_i - m)^T — covariance matrix (d×d); units: feature units squared; use 1/(n-1) for unbiased sample covariance if required by problem.
C·u = λ·u — eigenvalue problem for PCA; λ unit: variance along component; u unitless direction (can be normalized). The explained-variance ratio for component i is r_i = λ_i / Σ_j λ_j.
y = U_k^T (x - m) — PCA projection to k dimensions; y in R^k; applies after choosing top k eigenvectors.
S_w = Σ_{classes} Σ_{x∈class} (x - m_class)(x - m_class)^T — within-class scatter; units: feature units squared.
S_b = Σ_{classes} n_class (m_class - m)(m_class - m)^T — between-class scatter; units: feature units squared; m is global mean.
w = S_w^{-1} (m1 - m2) — two-class LDA projection vector; units: S_w has feature units squared, so S_w^{-1} has inverse-squared units; multiplying by (m1 - m2) (feature units) gives w in 1/feature units (a direction; not unitless unless features were standardized).
projection = w^T x — scalar projection of x onto w; typically unitless because w has reciprocal units; normalizing w does not change its units.
Z = (x - μ)/σ — Z-score normalization of scalar feature; μ mean, σ standard deviation; unitless.
Worked examples
Example 1 — PCA on a small 2D dataset (step-by-step). Data (unitless) points: (2,0), (0,2), (3,1), (1,3).
- Compute mean m = ((2+0+3+1)/4, (0+2+1+3)/4) = (1.5, 1.5).
- Center points: (0.5,-1.5), (-1.5,0.5), (1.5,-0.5), (-0.5,1.5).
- Compute covariance using 1/n: Sum x^2 = 0.5^2 + (-1.5)^2 + 1.5^2 + (-0.5)^2 = 5.0 → var_x = 5.0/4 = 1.25. Sum y^2 = 5.0 → var_y = 1.25. Sum xy = 0.5*(-1.5) + (-1.5)0.5 + 1.5(-0.5) + (-0.5)*1.5 = -3.0 → cov_xy = -3.0/4 = -0.75. Covariance matrix C = [[1.25, -0.75], [-0.75, 1.25]].
- Solve eigenvalues: for symmetric [[a,c],[c,a]], eigenvalues are a+c and a-c. Here a=1.25, c=-0.75 so λ1 = 0.5, λ2 = 2.0. Eigenvector for λ2=2.0 satisfies v1 = -v2 → principal direction u = (1/√2)[1, -1].
- Project first centered point (0.5,-1.5) onto u: y = u^T (0.5,-1.5) = (0.5+1.5)/√2 = 2/√2 = 1.4142.
Final: Principal component direction is (1/√2)[1, -1] (unit-norm). Projected coordinates give a 1D representation preserving maximal variance. (Check: ensure whether you use 1/n or 1/(n-1) as required.)
Example 2 — Two-class LDA (numerical). Class 1 points: (2,3), (3,3). Class 2 points: (0,1), (1,0). Units: unitless.
- Class means: m1 = (2.5, 3), m2 = (0.5, 0.5).
- Compute within-class scatter S_w: Class1 diffs: (-0.5,0), (0.5,0) → outer sums = [[0.5, 0],[0,0]]. Class2 diffs: (-0.5,0.5), (0.5,-0.5) → outer sums = [[0.5,-0.5],[-0.5,0.5]]. S_w = [[1.0, -0.5], [-0.5, 0.5]].
- Compute w = S_w^{-1} (m1 - m2). First m1 - m2 = (2.0, 2.5). Inverse of S_w: det = 0.25. S_w^{-1} = (1/0.25) * [[0.5, 0.5],[0.5, 1.0]] = [[2,2],[2,4]]. Multiply: w = [[2,2],[2,4]]·[2,2.5] = [9, 14].
- Projected class means: w^T m1 = 64.5, w^T m2 = 11.5. A threshold halfway (≈38) separates classes under equal priors and equal misclassification costs.
Final: LDA direction w = [9,14] (any positive scalar multiple works); project and threshold for classification.
Example 3 — short theoretical model answer (exam style): "Derive PCA projection for centered data and explain why eigenvectors give directions of maximum variance." Model answer (outline): center data, form covariance C, consider variance of projection onto unit vector u: var(u^T x) = u^T C u. Maximize u^T C u subject to u^T u = 1 → Lagrange multiplier gives C u = λ u. Thus eigenvectors are stationary directions and eigenvalues equal variances along them. Select top eigenvectors for reduced representation.
Common mistakes
- Not normalizing features before using distance-based classifiers or PCA when features have different units.
- Confusing feature selection (choose original features) with feature extraction (create new features).
- For LDA, forgetting that S_w must be invertible; small sample sizes can make S_w singular (use regularization or PCA first).
- Miscomputing eigenvectors sign: eigenvector v and -v represent same direction; classification threshold must account for sign.
For your exam
Question types you will see: short definitions (feature, covariance), short notes (compare PCA vs LDA; types of features), derivations (PCA eigen derivation, expression for LDA), numericals (compute covariance/eigenvectors, project points), diagram questions (draw PCA axes on scatter), and small programs (implement PCA in Python). A full-marks answer includes:
- Clear definitions of terms used.
- Stated assumptions (e.g., centered data, sample vs population covariance).
- Step-by-step algebra with intermediate results (means, centered data, covariance matrix entries).
- Properly labeled diagram if asked (mean, principal axes, scatter direction, arrows showing projection).
- Numerical answers showing units (or stating unitless) and any normalization used.
Quick check
- What is the main difference between feature extraction and feature selection?
- Why must data be centered before PCA?
- Write the LDA direction formula for two classes. Answers: 1) Extraction creates new features (projections/transforms); selection picks a subset of original features. 2) Because PCA solves eigenproblem on covariance, and covariance assumes zero-mean; centering ensures principal components capture directions of maximal variance. 3) w = S_w^{-1} (m1 - m2).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?