-
Notifications
You must be signed in to change notification settings - Fork 54
/
standardize.py
61 lines (44 loc) · 1.22 KB
/
standardize.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
'standardize (shift and scale to zero mean and unit standard deviation) data from csv file'
'meant to be used together with colstats.py'
'standardize.py <stats file> <input file> <output file> [<label index>]'
import sys, csv
import numpy as np
from f_is_headers import *
stats_file = sys.argv[1]
input_file = sys.argv[2]
output_file = sys.argv[3]
try:
label_index = int( sys.argv[4] )
except IndexError:
label_index = False
i = open( input_file )
stats_reader = csv.reader( open( stats_file ))
reader = csv.reader( i )
writer = csv.writer( open( output_file, 'wb' ))
# get stats
means = stats_reader.next()
means = np.array( map( float, means ))
standard_deviations = stats_reader.next()
standard_deviations = np.array( map( float, standard_deviations ))
# check headers
first_line = reader.next()
if is_headers( first_line ):
headers = first_line
else:
headers = False
i.seek( 0 )
# go
for line in reader:
if not label_index is False:
l = line.pop( label_index )
print l
x = np.array( map( float, line ))
# shift and scale
x = x - means
x = x / standard_deviations
if not label_index is False:
# -1.0,...
#x = np.insert( x, 0, l )
line = list( x )
line.insert( 0, l )
writer.writerow( line )