liveBook · Manning

Local averaging

As explained in Natural Language Processing in Action, Second Edition

A moving average is an aggregate that summarizes recent values over a changing or weighted window of observations.

Local averaging summarizes nearby sequence values with a moving, weighted window. As a hardcoded convolution kernel, it smooths or exposes short patterns without training first. section 7.2.5

A pure Python implementation makes the operation concrete:

  • Represent the sequence numerically, such as a binary indicator for whether each token is an adverb. section 7.2.5
  • Choose kernel weights such as [0.5, 0.5] to average each adjacent pair. section 7.2.5
  • Slide the kernel across the sequence, multiply corresponding values, and sum them; an input of length 20 produces 19 outputs. section 7.2.5
  • Inspect the result as a smoothed signal: adjacent adverb indicators produce outputs of 0.5 or 1. section 7.2.5

Adverbness in a quotation

Part-of-speech tagging converts a quotation into an is-adverb sequence. Applying [0.5, 0.5] highlights neighboring adverb indicators, producing a local adverbness signal that can be plotted against the original input. section 7.2.5

Listing 7.4 Convolution in pure Python
>>> kernel = [.5, .5]       #1
>>>
>>> output = []
>>> for i in range(len(inpt) - 1):         #2
...    z = 0
...    for k, weight in enumerate(kernel):   #3
...        z = z + weight * inpt[i + k]
...    output.append(z)
>>>
>>> print(f'inpt:\n{inpt}')
>>> print(f'len(inpt): {len(inpt)}')
>>> print(f'output:\n{[int(o) if int(o)==o else o for o in output]}')
>>> print(f'len(output): {len(output)}')inpt:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0., 1, 1., 0, 0, 0., 1., 0, 0, 0]
len(inpt): 20
output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, .5, 1, .5, 0, 0, .5, .5, 0, 0]
len(output): 19

Inspect the runnable pure Python convolution that defines the 2-point moving average and prints input and output lengths. section 7.2.5

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains weighted sliding computation in pure Python and stops short of training or fitting the kernel.
Examples
Uses cryptocurrency prices, rainfall reports, Morse code, and a part-of-speech sequence from a quotation.
What it assumes
Assumes basic Python sequences and multiplication while teaching convolution shapes and sliding weighted sums from first principles.
What you can do after
Apply it via pure Python to build a hardcoded detector for short sequence patterns.
How it connects
Connects local averaging with convolution, convolutional neural networks, natural language processing, feature engineering, and pattern detection.

Also explained in

Compare how 7 books explain local averaging →

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.