Skip to main content

PCA — Principal Component Analysis

TaskExploratory (unsupervised)
Method key (analysis_type)pca
Prediction on new dataNo
Librarysklearn.decomposition.PCA

When to use​

Use PCA to look at the data before you build a classifier:

  • see whether samples of different classes form separate groups;
  • find outliers and failed measurements;
  • find out which colour channels vary the most.

PCA does not use the label column to build the model. The label column only colours the points on the score plot.

How it works​

PCA finds new axes, the principal components, that are linear combinations of the features. The first component points in the direction of the largest variance in the data, the second one in the direction of the largest remaining variance orthogonal to the first, and so on.

The centred data matrix XX (nn samples × pp features) is decomposed as

X=TP⊤+E,X = T P^\top + E,

where TT (n×an \times a) holds the scores (coordinates of the samples on the components), PP (p×ap \times a) holds the loadings (weights of the features in each component), aa is the number of components and EE is the residual. The share of the variance explained by component jj is λj/∑kλk\lambda_j / \sum_k \lambda_k, where λj\lambda_j are the eigenvalues of the covariance matrix of XX.

Parameters​

Defaults as set by the analysis dialog.

ParameterUI labelDefaultNotes
n_componentsNumber of Components5Cannot exceed the number of feature columns: the dialog checks this.
note

PCA ignores feature selection and replicate averaging, even if they are set in the dialog. All selected rows and feature columns go into the model as they are.

Preprocessing​

The data are centred, but not scaled: sklearn.decomposition.PCA subtracts the mean of each column and does not divide by its standard deviation. Features with a large range (for example, raw 0–255 RGB channels) therefore dominate the first components over features with a small range (for example, ratios or normalised values). If your features have different units or ranges, make them comparable first with adjustments.

Rows are not filtered: rows with an empty label also take part in PCA (their label is shown as unknown).

Results and metrics​

FieldMeaning
explained_variance_ratioShare of the total variance explained by each component
cumulative_varianceRunning sum of explained_variance_ratio
transformed_dataScores TT: coordinates of every sample on every component
loadingsLoadings PP: one row per feature, one column per component
componentsThe same loadings, transposed (one row per component)

PCA has no cross-validation and no accuracy metrics: there is nothing to predict.

Visualizations​

  • Score plot: samples on two chosen components, coloured by label.
  • Loading plot: contribution of each feature to the components.

Source code​

The analysis is a thin wrapper around scikit-learn:

analysis.py · run_pca_analysis() · lines 938–956
def run_pca_analysis(X, y, parameters):
"""Run PCA analysis"""
n_components = parameters.get('n_components', min(X.shape[0], X.shape[1], 10))

pca = PCA(n_components=n_components)
X_pca = pca.fit_transform(X)

loadings = pca.components_.T

return {
'method': 'PCA',
'n_components': n_components,
'explained_variance_ratio': pca.explained_variance_ratio_.tolist(),
'cumulative_variance': np.cumsum(pca.explained_variance_ratio_).tolist(),
'components': pca.components_.tolist(),
'transformed_data': X_pca.tolist(),
'loadings': loadings.tolist(),
'labels': y.tolist() if y is not None else None
}

Where the call comes from: run_analysis in chrometrica/analysis/tasks.py builds X from the selected data range and y from the label column, then calls run_pca_analysis(X, y, parameters).

Limitations and common pitfalls​

  • Scale. Without scaling, PCA mostly describes the features with the largest numeric range. See Preprocessing.
  • Outliers. A single failed well can take a whole component. Check the score plot for isolated points before you interpret the loadings.
  • Separation is not classification. Classes that overlap on the first two components can still be separated by a supervised method such as PLS-DA or LDA, and vice versa: a visible separation is not a validated model.

References​

  • Jolliffe I. T., Cadima J. Principal component analysis: a review and recent developments. Philosophical Transactions of the Royal Society A, 374, 20150202 (2016). doi:10.1098/rsta.2015.0202
  • Wold S., Esbensen K., Geladi P. Principal component analysis. Chemometrics and Intelligent Laboratory Systems, 2, 37–52 (1987). doi:10.1016/0169-7439(87)80084-9
  • scikit-learn user guide: PCA.