-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize-data-final.py
81 lines (58 loc) · 2 KB
/
normalize-data-final.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
#Author: Jeremy Keys
#Last modified: 11-28-17
import sys
#constants
DEBUG = True
INPUT_FILE_NAME_TRAIN = "pp5i_train.gr.csv"
OUTPUT_FILE_NAME_TRAIN = "pp5i_train.gr.normalized.csv"
INPUT_FILE_NAME_TEST = "pp5i_test.gr.csv"
OUTPUT_FILE_NAME_TEST = "pp5i_test.gr.normalized.csv"
def debug(logMsg):
if DEBUG:
print(logMsg)
#just for a little bit of house keeping/debugging, we will keep a running list of every line that's been modified
modified_lines_train = []
modified_lines_test = []
def proc_file(input_file_name, output_file_name):
#https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list
with open(input_file_name) as f:
modified_lines = []
#open the file which will be the train output; we don't want to append
out_file = open(output_file_name, "w")
#create a list of lines, stripped of the newline
content = [line.rstrip('\n') for line in f]
firstValue = True
firstRecord = True
for line in content:
if len(line) <= 2:
continue
modified_line = []
int_strings = line.split(',')
#don't normalize the ID numbers
if firstRecord:
modified_lines.append(line + "\n")
firstRecord = False
continue
#print(int_strings)
for s in int_strings:
if (firstValue):
firstValue = False
modified_line.append(int_strings[0]);
#int_strings.pop(0);
continue
if(s.isspace() or s == ""):
continue
if(int(s) < 20):
modified_line.append(str(20))
elif (int(s) > 1600):
modified_line.append(str(1600))
else:
modified_line.append(s)
#join the normalized values together again with commas, append the stripped newline
modified_lines.append(",".join(modified_line) + "\n")
firstValue = True
out_file.writelines(modified_lines)
return modified_lines
modified_lines_train = proc_file(INPUT_FILE_NAME_TRAIN, OUTPUT_FILE_NAME_TRAIN)
modified_lines_test = proc_file(INPUT_FILE_NAME_TEST, OUTPUT_FILE_NAME_TEST)
#print(modified_lines_train)