-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabletocsv.py
62 lines (54 loc) · 1.79 KB
/
tabletocsv.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
# table to csv
import fileinput
import csv
import sys
import argparse
from sklearn import preprocessing
import numpy as np
########################################################################
# INPUT:
# table of conditional frequency distribution as standard input, e.g.:
# word1 word2 word3 ...
# 1 0 1 2
# 2 2 0 0
# ...
#
# OUTPUT: to frequencydata.csv where each row has the word frequencies
# for one particular article, e.g.:
# (line 1) 0 1 2 0 ...
# (line 2) 1 0 1 0 ...
########################################################################
data = []
parser = argparse.ArgumentParser(description='Convert frequency distribution table into a .csv file that can be fed into the classifier')
parser.add_argument('--features', '-f', nargs=1, default=None,
help='Any additional features in a .csv file')
parser.add_argument('output', nargs=1,
help='Name of output file for X data')
args = parser.parse_args()
first = 0
for line in fileinput.input(files='-'):
# skip the first 2 rows because we don't want the labels or dummy row
if first <= 1:
first += 1
continue
# split on whitespace and cast to int
data.append([int(k) for k in line.split()])
if args.features is not None:
with open(args.features[0], 'r', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
k = 0
for row in reader:
data[k].append(row[0])
data[k].append(row[1])
k += 1
# scale data with sklearn
X = np.array(data)
X = np.delete(X, 0, 1)
scaler = preprocessing.MaxAbsScaler()
X = scaler.fit_transform(X)
# write the data to a .csv file
with open(args.output[0], 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for datum in X:
# skip first column because we don't want the labels
writer.writerow(datum)