forked from paris-swc/oss-contribution-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
125 lines (101 loc) · 2.87 KB
/
preprocess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
A few functions to pre-process data.
"""
import numpy as np
def center(data, desired=0.0):
"""
Return the data with its mean shifted to the desired value.
Parameters
----------
data : ndarray
The data to center.
desired : float, optional
The new desired center of the data. Defaults to 0.0 if not specified.
Returns
-------
centered : ndarray
The data centered around the desired value.
"""
return (data - data.mean()) + desired
def whiten(data):
"""
Return a whitened copy of the data, i.e. data with zero mean and unit
variance.
Parameters
----------
data : ndarray
The data to whiten.
Returns
-------
whitened : ndarray
The whitened data.
"""
return center(data) / data.std()
def value_range(data):
"""
Return the range of the values in ``data``, i.e. the distance between its
lowest and the highest value.
Parameters
----------
data : ndarray
The data.
Returns
-------
range : number
The distance between the lowest and the highest value of ``data``.
"""
return 0.0
def rescale(data, lower=0.0, upper=1.0):
"""
(Linearly) rescale the data so that it fits into the given range.
Parameters
----------
data : ndarray
The data to rescale.
lower : number, optional
The lower bound for the data. Defaults to 0.
upper : number, optional
The upper bound for the data. Defaults to 1.
Returns
-------
rescaled : ndarray
The data rescaled between ``lower`` and ``upper``.
"""
return np.array([lower, upper])
def cut_to_same_size(data_1, data_2):
"""
Returns the two given arrays, cut so their length is the length of the
shorter one, i.e. so that the two arrays have the same length.
Parameters
----------
data_1 : ndarray
The first array.
data_2 : ndarray
The second array.
Returns
-------
cut_1, cut_2 : (ndarray, ndarray)
The two original arrays, the longer one cut at the end to the length of
the shorter one so that both arrays have the same length.
"""
return data_1, data_2
def pad_to_same_size(data_1, data_2, pad_with=0.0):
"""
Returns the two given arrays, the shorter one padded so that its length is
the length of the longer one, i.e. so that the two arrays have the same
length.
Parameters
----------
data_1 : ndarray
The first array.
data_2 : ndarray
The second array.
pad_with : number, optional
The value used for padding at the end. Defaults to 0.
Returns
-------
cut_1, cut_2 : (ndarray, ndarray)
The two original arrays, the shorter padded at the end with the values
given as ``pad_width`` so that both arrays have the same length.
"""
return data_1, data_2