Dimensionality and anomaly detection
Reduce dimensions, not meaning
Dimensionality reduction maps feature vectors to fewer coordinates. It can simplify visualization or reduce storage and computation, but may discard information necessary for a later task. Feature selection retains some original columns; feature extraction constructs new coordinates. For synthetic sensor pairs (-2,-1), (-2,1), (2,-1), (2,1), projection onto x keeps two distinct values and discards y. The first two points become indistinguishable despite different y readings. Choose representations by their intended use, not appearance alone.
PCA as a centered linear projection
Principal component analysis (PCA) finds orthogonal directions of maximum variance in centered training data, successively constrained to be orthogonal to previous components. A component score is the dot product of the centered vector with a unit direction. In the symmetric four-point fixture above, means are zero, cross covariance is zero, and population variances are 4 and 1. The x axis is therefore a valid first principal direction; its explained variance ratio is 4/(4+1)=0.8. The sign of the direction could be reversed with equivalent variance. Sample covariance uses n-1 instead of n; here both variances change by the same factor, so the ratio is unchanged. The code projects onto the known x direction and checks this ratio only for this diagonal fixture. It does not fit general PCA or compute eigenvectors. A general PCA implementation fits a centered basis on training data and reuses the mean and basis on held-out data. Standardizing first may change the principal directions; PCA does not automatically require all features to be standardized.
def project_x(points, mean_x): return [x - mean_x for x, y in points]
points = [(-2, -1), (-2, 1), (2, -1), (2, 1)]mean_x = sum(x for x, y in points) / len(points)mean_y = sum(y for x, y in points) / len(points)scores = project_x(points, mean_x)var_x = sum((x - mean_x)**2 for x, y in points) / len(points)var_y = sum((y - mean_y)**2 for x, y in points) / len(points)print(scores)print(var_x / (var_x + var_y))Reconstruction and lost information
Reconstruct a point from its one retained score by multiplying by the unit x direction and adding the original mean vector. The point (2,1) becomes (2,0); the squared reconstruction error is 1. All four original points have this error, so average squared reconstruction error is 1. An explained variance ratio of 0.8 does not imply 80% classification accuracy or that 80% of records are correct. If a later label depends entirely on the sign of y, this projection loses the needed feature. For all-constant data, total variance is zero, so a variance ratio is undefined. With equal largest eigenvalues, a principal direction may not be unique. Nonlinear embeddings such as t-SNE or UMAP can aid exploration but do not preserve all global distances; their parameters and randomness matter. No external embedding library is run here.
Anomaly detection versus novelty detection
An anomaly score measures how unusual an observation is relative to a model or reference. Outlier detection commonly looks for unusual records in a dataset that may already contain them. Novelty detection fits a reference intended to represent normal behavior and scores new records. A small illustrative novelty rule uses reference [10,11,12], mean 11, and flags absolute deviations strictly greater than 2. New readings 9,11,13,17 have deviations 2,0,2,6 and flags false,false,false,true. Equality is not flagged in this chosen policy. The threshold is in the original measurement units; choose it using separate validation data and the cost of investigations, not the final test set. The mean is sensitive to extreme reference values. This is not an isolation forest or LOF implementation, and it does not establish that the reference is normal.
def anomaly_flags(values, center, threshold): if threshold < 0: raise ValueError("Nonnegative threshold required") return [abs(x - center) > threshold for x in values]
training = [10, 11, 12]center = sum(training) / len(training)print(anomaly_flags([9, 11, 13, 17], center, 2))Use scores as investigation signals
Density, distance, isolation and reconstruction-error methods use different assumptions. A rare machine mode may be expected during maintenance; a frequently occurring defect may not be rare at all. Review provenance, sensor health, shifts in operating conditions and false alarms. When labels exist, evaluate precision and recall for the intended review process; without labels, use bounded human audits, known synthetic perturbations and sensitivity checks. Avoid clustering people to infer sensitive traits, and never treat anomaly flags as proof of wrongdoing or automatically trigger punitive actions. Practice: change the threshold to 1 and predict which values become flagged; then explain why a new mode may require a different baseline. Next: feature preparation, pipelines and generalization.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dimensionality and anomaly detection quiz
Apply clustering and representation concepts to small synthetic examples.
In the symmetric four-point fixture, what is the x-axis explained variance ratio?
What is the reconstruction of (2,1) using only the x component and zero mean?
Which mean should center held-out vectors before applying a PCA basis already fitted on training data?
With center 11 and strict threshold >2, which value is flagged?
What conclusion is justified by an unusual sensor score alone?