-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_manager.py
344 lines (305 loc) · 13.5 KB
/
data_manager.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
'''This class helps to create te desired synthetic dataset for experimentations'''
class DatasetManager:
def __init__(self, n_examples, n_features, version, factor=None):
self.n_examples = n_examples
self.n_features = n_features
self.y = None
self.version = version
self.data = None
self.noise = None
'''For outliers'''
self.factor = factor
print("Options: xor, outliers, circular, regression, regression_square, additive_non_linear,\nadditive_non_linear_with_product+2+3, mix_circular_additive, F1, F2, F3, F4, F5\n\nCorrelation: simple, non_linear, full")
def xor_data(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = -np.sign(self.data[:, 0]*self.data[:, 1])
y[y < 0] = 0
self.y = y.reshape(-1, 1)
def regression_square_data(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = (self.data[:, 0]*self.data[:, 1]
+ 0.5*np.square(self.data[:, 2])
+ 1
).reshape(-1, 1)
self.y = y
def regression_data(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = (4*self.data[:, 0]
- 4
+ self.noise
).reshape(-1, 1)
self.y = y
def outlier_data(self):
assert self.factor != None, "ERROR: Must provide an outlier factor coefficient!"
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 0.1, size=(self.n_examples))
y = (4*self.data[:, 0]
- 4
+ self.noise
).reshape(-1, 1)
ind = [x for x in range(self.n_examples)]
index_outliers = np.random.choice(ind, size=20, replace=False)
for i in index_outliers:
p = np.random.random()
if p > 0.5:
self.data[i, 0] = self.data[i, 0] + \
np.std(self.data[:, 0])*self.factor
else:
self.data[i, 0] = self.data[i, 0] - \
np.std(self.data[:, 0])*self.factor
self.y = y
def circular_data(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = np.round(1/(1+np.exp(
4*np.square(self.data[:, 0])
- 2*np.square(self.data[:, 1])
+ np.square(self.data[:, 2])
- 3*np.square(self.data[:, 3])
)
)
).reshape(-1, 1)
self.y = y
def circular_data_reg(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = (
4*np.square(self.data[:, 0])
- 2*np.square(self.data[:, 1])
+ np.square(self.data[:, 2])
- 3*np.square(self.data[:, 3])
).reshape(-1, 1)
self.y = y
def additive_non_linear(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = np.round(1/(1+np.exp((-10*np.sin(2*self.data[:, 0])
+ 2*np.abs(self.data[:, 1])
+ self.data[:, 2]
- np.exp(-self.data[:, 3])
)
)
)).reshape(-1, 1)
self.y = y
def additive_non_linear_reg(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = (-10*np.sin(2*self.data[:, 0])
+ 2*np.abs(self.data[:, 1])
+ self.data[:, 2]
- np.exp(-self.data[:, 3])
).reshape(-1, 1)
self.y = y
def additive_non_linear_with_product(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = np.round(1/(1+np.exp(-(4*self.data[:, 0]*self.data[:, 1]*self.data[:, 2]
+ self.data[:, 3]*self.data[:, 4]*self.data[:, 5]
)))).reshape(-1, 1)
self.y = y
def additive_non_linear_with_product_reg(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = (4*self.data[:, 0]*self.data[:, 1]*self.data[:, 2]
+ self.data[:, 3]*self.data[:, 4]*self.data[:, 5]
).reshape(-1, 1)
self.y = y
def additive_non_linear_with_product2(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = np.round(1/(1+np.exp(-(10*np.sin(self.data[:, 0]*self.data[:, 1]*self.data[:, 2])
+ np.abs(self.data[:, 3]*self.data[:, 4]*self.data[:, 5])
)))).reshape(-1, 1)
self.y = y
def additive_non_linear_with_product2_reg(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = (10*np.sin(self.data[:, 0]*self.data[:, 1]*self.data[:, 2])
+ np.abs(self.data[:, 3]*self.data[:, 4]*self.data[:, 5])
).reshape(-1, 1)
self.y = y
def additive_non_linear_with_product3(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = np.round(1/(1+np.exp((-20*np.sin(2*self.data[:, 0]*self.data[:, 1])
+ 2*np.abs(self.data[:, 2])
+ self.data[:, 3]*self.data[:, 4]
- 4*np.exp(-self.data[:, 5])
)
)
)).reshape(-1, 1)
self.y = y
def additive_non_linear_with_product3_reg(self):
self.data = np.random.normal(
0, 1, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 1, size=(self.n_examples))
y = (-20*np.sin(2*self.data[:, 0]*self.data[:, 1])
+ 2*np.abs(self.data[:, 2])
+ self.data[:, 3]*self.data[:, 4]
- 4*np.exp(-self.data[:, 5])
).reshape(-1, 1)
self.y = y
def F1(self):
self.data = np.random.uniform(
0, 1, size=(self.n_examples, self.n_features))
self.data[:, 3] = np.random.uniform(0.6, 1)
self.data[:, 4] = np.random.uniform(0.6, 1)
self.data[:, 7] = np.random.uniform(0.6, 1)
self.data[:, 9] = np.random.uniform(0.6, 1)
y = ((np.pi**(self.data[:, 0]*self.data[:, 1]))*np.sqrt(self.data[:, 2])
+ 1/np.sin(self.data[:, 3]) +
np.log(self.data[:, 2]+self.data[:, 4])
- (self.data[:, 8]/self.data[:, 9]) *
np.sqrt(self.data[:, 6]/self.data[:, 7])
- self.data[:, 1]*self.data[:, 6]
).reshape(-1, 1)
self.y = y
def F2(self):
self.data = np.random.uniform(-1, 1,
size=(self.n_examples, self.n_features))
y = (np.exp(np.abs(self.data[:, 0]-self.data[:, 1]))
+ np.abs(self.data[:, 1]*self.data[:, 2])
- np.square(self.data[:, 2])**np.abs(self.data[:, 3])
+ np.square(self.data[:, 0]*self.data[:, 3])
+ np.log(np.square(self.data[:, 3]) +np.square(self.data[:, 4]) +
np.square(self.data[:, 6])+np.square(self.data[:, 7]))
+ self.data[:, 8]
+ 1/(1+np.square(self.data[:, 9]))
).reshape(-1, 1)
self.y = y
def F3(self):
self.data = np.random.uniform(-1, 1,
size=(self.n_examples, self.n_features))
y = (np.sin(np.abs(self.data[:, 0]*self.data[:, 1])+1)
- np.log(np.abs(self.data[:, 2]*self.data[:, 3])+1)
+ np.cos(self.data[:, 4]+self.data[:, 5]-self.data[:, 7])
+ np.sqrt(np.square(self.data[:, 7]) +np.square(self.data[:, 8]) +
np.square(self.data[:, 9]))
).reshape(-1, 1)
self.y = y
def F4(self):
self.data = np.random.uniform(-1, 1,
size=(self.n_examples, self.n_features))
y = ( np.tanh(self.data[:, 0]*self.data[:, 1]+self.data[:, 2]*self.data[:, 3])*np.sqrt(np.abs(self.data[:, 4]))
+ np.log((self.data[:, 5]*self.data[:, 6]*self.data[:, 7])**2 + 1)
+ self.data[:, 8]*self.data[:, 9]
+ 1./(1+np.abs(self.data[:, 9]))
).reshape(-1, 1)
self.y = y
def F5(self):
self.data = np.random.uniform(-1, 1,
size=(self.n_examples, self.n_features))
y = (np.cos(self.data[:, 0]*self.data[:, 1]*self.data[:, 2])
+ np.sin(self.data[:, 3]*self.data[:, 4]*self.data[:, 5])
).reshape(-1, 1)
self.y = y
def mix_circular_additive(self):
self.data = np.random.normal(
0, 2, size=(self.n_examples, self.n_features))
self.noise = np.random.normal(0, 4, size=(self.n_examples))
y1 = np.round(1/(1+np.exp(
np.square(self.data[:self.n_examples//2, 5])
+ np.square(self.data[:self.n_examples//2, 6])
+ np.square(self.data[:self.n_examples//2, 7])
+ np.square(self.data[:self.n_examples//2, 8])
- 4
)
)
).reshape(-1, 1)
y2 = np.round(1/(1+np.exp(
-100*np.sin(2*self.data[self.n_examples//2:, 5])
+ 2*np.abs(self.data[self.n_examples//2:, 6])
+ self.data[self.n_examples//2:, 7]
+ np.exp(-self.data[self.n_examples//2:, 8])
)
)
).reshape(-1, 1)
y_loc = np.concatenate([y1, y2], axis=0)
self.y = y_loc
def correlation_simple(self):
self.data[:, 0] = 3*self.data[:, 1]+9
def correlation_non_linear(self):
self.data[:, 9] = np.sin(np.exp(self.data[:, 7]))
def correlation_both(self):
self.data[:, 9] = np.sin(np.exp(self.data[:, 7]))
self.data[:, 3] = 2*self.data[:, 5]+0.5
def plot_distribution(self):
fig = plt.figure(figsize=(5, 5))
plt.hist(self.y)
def plot_outliers(self):
fig = plt.figure(figsize=(5, 5))
plt.boxplot(self.data[:, 0])
def generate_dataset(self, correlation=None, display=True):
'''Datasets'''
if self.version is "xor":
self.xor_data()
elif self.version is "outlier":
self.outlier_data()
elif self.version is "circular":
self.circular_data()
elif self.version is "circular_reg":
self.circular_data_reg()
elif self.version is "regression":
self.regression_data()
elif self.version is "regression_square":
self.regression_square_data()
elif self.version is "additive_non_linear":
self.additive_non_linear()
elif self.version is "additive_non_linear_reg":
self.additive_non_linear_reg()
elif self.version is "additive_non_linear_with_product":
self.additive_non_linear_with_product()
elif self.version is "additive_non_linear_with_product2":
self.additive_non_linear_with_product2()
elif self.version is "additive_non_linear_with_product3":
self.additive_non_linear_with_product3()
elif self.version is "additive_non_linear_with_product_reg":
self.additive_non_linear_with_product_reg()
elif self.version is "additive_non_linear_with_product2_reg":
self.additive_non_linear_with_product2_reg()
elif self.version is "additive_non_linear_with_product3_reg":
self.additive_non_linear_with_product3_reg()
elif self.version is "mix_circular_additive":
self.mix_circular_additive()
elif self.version is "F1":
self.F1()
elif self.version is "F2":
self.F2()
elif self.version is "F3":
self.F3()
elif self.version is "F4":
self.F4()
elif self.version is "F5":
self.F5()
'''Correlations'''
if correlation is "simple":
self.correlation_simple()
elif correlation is "non_linear":
self.correlation_non_linear()
elif correlation is "full":
self.correlation_both()
assert (self.y is not None), "Please provide a correct distribution"
if display is True:
self.plot_distribution()
if self.version is "outlier":
self.plot_outliers()
return self.y, self.data