-
Notifications
You must be signed in to change notification settings - Fork 22
/
sparsedicttocsrmatrix.py
58 lines (53 loc) · 1.78 KB
/
sparsedicttocsrmatrix.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
"""
Convert a sparse dict feature to a scipy.sparse.csr_matrix.
"""
import common.idmap
import scipy.sparse
import numpy
import logging
import common.str
from collections import defaultdict
import sys
class SparseDictToCSRMatrix:
def __init__(self):
return
def train(self, features, mincount=None):
"""
Create a feature map and return a sparse matrix over these features.
"""
# print features
if mincount is None:
keys = set()
for f in features:
keys.update(f.keys())
else:
keycnt = defaultdict(int)
for fvec in features:
for f in fvec:
keycnt[f] += 1
tot = len(keycnt)
for f in keycnt.keys():
if keycnt[f] < mincount:
del keycnt[f]
print >> sys.stderr, "SparseDictToCSRMatrix.train kept %s features with mincount threshold %d" % (common.str.percent(len(keycnt), tot), mincount)
keys = keycnt.keys()
self.idmap = common.idmap.IDmap(keys)
return self.__call__(features)
def __call__(self, features):
data = []
row = []
col = []
assert type(features) == list
for k, f in enumerate(features):
keyvalues = []
for key in f:
if not self.idmap.exists(key):
# logging.debug("KEY %s does not exist" % key)
continue
keyvalues.append((self.idmap.id(key), f[key]))
keyvalues.sort()
for key, value in keyvalues:
data.append(value)
row.append(k)
col.append(key)
return scipy.sparse.csr_matrix((data, (row, col)), shape=(len(features), self.idmap.len))