-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_splits.py
45 lines (37 loc) · 1.61 KB
/
generate_splits.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
import sys
from CSVParser import CSVParser
def libsvm_entry(features, label):
return str(label) + ' ' + ' '. \
join(['%d:%f' % (i+1, f) for (i, f) in enumerate(features)])
def generate_training_splits(feature_file, csv_file, prefix):
for i in range(10):
train_folds = range(10)
test_fold = [train_folds.pop(i)]
fp = open('%s.train.%d.data' % (prefix, i), 'w')
fp.write(feature_file_to_train(feature_file, train_folds,
csv_file, test=False).encode('utf-8'))
fp.close()
fp = open('%s.test.%d.data' % (prefix, i), 'w')
fp.write(feature_file_to_train(feature_file, test_fold,
csv_file, test=True).encode('utf-8'))
fp.close()
def feature_file_to_train(feature_file, folds,
csv_file='anonymized_user_manifest.csv', test=False):
csv_data = CSVParser(csv_file)
result = []
features = {}
user_ids = csv_data.get_user_ids(folds, test)
for line in open(feature_file):
line = line.split(',')
user_id = line[0].strip()
feature = [float(x.strip()) for x in line[1:]]
features[user_id] = feature
for user_id in user_ids:
result.append(libsvm_entry(features[user_id],
csv_data.get_label(user_id)))
return '\n'.join(result) + '\n'
if __name__ == '__main__':
feature_file = sys.argv[1] # 'liwc.feature'
csv_file = sys.argv[2] # anonymized_user_manifest.csv
prefix = sys.argv[3] # 'result/liwc'
generate_training_splits(feature_file, csv_file, prefix)