-
Notifications
You must be signed in to change notification settings - Fork 1
/
datasets.py
130 lines (79 loc) · 2.91 KB
/
datasets.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
from metrics import Task
from preprocessing import one_hot_encoding
class Dataset():
def __init__(self, name, task = Task.REGRESSION):
self.name = name
self.task = task
self.X_TR, self.Y_TR = self.readfile(self.train_suffix)
self.X_TS, self.Y_TS = self.readfile(self.test_suffix)
self.tr_size = len(self.X_TR)
self.ts_size = len(self.X_TS)
def size(self):
return self.input_size, self.output_size
def cardinality(self):
return len(self.X_TR)
def __str__(self):
return f"{self.name}"
class Monk(Dataset):
def __init__(self, n):
self.path = f'DATASETS/MONK/monks-{n}.'
self.train_suffix = 'train'
self.test_suffix = 'test'
self.input_size = 6
self.output_size = 1
super().__init__(f"MONK{n}", Task.BINARY_CLASSIFICATION)
def readfile(self, set):
file = open(self.path + set, 'r')
x = []
y = []
for line in file.readlines():
vals = line.split(" ")
x.append([list(map(lambda x_i: int(x_i), vals[2:8]))])
y.append([[int(vals[1])]])
x = np.array(x)
y = np.array(y)
return x, y
def getTR(self, one_hot = False):
if one_hot == True:
X_TR, input_size = one_hot_encoding(self.X_TR)
self.input_size = input_size
return X_TR, self.Y_TR
def getTS(self, one_hot = False):
if one_hot == True:
X_TS, input_size = one_hot_encoding(self.X_TS)
self.input_size = input_size
return X_TS, self.Y_TS
def getAll(self, one_hot = False):
X_TR, Y_TR = self.getTR(one_hot)
X_TS, Y_TS = self.getTS(one_hot)
return X_TR, Y_TR, X_TS, Y_TS
class CUP(Dataset):
def __init__(self, internal_split = True):
self.path = 'DATASETS/CUP/internal-CUP-' if internal_split else 'DATASETS/CUP/ML-CUP21-'
self.internal_split = internal_split
self.train_suffix = 'TR'
self.test_suffix = 'TS'
self.input_size = 10
self.output_size = 2
super().__init__("CUP", Task.REGRESSION)
def readfile(self, set):
file = open(f'{self.path}{set}.csv', 'r')
x = []
y = []
for line in file.readlines():
if (line.startswith("#")):
continue
vals = line.split(",")
x.append([list(map(lambda x_i: float(x_i), vals[1:11]))])
if set == self.train_suffix or self.internal_split:
y.append([list(map(lambda y_i: float(y_i), vals[11:13]))])
x = np.array(x)
y = np.array(y)
return x, y
def getTR(self):
return self.X_TR, self.Y_TR,
def getTS(self):
return self.X_TS, self.Y_TS
def getAll(self):
return self.X_TR, self.Y_TR, self.X_TS, self.Y_TS