-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_data_operations_implementations.py
660 lines (518 loc) · 26.6 KB
/
test_data_operations_implementations.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import os
import numpy as np
import pytest
from examples.simple.classification.classification_with_tuning import get_classification_dataset
from examples.simple.regression.regression_with_tuning import get_regression_dataset
from examples.simple.time_series_forecasting.gapfilling import generate_synthetic_data
from fedot.core.data.data import InputData
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.data.supplementary_data import SupplementaryData
from fedot.core.operations.evaluation.operation_implementations.data_operations.sklearn_imbalanced_class import \
ResampleImplementation
from fedot.core.operations.evaluation.operation_implementations.data_operations. \
sklearn_transformations import ImputationImplementation
from fedot.core.operations.evaluation.operation_implementations.data_operations.ts_transformations import \
CutImplementation, LaggedTransformationImplementation
from fedot.core.operations.operation_parameters import OperationParameters
from fedot.core.pipelines.node import PipelineNode
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.repository.dataset_types import DataTypesEnum
from fedot.core.repository.operation_types_repository import OperationTypesRepository
from fedot.core.repository.tasks import Task, TaskTypesEnum, TsForecastingParams
from fedot.preprocessing.data_types import TYPE_TO_ID
from test.unit.preprocessing.test_preprocessing_through_api import data_with_only_categorical_features
def get_small_regression_dataset():
""" Function returns features and target for train and test regression models """
features_options = {'informative': 2, 'bias': 2.0}
x_train, y_train, x_test, y_test = get_regression_dataset(features_options=features_options,
samples_amount=70,
features_amount=4)
y_train = y_train.reshape((-1, 1))
y_test = y_test.reshape((-1, 1))
# Define regression task
task = Task(TaskTypesEnum.regression)
# Prepare data to train the model
train_input = InputData(idx=np.arange(0, len(x_train)),
features=x_train,
target=y_train,
task=task,
data_type=DataTypesEnum.table)
predict_input = InputData(idx=np.arange(0, len(x_test)),
features=x_test,
target=None,
task=task,
data_type=DataTypesEnum.table)
return train_input, predict_input, y_test
def get_small_classification_dataset():
""" Function returns features and target for train and test classification models """
features_options = {'informative': 1, 'redundant': 0,
'repeated': 0, 'clusters_per_class': 1}
x_train, y_train, x_test, y_test = get_classification_dataset(features_options=features_options,
samples_amount=70,
features_amount=4,
classes_amount=2)
y_train = y_train.reshape((-1, 1))
y_test = y_test.reshape((-1, 1))
# Define regression task
task = Task(TaskTypesEnum.classification)
# Prepare data to train the model
train_input = InputData(idx=np.arange(0, len(x_train)),
features=x_train,
target=y_train,
task=task,
data_type=DataTypesEnum.table)
predict_input = InputData(idx=np.arange(0, len(x_test)),
features=x_test,
target=None,
task=task,
data_type=DataTypesEnum.table)
return train_input, predict_input, y_test
def get_time_series(len_forecast=5, length=80):
""" Function returns time series for time series forecasting task """
synthetic_ts = generate_synthetic_data(length=length)
train_data = synthetic_ts[:-len_forecast]
test_data = synthetic_ts[-len_forecast:]
task = Task(TaskTypesEnum.ts_forecasting,
TsForecastingParams(forecast_length=len_forecast))
train_input = InputData(idx=np.arange(0, len(train_data)),
features=train_data,
target=train_data,
task=task,
data_type=DataTypesEnum.ts)
start_forecast = len(train_data)
end_forecast = start_forecast + len_forecast
predict_input = InputData(idx=np.arange(start_forecast, end_forecast),
features=train_data,
target=None,
task=task,
data_type=DataTypesEnum.ts)
return train_input, predict_input, test_data
def get_multivariate_time_series(mutli_ts=False):
""" Generate several time series in one InputData block """
ts_1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape((-1, 1))
ts_2 = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]).reshape((-1, 1))
several_ts = np.hstack((ts_1, ts_2))
task = Task(TaskTypesEnum.ts_forecasting,
TsForecastingParams(forecast_length=2))
if mutli_ts:
data_type = DataTypesEnum.multi_ts
target = several_ts
else:
data_type = DataTypesEnum.ts
target = np.ravel(ts_1)
train_input = InputData(idx=np.arange(0, len(several_ts)),
features=several_ts, target=target,
task=task, data_type=data_type)
return train_input
def get_nan_inf_data():
supp_data = SupplementaryData(col_type_ids={'features': np.array([TYPE_TO_ID[float]] * 4)})
train_input = InputData(
idx=np.array([0, 1, 2, 3]),
features=np.array([
[1, 2, 3, 4],
[2, np.nan, 4, 5],
[3, 4, 5, np.inf],
[-np.inf, 5, 6, 7]
]),
target=np.array([1, 2, 3, 4]),
numerical_idx=np.array([0, 1, 2, 3]),
categorical_idx=np.array([]),
task=Task(TaskTypesEnum.regression),
data_type=DataTypesEnum.table,
supplementary_data=supp_data
)
return train_input
def get_single_feature_data(task=None):
supp_data = SupplementaryData(col_type_ids={'features': np.array([TYPE_TO_ID[int]]),
'target': np.array([TYPE_TO_ID[int]])})
train_input = InputData(idx=[0, 1, 2, 3, 4, 5],
features=np.array([[1], [2], [3], [7], [8], [9]]),
target=np.array([[0], [0], [0], [1], [1], [1]]),
task=task,
data_type=DataTypesEnum.table,
supplementary_data=supp_data)
return train_input
def get_mixed_data(task=None, extended=False):
""" Generate InputData with five categorical features. The categorical features
are created in such a way that in any splitting there will be categories in the
test part that were not in the train.
"""
if extended:
features = np.array([[1, '0', '1', 1, '5', 'blue', 'blue'],
[2, '1', '0', 0, '4', 'blue', 'da'],
[3, '1', '0', 1, '3', 'blue', 'ba'],
[np.nan, np.nan, '1', np.nan, '2', 'not blue', 'di'],
[8, '1', '1', 0, '1', 'not blue', 'da bu'],
[9, '0', '0', 0, '0', 'not blue', 'dai']], dtype=object)
feature_type_ids = np.array([TYPE_TO_ID[int], TYPE_TO_ID[str], TYPE_TO_ID[str], TYPE_TO_ID[int],
TYPE_TO_ID[str], TYPE_TO_ID[str], TYPE_TO_ID[str]])
target_type_ids = np.array([TYPE_TO_ID[int]])
supp_data = SupplementaryData(col_type_ids={'features': feature_type_ids,
'target': target_type_ids})
else:
features = np.array([[1, '0', 1],
[2, '1', 0],
[3, '1', 0],
[7, '1', 1],
[8, '1', 1],
[9, '0', 0]], dtype=object)
feature_type_ids = np.array([TYPE_TO_ID[int], TYPE_TO_ID[str], TYPE_TO_ID[int]])
target_type_ids = np.array([TYPE_TO_ID[int]])
supp_data = SupplementaryData(col_type_ids={'features': feature_type_ids,
'target': target_type_ids})
train_input = InputData(idx=[0, 1, 2, 3, 4, 5],
features=features,
target=np.array([[0], [0], [0], [1], [1], [1]]),
task=task,
data_type=DataTypesEnum.table,
supplementary_data=supp_data)
return train_input
def get_nan_binary_data(task=None):
""" Generate table with two numerical and one categorical features.
Both them contain nans, which need to be filled in.
Binary int columns must be processed as "almost categorical". Current dataset
For example, nan object in [1, nan, 0, 0] must be filled as 0, not as 0.33
"""
feature_type_ids = np.array([TYPE_TO_ID[int], TYPE_TO_ID[str], TYPE_TO_ID[int]])
supp_data = SupplementaryData(col_type_ids={'features': feature_type_ids})
features = np.array([[1, '0', 0],
[np.nan, np.nan, np.nan],
[0, '2', 1],
[1, '1', 1],
[5, '1', 1]], dtype=object)
input_data = InputData(
idx=np.array([0, 1, 2, 3]),
features=features,
target=np.array([[0], [0], [1], [1]]),
categorical_idx=np.array([1]),
task=task, data_type=DataTypesEnum.table,
supplementary_data=supp_data
)
return input_data
def get_unbalanced_dataset(size=10, disbalance=0.4, target_dim=None):
""" Generate table with one numerical and one categorical features by selected size and class disbalance
Target is binary and unbalanced: majority "1" class is more than minority "0" class.
It can be generated with two options: 1D or 2D representation of target.
"""
minor_size = round(size * disbalance)
major_size = size - minor_size
features = np.array([[np.random.rand(), 'minor']] * minor_size + [[np.random.rand(), 'major']] * major_size)
target = np.array([0] * minor_size + [1] * major_size)
if target_dim == 2:
target = target.reshape(-1, 1)
supp_data = SupplementaryData(col_type_ids={
'features': np.array([TYPE_TO_ID[int], TYPE_TO_ID[str]]),
'target': np.array([TYPE_TO_ID[int]])
})
input_data = InputData(idx=np.arange(features.shape[0]),
features=features,
target=target,
task=Task(TaskTypesEnum.classification),
data_type=DataTypesEnum.table,
supplementary_data=supp_data)
return input_data
def data_with_binary_int_features_and_equal_categories():
"""
Generate table with binary integer features and nans there. Such a columns
must be processed as "almost categorical". Current dataset
For example, nan object in [1, nan, 0, 0] must be filled as 0, not as 0.33
"""
supp_data = SupplementaryData(col_type_ids={'features': np.array([TYPE_TO_ID[int], TYPE_TO_ID[int]])})
task = Task(TaskTypesEnum.classification)
features = np.array([[1, 10],
[np.nan, np.nan],
[np.nan, np.nan],
[0, 0]])
target = np.array([['not-nan'], ['nan'], ['nan'], ['not-nan']])
train_input = InputData(
idx=np.array([0, 1, 2, 3]),
features=features,
target=target,
numerical_idx=np.array([0, 1]),
categorical_idx=np.array([]),
encoded_idx=np.array([]),
categorical_features=None,
features_names=None,
task=task,
data_type=DataTypesEnum.table,
supplementary_data=supp_data
)
return train_input
def test_regression_data_operations():
train_input, predict_input, y_test = get_small_regression_dataset()
operation_names = OperationTypesRepository('data_operation').suitable_operation(
task_type=TaskTypesEnum.regression)
for data_operation in operation_names:
node_data_operation = PipelineNode(data_operation)
node_final = PipelineNode('linear', nodes_from=[node_data_operation])
pipeline = Pipeline(node_final)
# Fit and predict for pipeline
pipeline.fit_from_scratch(train_input)
predicted_output = pipeline.predict(predict_input)
predicted = predicted_output.predict
assert len(predicted) == len(y_test)
def test_classification_data_operations():
train_input, predict_input, y_test = get_small_classification_dataset()
operation_names = OperationTypesRepository('data_operation').suitable_operation(
task_type=TaskTypesEnum.classification)
for data_operation in operation_names:
node_data_operation = PipelineNode(data_operation)
node_final = PipelineNode('logit', nodes_from=[node_data_operation])
pipeline = Pipeline(node_final)
# Fit and predict for pipeline
pipeline.fit_from_scratch(train_input)
predicted_output = pipeline.predict(predict_input)
predicted = predicted_output.predict
assert len(predicted) == len(y_test)
def test_ts_forecasting_lagged_data_operation():
train_input, predict_input, y_test = get_time_series()
node_lagged = PipelineNode('lagged')
node_ridge = PipelineNode('ridge', nodes_from=[node_lagged])
pipeline = Pipeline(node_ridge)
pipeline.fit_from_scratch(train_input)
predicted_output = pipeline.predict(predict_input)
predicted = np.ravel(predicted_output.predict)
assert len(predicted) == len(np.ravel(y_test))
def test_ts_forecasting_cut_data_operation():
train_input, predict_input, y_test = get_time_series()
horizon = train_input.task.task_params.forecast_length
params = OperationParameters(cut_part=0.5)
operation_cut = CutImplementation(params=params)
transformed_input = operation_cut.transform_for_fit(train_input)
assert train_input.idx.shape[0] == 2 * transformed_input.idx.shape[0] - horizon
def test_ts_forecasting_smoothing_data_operation():
train_input, predict_input, y_test = get_time_series()
model_names = OperationTypesRepository().operations_with_tag(tags=['smoothing'])
for smoothing_operation in model_names:
node_smoothing = PipelineNode(smoothing_operation)
node_lagged = PipelineNode('lagged', nodes_from=[node_smoothing])
node_ridge = PipelineNode('ridge', nodes_from=[node_lagged])
pipeline = Pipeline(node_ridge)
pipeline.fit_from_scratch(train_input)
predicted_output = pipeline.predict(predict_input)
predicted = np.ravel(predicted_output.predict)
assert len(predicted) == len(np.ravel(y_test))
def test_inf_and_nan_absence_after_imputation_implementation_fit_transform():
input_data = get_nan_inf_data()
output_data = ImputationImplementation().fit_transform(input_data)
assert np.sum(np.isinf(output_data.predict)) == 0
assert np.sum(np.isnan(output_data.predict)) == 0
def test_inf_and_nan_absence_after_imputation_implementation_fit_and_transform():
input_data = get_nan_inf_data()
imputer = ImputationImplementation()
imputer.fit(input_data)
output_data = imputer.transform(input_data)
assert np.sum(np.isinf(output_data.predict)) == 0
assert np.sum(np.isnan(output_data.predict)) == 0
def test_inf_and_nan_absence_after_pipeline_fitting_from_scratch():
train_input = get_nan_inf_data()
model_names = OperationTypesRepository().suitable_operation(task_type=TaskTypesEnum.regression)
for model_name in model_names:
node_data_operation = PipelineNode(model_name)
node_final = PipelineNode('linear', nodes_from=[node_data_operation])
pipeline = Pipeline(node_final)
# Fit and predict for pipeline
pipeline.fit_from_scratch(train_input)
predicted_output = pipeline.predict(train_input)
predicted = predicted_output.predict
assert np.sum(np.isinf(predicted)) == 0
assert np.sum(np.isnan(predicted)) == 0
def test_feature_selection_of_single_features():
for task_type in [TaskTypesEnum.classification, TaskTypesEnum.regression]:
model_names = OperationTypesRepository(operation_type='data_operation') \
.suitable_operation(tags=['feature_selection'], task_type=task_type)
task = Task(task_type)
for data_operation in model_names:
node_data_operation = PipelineNode(data_operation)
assert node_data_operation.fitted_operation is None
# Fit and predict for pipeline
train_input = get_single_feature_data(task)
node_data_operation.fit(train_input)
predicted_output = node_data_operation.predict(train_input)
predicted = predicted_output.predict
assert node_data_operation.fitted_operation is not None
assert predicted.shape == train_input.features.shape
def test_one_hot_encoding_new_category_in_test():
""" Check if One Hot Encoding can correctly predict data with new categories
(which algorithm were not process during train stage)
"""
cat_data = get_mixed_data(task=Task(TaskTypesEnum.classification),
extended=True)
train, test = train_test_data_setup(cat_data)
# Create pipeline with encoding operation
one_hot_node = PipelineNode('one_hot_encoding')
final_node = PipelineNode('dt', nodes_from=[one_hot_node])
pipeline = Pipeline(final_node)
pipeline.fit(train)
predicted = pipeline.predict(test)
assert predicted is not None
def test_knn_with_float_neighbors():
"""
Check pipeline with k-nn fit and predict correctly if n_neighbors value
is float value
"""
node_knn = PipelineNode('knnreg')
node_knn.parameters = {'n_neighbors': 2.5}
pipeline = Pipeline(node_knn)
input_data = get_single_feature_data(task=Task(TaskTypesEnum.regression))
pipeline.fit(input_data)
pipeline.predict(input_data)
def test_imputation_with_binary_correct():
"""
Check if SimpleImputer can process mixed data with both numerical and categorical
features correctly. Moreover, check if the imputer swaps the columns (it shouldn't)
"""
nan_data = get_nan_binary_data(task=Task(TaskTypesEnum.classification))
# Create node with imputation operation
imputation_node = PipelineNode('simple_imputation')
imputation_node.fit(nan_data)
predicted = imputation_node.predict(nan_data)
assert np.isclose(predicted.predict[1, 0], 1.75)
assert predicted.predict[1, 1] == '1'
assert np.isclose(predicted.predict[1, 2], 1)
def test_imputation_binary_features_with_equal_categories_correct():
"""
The correctness of the gap-filling algorithm is checked on data with binary
features. The number of known categories in each column is equal. Consequently,
there is no possibility to insert the majority class label into the gaps.
Instead of that the mean value is inserted.
"""
nan_data = data_with_binary_int_features_and_equal_categories()
imputation_node = PipelineNode('simple_imputation')
imputation_node.fit(nan_data)
predicted = imputation_node.predict(nan_data)
assert np.isclose(predicted.predict[1, 0], 0.5)
assert np.isclose(predicted.predict[1, 1], 5.0)
def test_label_encoding_correct():
"""
Check if LabelEncoder can perform transformations correctly. Also the dataset
is generated so that new categories appear in the test sample.
"""
cat_data = data_with_only_categorical_features()
train_data, test_data = train_test_data_setup(cat_data)
encoding_node = PipelineNode('label_encoding')
encoding_node.fit(train_data)
predicted_train = encoding_node.predict(train_data)
predicted_test = encoding_node.predict(test_data)
# Label 'a' was in the training sample - convert it into 0
assert predicted_train.predict[0, 0] == 0
# Label 'b' was in the training sample - convert it into 1
assert predicted_train.predict[1, 0] == 1
# Label 'c' was not in the training sample - convert it into 2
assert predicted_test.predict[0, 0] == 2
def test_lagged_with_multivariate_time_series():
"""
Checking the correct processing of multivariate time series in the lagged operation
"""
correct_fit_output = np.array([[0., 1., 10., 11.],
[1., 2., 11., 12.],
[2., 3., 12., 13.],
[3., 4., 13., 14.],
[4., 5., 14., 15.],
[5., 6., 15., 16.],
[6., 7., 16., 17.]])
correct_predict_output = np.array([[8, 9, 18, 19]])
input_data = get_multivariate_time_series()
lagged = LaggedTransformationImplementation(OperationParameters(window_size=2))
transformed_for_fit = lagged.transform_for_fit(input_data)
transformed_for_predict = lagged.transform(input_data)
# Check correctness on fit stage
lagged_features = transformed_for_fit.predict
assert lagged_features.shape == correct_fit_output.shape
assert np.all(np.isclose(lagged_features, correct_fit_output))
# Check correctness on predict stage
lagged_predict = transformed_for_predict.predict
assert lagged_predict.shape == correct_predict_output.shape
assert np.all(np.isclose(lagged_predict, correct_predict_output))
def test_lagged_with_multi_ts_type():
"""
Checking the correct processing of time series with multi_ts data type in the lagged operation
"""
correct_fit_output = np.array([[0., 1.],
[1., 2.],
[2., 3.],
[3., 4.],
[4., 5.],
[5., 6.],
[6., 7.],
[10., 11.],
[11., 12.],
[12., 13.],
[13., 14.],
[14., 15.],
[15., 16.],
[16., 17.]])
correct_predict_output = np.array([[8, 9]])
input_data = get_multivariate_time_series(mutli_ts=True)
lagged = LaggedTransformationImplementation(OperationParameters(window_size=2))
transformed_for_fit = lagged.transform_for_fit(input_data)
transformed_for_predict = lagged.transform(input_data)
# Check correctness on fit stage
lagged_features = transformed_for_fit.predict
assert lagged_features.shape == correct_fit_output.shape
assert np.all(np.isclose(lagged_features, correct_fit_output))
# Check correctness on predict stage
lagged_predict = transformed_for_predict.predict
assert lagged_predict.shape == correct_predict_output.shape
assert np.all(np.isclose(lagged_predict, correct_predict_output))
def test_poly_features_on_big_datasets():
"""
Use a table with a large number of features to run a poly features operation.
For a large number of features the operation should not greatly increase the
number of columns.
"""
test_file_path = str(os.path.dirname(__file__))
file = os.path.join('../../data', 'advanced_classification.csv')
train_input = InputData.from_csv(os.path.join(test_file_path, file),
task=Task(TaskTypesEnum.classification))
# Take only small number of rows from dataset
train_input.features = train_input.features[5: 20, :]
train_input.idx = np.arange(len(train_input.features))
train_input.target = train_input.target[5: 20].reshape((-1, 1))
poly_node = Pipeline(PipelineNode('poly_features'))
poly_node.fit(train_input)
transformed_features = poly_node.predict(train_input)
n_rows, n_cols = transformed_features.predict.shape
assert n_cols == 85
@pytest.mark.parametrize(
'balance_ratio, target_dim, expected',
[(None, 1, (12, 2)), (None, 2, (12, 2)),
(0.5, 1, (11, 2)), (0.5, 2, (11, 2)),
(1.5, 1, (12, 2)), (1.5, 2, (12, 2))]
)
def test_correctness_resample_operation_with_expand_minority(balance_ratio, target_dim, expected):
params = {'balance': 'expand_minority', 'replace': False, 'balance_ratio': balance_ratio}
resample = ResampleImplementation(OperationParameters(**params))
data = get_unbalanced_dataset(target_dim=target_dim)
assert resample.transform_for_fit(data).predict.shape == expected
@pytest.mark.parametrize(
'balance_ratio, target_dim, expected',
[(None, 1, (8, 2)), (None, 2, (8, 2)),
(0.5, 1, (9, 2)), (0.5, 2, (9, 2)),
(1.5, 1, (8, 2)), (1.5, 2, (8, 2))]
)
def test_correctness_resample_operation_with_reduce_majority(balance_ratio, target_dim, expected):
params = {'balance': 'reduce_majority', 'replace': False, 'balance_ratio': balance_ratio}
resample = ResampleImplementation(OperationParameters(**params))
data = get_unbalanced_dataset(target_dim=target_dim)
assert resample.transform_for_fit(data).predict.shape == expected
@pytest.mark.parametrize(
'strategy, balance_ratio, disbalance, expected',
[('expand_minority', 0.5, 0.4, True), ('expand_minority', 0.5, 0.2, True),
('expand_minority', 1, 0.4, True), ('expand_minority', 1, 0.2, True),
('expand_minority', 1.5, 0.4, True), ('expand_minority', 1.5, 0.2, True),
('reduce_majority', 0.5, 0.4, False), ('reduce_majority', 0.5, 0.2, False),
('reduce_majority', 1, 0.4, False), ('reduce_majority', 1, 0.2, False),
('reduce_majority', 1.5, 0.4, False), ('reduce_majority', 1.5, 0.2, False)]
)
def test_correctness_resample_operation_with_dynamic_replace_param(strategy, balance_ratio, disbalance, expected):
"""
Default params for replace is False.
In case of expanding strategy it causes difficulties and needed to change replace param to True.
It is required to avoid error in sklearn method, which cannot reuse data if replace is False.
"""
params = {'balance': strategy, 'replace': False, 'balance_ratio': balance_ratio}
resample = ResampleImplementation(OperationParameters(**params))
data = get_unbalanced_dataset(size=10, disbalance=disbalance)
resample.transform_for_fit(data)
assert resample.replace == expected