Skip to content

Commit bace0dd

Browse files
committed
Add two example files (previously only included in the text)
1 parent ea5fcbc commit bace0dd

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

examples/preprocess.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def whiten(data):
2+
"""
3+
Return a whitened copy of the data, i.e. data with zero mean and unit
4+
variance.
5+
6+
Parameters
7+
----------
8+
data : ndarray
9+
The data to whiten.
10+
11+
Returns
12+
-------
13+
whitened : ndarray
14+
The whitened data.
15+
"""
16+
centered = data - data.mean()
17+
whitened = centered / data.std()
18+
return whitened

examples/test_whiten.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from numpy.testing.utils import assert_allclose, assert_equal
2+
import numpy
3+
4+
from preprocess import whiten
5+
6+
def test_1d():
7+
test_data = numpy.array([1, 3, 5, 7])
8+
whitened = whiten(test_data)
9+
assert_allclose(whitened.mean(), 0)
10+
assert_allclose(whitened.std(), 1)
11+
assert_allclose(whitened*test_data.std() + test_data.mean(),
12+
test_data)
13+
def test_2d():
14+
test_data = numpy.array([[1, 3, 5, 7],
15+
[2, 3, 4, 1]])
16+
whitened = whiten(test_data)
17+
assert_allclose(whitened.mean(), 0)
18+
assert_allclose(whitened.std(), 1)
19+
assert_allclose(whitened*test_data.std() + test_data.mean(),
20+
test_data)

0 commit comments

Comments
 (0)