/
util.py
33 lines (27 loc) · 883 Bytes
/
util.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
import sys
import numpy as np
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i + n]
class ChunkedTransform(object):
verbose = 0
def transform(self, X):
features = None
for chunk in chunks(X, self.batch_size):
if features is not None:
features = np.vstack(
[features, self._compute_features(chunk)])
else:
features = self._compute_features(chunk)
if self.verbose:
sys.stdout.write(
"\r[%s] %d%%" % (
self.__class__.__name__,
100. * len(features) / len(X),
))
sys.stdout.flush()
if self.verbose:
sys.stdout.write('\n')
return features