-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream.py
53 lines (45 loc) · 1.25 KB
/
stream.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
class Stream(object):
data = None
initialData = None
"""
Initialize a stream by reading data from file.
Input data file formats: ARFF or Sparse (.data)
"""
def __init__(self, filename, classList, initialSize):
self.data = []
self.initialData = []
self.MAXVAR = self.__readData(filename, classList, initialSize)
"""
Read data from file in CSV or Sparse format.
Return maximum number of variables.
"""
def __readData(self, filename, classList, initialSize):
with open(filename) as f:
data = f.readlines()
maxvar = 0
for i in data:
d = {}
if filename.endswith('.csv'):
features = i.strip().split(',')
if features[-1] not in classList:
classList.append(features[-1])
d[-1] = float(classList.index(features[-1]))
for j in xrange(len(features)-1):
d[j] = float(features[j])
maxvar = len(features)-1
else:
features = i.strip().split(' ')
for fea in features:
val = fea.strip().split(':')
if len(val) < 2:
d[-1] = float(val[0])
else:
d[int(val[0])-1] = float(val[1])
#get maximum number of features
if maxvar < int(val[0]):
maxvar = int(val[0])
if len(self.initialData) < initialSize:
self.initialData.append(d)
else:
self.data.append(d)
return maxvar