Concepts / Principal Component Analysis (PCA)

Principal Component Analysis (PCA)

The components are the shared building blocks.

  • Programming

Predicting the Required Property

Imagine you have a collection of data points, and you want to describe each one as a combination of a few shared building blocks. Each point becomes a recipe: how much of each building block it contains. This article is about one specific version of that idea, called Non-Negative Matrix Factorization, or NMF. It is also closely related to Principal Component Analysis, or PCA, which shares the same basic structure. NMF is an unsupervised learning algorithm that extracts useful features and can be used for dimensionality reduction.

What do you think happens?

Before reading on, predict the one property that the data must have for NMF to make sense as a recipe of building blocks.

Reveal answer
Answer:

The data must be non-negative: every feature value must be greater than or equal to zero.

NMF requires both the components and the coefficients to be greater than or equal to zero, so it can only be applied to data where each feature is non-negative. A negative entry cannot be built from a sum of non-negative pieces.

Tracing a Point Back to Its Parts

NMF starts from an idea it shares with PCA. Each data point is written as a weighted sum of some components. The components are the shared building blocks. The weights, which the source calls coefficients, say how strongly each component contributes to a particular data point. Once the components and coefficients are known, the data point can be rebuilt by adding up the weighted components. In matrix shorthand, the data matrix $V$ is approximately the product of a component matrix $W$ and a coefficient matrix $H$, written $V \approx WH$.

Message data pointword counts [2, 3, 3]Component 1: greetingwords[1, 0, 0]Component 2: orderwords[0, 1, 1]Coefficient 2weight on component 1Coefficient 3weight on component 2times 2times 3scalesscales
How do two non-negative component vectors, each scaled by its own coefficient, add up to reconstruct one data point?
Worked example

Rebuilding a Message from Its Parts

Features are the counts of three words: hello, order, refund. Two components are given, and one message has coefficients 2 for component 1 and 3 for component 2. Rebuild the message as a weighted sum.

  1. 1
    Write the components

    Component 1 is [1, 0, 0] (greeting words) and component 2 is [0, 1, 1] (order and refund words).

  2. 2
    Scale each component

    Multiply component 1 by its coefficient 2, giving [2, 0, 0]. Multiply component 2 by its coefficient 3, giving [0, 3, 3].

  3. 3
    Add the scaled components

    Add the two results position by position: [2 + 0, 0 + 3, 0 + 3] = [2, 3, 3].

  4. 4
    Check the sign

    Every component value and every coefficient is zero or positive, so every contribution pushes the total upward. No part subtracts from another.

Result

The message is rebuilt as [2, 3, 3], which matches the observed word counts.

The Non-Negativity Rule

The difference from PCA lies in the constraints. NMF requires both the components and the coefficients to be greater than or equal to zero. Nothing in the rebuilt data point can subtract from another part of it, so every contribution pushes the total in the same direction. The same rule applies to the input: NMF can only be applied to data where each feature is non-negative.

QuantityRequired sign for NMFExample in this article
Input featuresGreater than or equal to zeroWord counts such as 0, 2, or 3
ComponentsGreater than or equal to zero[1, 0, 0] and [0, 1, 1]
CoefficientsGreater than or equal to zero2 and 3

Every quantity in the NMF model must be zero or positive.

In the real world

Suppose a dataset records how often a set of words appears in each of several short messages. Every entry is a count, so none of them can be negative. This is the kind of data NMF can accept. Now suppose a second dataset records changes in temperature, where some readings are below zero. That dataset does not satisfy the requirement, because a negative entry cannot be built from a sum of non-negative pieces.

NMF Compared with PCA

NMF and PCA are closely related. Both write each data point as a weighted sum of components, and both can be used for dimensionality reduction. Where they differ is in what they optimize and which constraints they place on the parts. PCA seeks orthogonal components that explain maximum variance. NMF seeks non-negative components and coefficients instead.

Non-negative partsobjectiveMaximum varianceobjectiveComponents at orabove zeroconstraintOrthogonal componentsconstraintCoefficients at orabove zeroconstraintWeighted sum ofcomponentsshared structure
How do NMF and PCA differ in their constraints on components and coefficients, and in what they optimize?
FeatureNMFPCA
Shared structureEach data point is a weighted sum of componentsEach data point is a weighted sum of components
Use for dimensionality reductionYesYes
Constraint on componentsGreater than or equal to zeroOrthogonal
Constraint on coefficientsGreater than or equal to zeroNot stated as a non-negativity rule in the source
Optimization goalNon-negative components and coefficientsComponents that explain maximum variance

Both methods share a structure; the constraints and goals are what separate them.

Is Your Dataset a Fit?

Before choosing NMF, check the data in a fixed order. The first question concerns the sign of every input feature. Only if all features pass that check does the question of whether the parts can plausibly add up to each sample become relevant.

Start with adatasetEvery feature valueat or above zero?input checkDoes each sample addup from shared parts?structure checkNMF is suitableNMF is not suitablefor this data as isyesnoyesno
What sequence of checks tells me whether my dataset can be factorized with NMF?
EASY

A dataset records the ratings of several movies, where each rating is between 1 and 5, and a second dataset records daily changes in a stock price, where some changes are negative. Which dataset can NMF accept, and why?

Hints
  • Check whether every input value is zero or positive.
  • A negative entry cannot be built from a sum of non-negative pieces.
  • Ratings in this case never fall below 1.

Mistakes Learners Make with NMF

  • Common mistake

    Assuming NMF works on any numeric dataset

    What it looks like

    Applying NMF directly to temperature readings that include values below zero.

    Why it fails

    NMF requires components and coefficients to be greater than or equal to zero, so it can only be applied to data where each feature is non-negative.

    Do this instead

    Check every input feature for negative values first, and only proceed if all are zero or positive.

  • Common mistake

    Treating NMF and PCA as having the same constraints

    What it looks like

    Believing that because both write data as a weighted sum, they optimize the same goal.

    Why it fails

    PCA seeks orthogonal components that explain maximum variance, while NMF seeks non-negative components and coefficients.

    Do this instead

    Compare the constraints and objectives separately, as shown in the comparison table.

  • Common mistake

    Thinking a negative coefficient can make the rebuild work

    What it looks like

    Using a coefficient of -1 to subtract a component from the total.

    Why it fails

    NMF requires coefficients to be zero or positive, so nothing in the rebuilt point can subtract from another part of it.

    Do this instead

    Use only zero or positive coefficients, so every contribution pushes the total in the same direction.

Key Takeaways

  • NMF writes each data point as a weighted sum of components, with coefficients stating how strongly each component contributes, similar to the structure used in PCA.
  • NMF requires components and coefficients to be greater than or equal to zero, so it can only be applied to data where each feature is non-negative.
  • PCA seeks orthogonal components that explain maximum variance, whereas NMF seeks non-negative components and coefficients.
  • Before using NMF, check that every input feature is zero or positive; a dataset with negative entries, such as temperature changes below zero, is not a fit.

Related concepts

More concepts starting with P