-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.py
executable file
·161 lines (146 loc) · 4.79 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python3
import csv
import sys
import pdb
import operator
import copy
def normalize(data, feature_num = None):
data = copy.deepcopy(data)
if feature_num != None:
features = [example[feature_num] for example in data]
max_f = max(features)
min_f = min(features)
for i in range(len(data)):
data[i][feature_num] = (data[i][feature_num] - min_f) / (max_f - min_f)
else:
max_f = max(data)
min_f = min(data)
for f in range(len(data)):
data[f] = (data[f] - min_f) / (max_f - min_f)
return data
# Function: read_csv
# Inputs: path to csv file
# Outputs: data - [[feature1, feature1, ...],
# [feature2, feature2, ...],
# ...
# [label, label, ...]
# ]
# Purpose: Opens and reads CSV file
def read_csv(path, headers = False):
data_file = open(path, "r")
data = list(csv.reader(data_file))
data_file.close()
if headers:
data.pop(0)
for i in range(len(data)):
for j in range(len(data[i])):
try:
data[i][j] = float(data[i][j])
except:
data[i][j] = data[i][j]
return data
# Function: sort_data
# Inputs: Array of Arrays, with feature_num being sub-index
# Outputs: Input, but sorted by feature_num
# Purpose: Sort an array of arrays by sub array index
def sort_data(data, feature_num):
for example in data:
example[feature_num] = float(example[feature_num])
return sorted(data, key=operator.itemgetter(feature_num))
# Function: write_csv
# Inputs: example set (data), path to .csv file
# Outputs: None
# Purpose: Write data to .csv file
def write_csv(path, data):
output_file = open(path, 'w')
csv_writer = csv.writer(output_file)
for x in data:
csv_writer.writerow(x)
output_file.close()
def equidense(num_bins, data, feature_num):
bin_length = len(data) / num_bins
current_bound = bin_length
current_bin = 0
binned_data = []
i = 0
for example in data:
example[feature_num] = current_bin
binned_data.append(data[i])
if i == current_bound:
current_bin += 1
current_bound += bin_length
i += 1
return binned_data
# Function: equidistant
# Inputs: bin_length: float
# data: [[feature_1, feature_2, ..., class_label],
# ...,
# ]
# feature_num: index of feature to bin
# Outputs: binned_data: Same format as data, with feature_num put into bins
# Purpose: sort the given feature into bins using equidistant method
def equidistant(num_bins, data, feature_num):
#Get Bin Length
maximum = max([float(example[feature_num]) for example in data])
minimum = min([float(example[feature_num]) for example in data])
data_range = maximum - minimum
bin_length = data_range / num_bins
#Get original Bounds
current_bin = 0
previous_bound = minimum
current_bound = minimum + bin_length
binned_data = []
i = 0
while i < len(data):
if data[i][feature_num] >= previous_bound and data[i][feature_num] <= current_bound:
data[i][feature_num] = current_bin
binned_data.append(data[i])
i += 1
else:
current_bin = current_bin + 1
previous_bound = current_bound
if current_bin == num_bins - 1:
current_bound = maximum
else:
current_bound += bin_length
return binned_data
def bin_example(example, minimum, maximum, num_bins, feature_num):
boundaries = np.arange(minimum, maximum, num_bins)
bin_num = 0
for i in range(len(boundaries) - 1):
f = example[feature_num]
pb = boundaries[i]
cb = boundaries[i+1]
if f > pb and f < cb:
example[feature_num] = i
return example
# Function: bin_data
# Inputs: An array of examples, num_bins, the feature to bin, and either 'equidistant' or 'equidense'
# Outputs: A modified array of examples with feature_num placed into bins
# Purpose: Select either equidistant or equidense binning methods and place the specified features
# into those bins
def bin_data(data, num_bins, feature_num, bin_method = 'equidistant'):
bin_length = 0
try:
data = sort_data(data, feature_num)
except:
return new_data # Must already be in strings, which are guaranteed to be discrete
if bin_method == 'equidistant':
binned_data = equidistant(num_bins, data, feature_num)
if bin_method == 'equidense':
binned_data = equidense(num_bins, data, feature_num)
return binned_data
# Function: main
# Inputs: None
# Outputs: None
# Purpose: Main
def main(input_file, num_bins, feature_num):
data = []
data = read_csv(input_file)
data = bin_data(data, num_bins, feature_num, bin_method='equidense')
write_csv('./data_files/binned_data.csv', data)
if __name__ == '__main__':
if len(sys.argv) < 3:
print('Usage: ./binner.py <input.csv> <num_bins> <feature_num>')
else:
main(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))