liveBook · Manning

Correlation

As explained in Natural Language Processing in Action, Second Edition

A correlation coefficient is a numerical value quantifying the relationship between paired values in two sequences.

Correlation quantifies how paired values in two sequences vary together. Pearson correlation centers each sequence by subtracting its mean, then compares their shared variation after scaling by both sequences’ magnitudes. section 7.2.3 section 4.3.1

The coefficient requires sequences of equal length. A value of 1.0 indicates perfect self-correlation, while values nearer zero indicate weaker linear alignment; interpretation depends on the data being compared. section 7.2.3

Computing Pearson correlation

A short NumPy function centers two arrays and returns their normalized dot product. For a repeating array a and steadily increasing array b, corr(a, b) is 0.316; corr(a, a) is 1.0. section 7.2.3

Correlation takes several practical roles:

  • Relate features during dimensionality reduction: covariance and correlation reveal redundancy between dimensions, helping PCA find axes that preserve variance while reducing dependence. section 4.3.1
  • Inspect neural-network representations: correlating hidden weights with character position exposes units whose values track where a name occurs in the sequence. section 8.2.3
  • Measure local pattern matches in CNNs: a kernel’s weights and a text window produce a dot product or correlation score, which can be evaluated across sliding windows. section 7.2.1
Listing 7.1 A function to compute Pierson correlation
>>> import numpy as np
>>> def corr(a, b):
...    """ Compute the Pearson correlation coefficient R """
...    a = a - np.mean(a)
...    b = b - np.mean(b)
...    return sum(a * b) / np.sqrt(sum(a*a) * sum(b*b))
>>> a = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
>>> b = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> corr(a, b)
0.316...
>>> corr(a, a)
1.0

Inspect the complete NumPy implementation and its output on repeating and increasing sequences. section 7.2.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines Pearson normalization and demonstrates code, then applies correlation to PCA, RNN interpretation, and CNN sliding windows.
Examples
Demonstrates NumPy arrays, PCA feature dimensions, RNN hidden weights versus character positions, and CNN kernels over text windows.
What it assumes
Assumes paired numerical sequences, means, products, square roots, and basic familiarity with arrays and neural-network representations.
What you can do after
Apply it via NumPy or scipy.stats to compare equal-length sequences and interpret relationships in learned numerical representations.
How it connects
Connects correlation with covariance, convolution, convolutional neural networks, principal component analysis, recurrent neural networks, and hidden representations.

Also explained in

Compare how 16 books explain correlation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.