-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_generator.py
82 lines (65 loc) · 2.05 KB
/
data_generator.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from sklearn import datasets
import numpy as np
import csv
import sqlite3
DB_NUM_ROWS = 10000
def create_db_connection(db_file):
try:
conn = sqlite3.connect(db_file)
return conn
except sqlite3.Error as e:
print(e)
return None
def get_db_dataset(conn):
print("Getting Data")
cur = conn.cursor()
data = []
devices = ["\"SwannOne SoundView Outdoor Camera\"",
"\"DLink Camera\"",
"\"Google-Home\"",
"\"WeMo Switch\"",
"\"SwannOne Smart Hub\"",
"\"Philips Hue Bulbs\"",
"\"rpi-bonesi\""
]
for curr_name in devices:
print("Extracting row from database:", curr_name)
data += cur.execute(f'''SELECT DISTINCT * FROM flow_features WHERE name = {curr_name} AND ''' +
f'''abs(CAST(random() AS REAL)) ''' +
f'''/ 9223372036854775808 < 0.5 LIMIT {DB_NUM_ROWS}''').fetchall()
print("Loaded all the data")
return data
def read_from_db(db_file):
conn = create_db_connection(db_file)
if conn is None:
exit(1)
db_rows = get_db_dataset(conn)
data = []
target = []
for row in db_rows:
vals = row[1:]
tar = row[0]
data.append(list(map(np.float64, vals)))
target.append(tar)
return np.copy(data), np.copy(target)
def read_from_file(filename):
data = []
target = []
with open(filename, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
vals = row[:-1]
tar = row[-1]
data.append(list(map(np.float64, vals)))
target.append(tar)
return np.array(data), np.array(target)
def main(*args, **kwargs):
l = datasets.load_digits()
digits = l.data
targets = l.target
for digit, target in list(map(lambda x: tuple(x), list(zip(digits, targets)))):
row = ','.join(list(map(str, digit)))
row += ',' + str(target)
print(row)
if __name__ == '__main__':
main()