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.

Data reduction: sampling, feature selection and PCA

Data reduction reduces data size and complexity using sampling, selecting relevant features, and transforming features (PCA) so BI systems are faster, cheaper and avoid overfitting.

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

Two sentences: Engineers use data reduction to make Business Intelligence (BI) systems faster and cheaper to run, and to improve model accuracy by removing noise and redundancy. Sampling, feature selection and principal component analysis (PCA) are standard steps in the BI life cycle before building OLAP reports or predictive models.

Key ideas

  • Sampling bias: systematic error when the sample is not representative of the population (for example, missing a stratum).
  • Simple random sampling (SRS): without replacement, every subset of size n has equal probability; with replacement, draws are independent and each unit has equal selection probability on each draw. Assumes a well-defined population list.
  • Stratified sampling: divide population into non-overlapping groups called strata and sample from each. Use when population is heterogeneous but strata are internally homogeneous.
  • Cluster sampling: divide population into clusters (often geographic) and sample whole clusters. Use when listing individuals is hard but clusters are natural.
  • Neyman allocation (optimal allocation): in stratified sampling, allocate sample size ni proportional to Ni·si (stratum size times stratum standard deviation) to minimise variance for fixed total n; assumes equal per-unit sampling cost. If per-unit costs ci differ, optimal allocation is proportional to Ni·si divided by sqrt(ci).

Feature selection concepts:

  • Feature selection: choosing a subset of original variables (features) that are most useful for a task (e.g., prediction). It reduces dimensionality but keeps original features.
  • Filter methods: select features using statistics independent of the learning algorithm (e.g., correlation, mutual information). Fast, scalable.
  • Wrapper methods: evaluate subsets by training a model (e.g., forward/backward selection). Slower but catches interactions.
  • Embedded methods: feature selection done during model training (e.g., LASSO regularisation). Balanced between filter and wrapper.
  • Multicollinearity: when two or more features are highly correlated, causing unstable model coefficients.

Principal Component Analysis (PCA):

  • PCA: a linear transformation that produces new orthogonal variables (principal components) that are ordered by decreasing variance. PCA is a dimensionality reduction technique — it replaces original correlated variables by fewer uncorrelated components.
  • Assumptions: linear relationships suffice, the principal-variance directions are useful for the task, data is mean-centered (zero mean). PCA is unsupervised (does not use labels). PCA is scale-sensitive: if features have different units/scales, standardize them (or perform PCA on the correlation matrix) before computing PCs.
  • Theorem (PCA optimality, stated exactly): Let S be the sample covariance matrix of mean-centered data. The first principal component direction u1 is the unit vector that maximises the projected variance u^T S u subject to u^T u = 1. The solution u1 is an eigenvector of S with largest eigenvalue λ1, and the variance explained equals λ1. The k-dimensional PCA subspace (spanned by the top k eigenvectors) minimises the mean squared reconstruction error among all k-dimensional linear subspaces.
  • Derivation (standard exam derivation): maximize u^T S u subject to u^T u = 1. Use a Lagrange multiplier λ: L(u,λ)=u^T S u - λ(u^T u -1). Differentiate: 2S u - 2λ u = 0 => S u = λ u. Thus u is an eigenvector of S; to maximise variance choose the eigenvector with largest eigenvalue. Repeat with orthogonality constraints to get further components.

Diagram to draw (exam): Draw a 2D scatterplot of points. Mark the sample mean μ with a dot. From μ draw two perpendicular arrows: the longer arrow labelled PC1 and the shorter PC2. Indicate projection of some points onto PC1 with dashed perpendicular lines and label projected points. Add labels: axes x1, x2; angle between PC1 and x1.

Formulas

\bar{x} = (1/n) \sum_{i=1}^n x_i — sample mean; x_i are observations, unit = unit of x (e.g., kg); applies to estimating central tendency.

s^2 = (1/(n-1)) \sum_{i=1}^n (x_i - \bar{x})^2 — sample variance; units = (unit of x)^2; the sample standard deviation s = sqrt(s^2) has the same units as x; applies to sample spread estimate.

SE(\bar{x}) = s / \sqrt{n} — standard error of the mean; s has the same units as x; applies to SRS with replacement or to SRS without replacement when the sampling fraction n/N is small; for SRS without replacement from a finite population of size N, multiply by the finite population correction sqrt(1 - n/N).

n_i = n * (N_i \cdot s_i) / \sum_j (N_j \cdot s_j) — Neyman allocation for stratified sampling; N_i dimensionless counts, s_i standard deviation (units of measured variable); applies when minimizing variance given total sample n.

cov(X,Y) = (1/(n-1)) \sum_{i=1}^n (X_i - \bar{X})(Y_i - \bar{Y}) — sample covariance; units = unit(X)·unit(Y).

r_{XY} = cov(X,Y) / (s_X s_Y) — Pearson correlation coefficient; dimensionless between -1 and 1.

S = (1/(n-1)) X^T X (after mean-centering the columns of X, i.e., each feature) — sample covariance matrix; units = unit(feature_i)·unit(feature_j).

S u = \lambda u — eigen decomposition of covariance matrix; λ is variance explained by component u; units = (unit of features)^2.

z = U^T (x - \mu) — PCA projection of vector x onto principal components U (columns are eigenvectors); z units = the same as the features when using the covariance matrix (if using the correlation matrix or standardized data, z is in standard-deviation units).

