-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatasetHandler.py
46 lines (38 loc) · 1.45 KB
/
DatasetHandler.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
import csv
import numpy as np
from sklearn.preprocessing import StandardScaler
class Dataset:
def __init__(self, filename, modes):
self.modes = modes
self.filename = filename
print('Dataset handler is ready using ', filename)
def WriteRow(self, row):
with open(self.filename, 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(row)
def ReadCSV(self, transform):
try:
x, y = [], []
corrupted = 0
with open(self.filename, 'r') as file:
csvreader = csv.reader(file)
for row in csvreader:
if len(row) == 15:
x.append(row[0 : 14])
y.append(row[14])
else:
corrupted += 1
x, y = x[1:], y[1:]
if transform:
scaler = StandardScaler()
x = scaler.fit_transform(x)
classes = list(set(y))
y = [classes.index(i) for i in y]
if self.modes['Info']:
print('Total Classes Detected', classes)
print('Corrupted entries found:', corrupted)
return np.array(x, dtype='int'), np.array(y, dtype='int'), classes
except Exception as e:
if self.modes['Warning']:
print(e)
return False