-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_loader.py
37 lines (26 loc) · 867 Bytes
/
data_loader.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
from random import Random
import numpy as np
class Partition:
def __init__(self, data, index):
self.data = data
self.index = index
def __len__(self):
return len(self.index)
def __getitem__(self, item):
data_idx = self.index[item]
return self.data[data_idx]
class DataPartitioner:
def __init__(self, data, sizes=[0.7, 0.2, 0.1], seed=1111):
self.data = data
self.partitions = []
rand = Random()
rand.seed(seed)
data_len = len(data)
indexes = np.arange(data_len)
rand.shuffle(indexes)
for frac in sizes:
part_len = int(frac * data_len)
self.partitions.append(indexes[:part_len])
indexes = indexes[part_len:]
def use(self, partition):
return Partition(self.data, self.partitions[partition])