explained variance fraction for PC k = \lambda_k / \sum_{j} \lambda_j — fraction dimensionless.

X = U \Sigma V^T — singular value decomposition; singular values in Σ relate to √(eigenvalues) of X^T X; applies to any real matrix X.

Worked examples

Example 1 — Stratified sampling (Neyman allocation) Data: Population N = 1000 customers divided into three strata: N1 = 500, N2 = 300, N3 = 200. Stratum standard deviations for purchase amount: s1 = 10 rupees, s2 = 20 rupees, s3 = 30 rupees. Total sample budget n = 100. Steps:

  1. Compute Ni·si: 500·10 = 5000; 300·20 = 6000; 200·30 = 6000.
  2. Sum = 5000 + 6000 + 6000 = 17000.
  3. Neyman allocation: n1 = 100·(5000/17000) = 29.4118 ≈ 29; n2 = 100·(6000/17000) = 35.2941 ≈ 35; n3 = 35.2941 ≈ 36 (adjust to sum to 100). Answer: choose n1 = 29, n2 = 35, n3 = 36 (sample numbers); these minimise estimator variance for given n under the Neyman rule. Bold final: n1 = 29, n2 = 35, n3 = 36.

Example 2 — Feature selection by Pearson correlation (filter) Data (5 observations): X1 = [1,2,3,4,5] X2 = [2,1,4,3,5] X3 = [5,4,3,2,1] Y = [2,3,4,5,6] Steps:

  1. Compute correlations with Y. For X1: Y = X1 + 1 so r_{X1,Y} = 1. For X3: X3 = 6 - X1 so r_{X3,Y} = -1. For X2 compute means: \bar{X2} = 3, \bar{Y}=4. Cov(X2,Y) = 2 (calc), s_X2 = sqrt(2.5) ≈ 1.5811, s_Y ≈ 1.5811, so r_{X2,Y} = 2/(1.5811·1.5811) = 0.8.
  2. Rank by absolute correlation: |r| = {X1:1.0, X3:1.0, X2:0.8}. Interpretation: filter method would pick X1 and X3. But note that X1 and X3 are perfectly (negatively) collinear — a model including both will have multicollinearity. Use wrapper or embedded methods or drop one redundant feature. Final decision often: keep X1 and drop X3. Bold final: select X1 (and drop X3 to avoid multicollinearity).

Example 3 — PCA on 2D points (numerical eigen-decomposition) Data points: (2,0), (0,2), (3,1), (1,3). Units: arbitrary (e.g., score points). Steps:

  1. Mean: μ = (1.5, 1.5). Centered points: (0.5,-1.5), (-1.5,0.5), (1.5,-0.5), (-0.5,1.5).
  2. Covariance matrix S = [[1.6667, -1.0000], [-1.0000, 1.6667]] (units^2).
  3. Eigenvalues: λ1 = 2.6667, λ2 = 0.6667. Eigenvectors: u1 ∝ [1,-1], u2 ∝ [1,1].
  4. Project first component: normalized u1 = (1/√2)[1,-1]. Projections of the four centered points on u1 are approximately [1.4142, -1.4142, 1.4142, -1.4142]. Variance along PC1 = 2.6667 which equals λ1.
  5. Explained variance fraction = λ1/(λ1+λ2) = 2.6667/3.3334 = 0.80 = 80%. Bold results: PC1 direction [1,-1], explained variance 80%.

Common mistakes

  • Using Pearson correlation on non-linear relationships without verification.
  • Using filter methods alone when features are multicollinear; students often keep two perfectly correlated features.
  • Confusing sampling fraction with sample size; forgetting finite population correction when sampling fraction is large.
  • Rounding Neyman allocation too early; always compute exact fractional allocations then round keeping total n.

For your exam

Question types: definitions (define sampling types, PCA, feature-selection methods), short notes (advantages/disadvantages of sampling methods, filter vs wrapper), derivations (PCA Lagrange multiplier proof), numericals (Neyman allocation, PCA eigen-decomposition on small matrices), and diagram (scatterplot showing PCs). What full-marks answers include:

  • For definitions: bold term, exact property, assumptions and a short example.
  • For derivations: show every algebra step (Lagrange function, differentiation, conclusion that S u = λ u), state assumptions (mean-centred data), and interpret λ and u.
  • For numericals: show data, compute means, centre, compute covariance with (n-1) denominator, solve eigenvalues algebraically, normalise eigenvectors, project points, compute explained variance fraction, and give final numeric answers with units.
  • For sampling questions: state population and strata, show Ni·si computations, show fractional ni, round with justification, discuss potential bias and finite-population correction if relevant.

Quick check

  1. What is the PCA objective for the first principal component? (short)
  2. Give one advantage and one disadvantage of filter feature-selection methods.
  3. Write the Neyman allocation formula and say what each symbol means. Answers: 1) Maximise projected variance u^T S u subject to u^T u = 1 (u is eigenvector of S with largest eigenvalue). 2) Advantage: fast and independent of model; Disadvantage: ignores feature interactions and multicollinearity. 3) n_i = n * (N_i s_i) / sum_j (N_j s_j); where n total sample, n_i sample for stratum i, N_i stratum size, s_i stratum standard deviation.

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

Stuck on something here?