-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessor.py
58 lines (33 loc) · 1.49 KB
/
preprocessor.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
#!/usr/bin/env python
# encoding: utf-8
# Andre Anjos <[email protected]>
# Thu 18 Jun 14:24:51 CEST 2015
'''A simple pre-processing that applies Z-normalization to the input
features'''
import numpy
def estimate_norm(X):
'''Estimates the mean and standard deviation from a data set
Parameters:
X (numpy.ndarray): A 2D numpy ndarray in which the rows represent examples
while the columns, features of the data you want to estimate
normalization parameters on
Returns:
numpy.ndarray: A 1D numpy ndarray containing the estimated mean over
dimension 1 (columns) of the input data X
numpy.ndarray: A 1D numpy ndarray containing the estimated unbiased
standard deviation over dimension 1 (columns) of the input data X
'''
return X.mean(axis=0), X.std(axis=0, ddof=1)
def normalize(X, norm):
'''Applies the given norm to the input data set
Parameters:
X (numpy.ndarray): A 3D numpy ndarray in which the rows represent examples
while the columns, features of the data set you want to normalize. Every
depth corresponds to data for a particular class
norm (tuple): A tuple containing two 1D numpy ndarrays corresponding to the
normalization parameters extracted with :py:func:`estimated_norm` above.
Returns:
numpy.ndarray: A 3D numpy ndarray with the same dimensions as the input
array ``X``, but with its values normalized according to the norm input.
'''
return numpy.array([(k - norm[0]) / norm[1] for k in X])