-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature.py
35 lines (31 loc) · 1.07 KB
/
feature.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
import collections
import functools
import numpy as np
import scipy.sparse
named_features = collections.defaultdict(list)
def feature(name):
'''
decorator for specific classifier, the wrapped function should accept as an input a corpus and return a matrix of vector features.
'''
def feature_wrapper(func):
@functools.wraps(func)
def wrapper(*args, **kwds):
return func(*args, **kwds)
named_features[name].append(wrapper)
return wrapper
return feature_wrapper
def fitter(name, inputs):
'''
The fitter function takes all features function that `name` owns and run them one by one on the inputs (corpus).
Each of those functions return a matrix, which they are all concatenated into one big matrix (concatenated by extending lines).
'''
matrices = []
for f in named_features[name]:
matrices.append(f(inputs))
a = matrices[0]
for b in matrices[1:]:
try:
a = np.concatenate((a, b), axis=1)
except:
a = scipy.sparse.hstack([a,b])
return a