-
Notifications
You must be signed in to change notification settings - Fork 150
/
colstats.py
69 lines (48 loc) · 1.11 KB
/
colstats.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
"""
compute column means and standard deviations from data in csv file
colstats.py <input file> <output file> [<label index>]
"""
import sys, csv
import numpy as np
from f_is_headers import *
print __doc__
input_file = sys.argv[1]
output_file = sys.argv[2]
try:
label_index = int( sys.argv[3] )
except IndexError:
label_index = False
i = open( input_file )
reader = csv.reader( i )
writer = csv.writer( open( output_file, 'wb' ))
# check headers
first_line = reader.next()
if not is_headers( first_line ):
# rewind
i.seek( 0 )
n = 0
sums_x = 0 # will be a np array
sums_x2 = 0 # will be a np array
for line in reader:
n += 1
if not label_index is False:
line.pop( label_index )
x = np.array( map( float, line ))
x2 = np.square( x )
sums_x += x
sums_x2 += x2
# preparation
print n
print sums_x
print sums_x2
means = sums_x / n
sums2_x = np.square( sums_x )
#print means
#print sums2_x
variances = sums_x2 / n - sums2_x / ( n ** 2 )
standard_deviations = np.sqrt( variances )
#print variances
#print standard_deviations
# save stats
writer.writerow( means )
writer.writerow( standard_deviations )