33 / Predictive analytics
Principal component analysis
Principal component analysis transforms correlated numerical features into orthogonal components ordered by explained variance. It can simplify exploratory analysis and some predictive pipelines, but the direction with most variation is not automatically the direction with most business importance.
The business question
Can many correlated measurements be summarised without carrying every variable separately?
- Summarise several correlated sensor or operating indicators before visual exploration.
- Reduce redundant features in a carefully validated prediction pipeline.
- Investigate whether many recorded measures largely follow the same broad activity pattern.
How the method works
PCA centres the data and finds directions that capture successively large amounts of variation while remaining orthogonal to earlier directions. Each component is a weighted combination of the original features. Projecting an observation onto these directions gives its component scores. A component is a mathematical axis, not a newly measured physical quantity.
Scaling affects which directions dominate. If rand values and order counts are mixed, standardising can stop one unit from overwhelming the calculation, but may also give a noisy variable undue weight. The explained-variance ratio for a component divides its variance by the total variance. Choosing how many components to retain trades compression against information discarded under that particular variance criterion.
Loadings help interpret which variables move together, but a component does not establish a causal factor. Its sign is arbitrary: reversing all loadings and scores describes the same axis. For predictive work, fit centring, scaling and PCA inside training data or each validation fold. Evaluate the downstream business task, because a small-variance feature may still contain important information about rare failures or profitable customers.
The calculation
| Symbol | Meaning |
|---|---|
| X_standardised | The chosen centred and scaled feature matrix. |
| W | Matrix whose columns are component directions or loadings. |
| λ_j | Variance associated with principal component j. |
| Z | Scores of observations on the retained component axes. |
A worked business example
ILLUSTRATIVE SCENARIO
Constructed daily sales counts for three product indicators demonstrate redundancy.
Inputs
- Rows: (10,20,5), (12,24,4), (14,28,6), (16,32,5), (18,36,7), (20,40,6).
- The second indicator is exactly twice the first in the teaching data.
- Standardise all three features and fit two principal components.
Work through the numbers
- The first component has an explained-variance ratio of approximately 0.8540.
- The second explains approximately 0.1460; the remaining component has effectively zero variance.
- Together, two components retain essentially all variation in this particular three-column sample.
- One valid first-component loading vector is approximately (0.6064, 0.6064, 0.5144); its simultaneous sign reversal is equivalent.
Retaining 100% here does not imply that two components will always retain all future business information. The six-row sample is illustrative, and variance retained is not a prediction accuracy or an economic benefit.
Run and inspect the fitted example
This method needs a richer model-fitting workflow. The complete runnable Python example below shows the fit and relevant checks; use the downloadable notebook to change the data and rerun it.
Download the worked notebookApply it at work
- Define whether PCA serves visual exploration, storage reduction or a predictive task.
- Choose meaningful numerical inputs and document centring, scaling and treatment of missing or unusual values.
- Fit PCA, inspect loadings and explained variance, and compare alternative retained dimensions.
- Validate interpretability, stability and any downstream task on representative data not used to select the pipeline.
Check these assumptions
- Numerical features and the chosen linear combinations are meaningful for the intended task.
- The sample represents the variation the analysis is meant to summarise.
- The selected scaling and variance-based compression criterion are appropriate business choices.
- For prediction, all learned preprocessing is restricted to training data to avoid leakage.
Common mistakes and how to avoid them
- A high retained-variance percentage does not prove that a classifier or operational decision will improve.
- Interpreting component loadings as causal mechanisms overstates an unsupervised transformation.
- Dropping a low-variance feature can remove a rare but commercially important fault signal.
How this can help the business
- Analysts can replace several redundant charts with a smaller set of inspectable patterns during diagnosis.
- A validated pipeline may become easier to fit or visualise, but any savings or prediction improvements must be measured against a simpler alternative.
Retain only a dimensionality and preprocessing choice that serves the stated business task; judge success with downstream usefulness and validation, not an arbitrary variance threshold alone.
When to use another approach
Use feature selection when original-variable interpretation matters, domain-designed indices when justified, or nonlinear embeddings for limited exploratory purposes.
Reproduce the worked example in Python
The code is self-contained for this example. Dependencies and run instructions are included with the downloadable notebook.
Show executable code and recorded output
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
sales = np.array([[10,20,5],[12,24,4],[14,28,6],[16,32,5],[18,36,7],[20,40,6]])
scaled = StandardScaler().fit_transform(sales)
model = PCA(n_components=2).fit(scaled)
print("Explained-variance ratios:", np.round(model.explained_variance_ratio_,4).tolist())
print("Cumulative retained variance:", round(model.explained_variance_ratio_.sum(),4))
print("First component weights:", np.round(model.components_[0],4).tolist())Recorded output
Explained-variance ratios: [0.854, 0.146] Cumulative retained variance: 1.0 First component weights: [0.6064, 0.6064, 0.5144]
Sources and further reading
Concepts are linked to primary teaching or technical sources. The business examples are illustrative applications, not reported client results.