-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfeature_matrix_statistics.py
More file actions
74 lines (57 loc) · 2.08 KB
/
Copy pathfeature_matrix_statistics.py
File metadata and controls
74 lines (57 loc) · 2.08 KB
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
# Copyright (C) 2021-2022 Dino Bollinger, ETH Zürich, Information Security Group
# Released under the MIT License
"""
Takes as input a feature matrix and outputs which features are most used.
Usage:
feature_matrix_statistics.py <train_data> <fmap>
Options:
-h Help
"""
from docopt import docopt
import logging
import os
import re
import numpy as np
import pickle
logger = logging.getLogger("matrix_statistics")
def setupLogger(logdir: str, loglevel: str) -> None:
"""
Set up the logger instance, which will write its output to stderr.
:param loglevel: Log level at which to record.
"""
loglevel = logging.getLevelName(loglevel)
logger.setLevel(loglevel)
""" Enables logging to stderr """
formatter = logging.Formatter('%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s', datefmt="%Y-%m-%d-%H:%M:%S")
ch = logging.StreamHandler()
ch.setLevel(loglevel)
ch.setFormatter(formatter)
logger.addHandler(ch)
def main() -> int:
argv = None
args = docopt(__doc__, argv=argv)
setupLogger(".", "INFO")
if args["<train_data>"] is not None:
train_data = args["<train_data>"]
if not os.path.exists(train_data):
logger.error("Training data file does not exist.")
return 1
with open(args["<train_data>"], 'rb') as fd:
sparse_mat = pickle.load(fd)
feature_map = dict()
with open(args["<fmap>"], 'r') as fd:
for l in fd:
mobj = re.search("([0-9]*) (.*) ", l)
if mobj:
feature_map[int(mobj.group(1))] = mobj.group(2)
all_counts = list()
dtrain = sparse_mat.todense()
print(f"Number of cookies total: {dtrain.shape[0]}")
print(f"Number of features total: {dtrain.shape[1]}")
for i in range(dtrain.shape[1]):
count: int = np.count_nonzero(dtrain[:, i])
all_counts.append((feature_map[i], count))
for feat, c in sorted(all_counts, key=lambda x: x[1], reverse=True):
print(f"{feat}, {c}")
if __name__ == "__main__":
exit(main())