medpipe 0.2.0.dev0__tar.gz → 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of medpipe might be problematic. Click here for more details.
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/PKG-INFO +1 -1
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/pyproject.toml +1 -1
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/__init__.py +1 -1
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/data/sampler.py +85 -54
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/metrics/core.py +4 -4
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/pipeline/pipeline.py +16 -5
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe.egg-info/PKG-INFO +1 -1
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_data_sampler.py +132 -47
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/LICENSE +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/README.md +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/requirements.txt +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/setup.cfg +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/_types.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/data/__init__.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/data/db.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/data/preprocessing.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/data/preprocessor.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/data/utils.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/data/weighting.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/metrics/__init__.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/metrics/plots.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/models/__init__.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/models/calibrators.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/models/core.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/models/predictors.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/pipeline/__init__.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/py.typed +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/utils/__init__.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/utils/config.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/utils/exceptions.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/utils/io.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe/utils/logger.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe.egg-info/SOURCES.txt +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe.egg-info/dependency_links.txt +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe.egg-info/requires.txt +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/src/medpipe.egg-info/top_level.txt +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_data_db.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_data_preprocessing.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_data_preprocessor.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_data_utils.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_data_weighting.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_models_calibrators.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_models_core.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_models_predictors.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_pipeline_pipeline.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_utils_config.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_utils_exceptions.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_utils_io.py +0 -0
- {medpipe-0.2.0.dev0 → medpipe-1.0.0}/test/test_utils_logger.py +0 -0
|
@@ -44,7 +44,7 @@ if TYPE_CHECKING:
|
|
|
44
44
|
def data_sampler(
|
|
45
45
|
data: PredData,
|
|
46
46
|
labels: Labels,
|
|
47
|
-
|
|
47
|
+
reduction_factor: float = 0.25,
|
|
48
48
|
sampler_fn: str = "random_undersampler",
|
|
49
49
|
groups: npt.NDArray = np.array([]),
|
|
50
50
|
**kwargs: Any,
|
|
@@ -54,9 +54,10 @@ def data_sampler(
|
|
|
54
54
|
|
|
55
55
|
The majority class is assumed to have a False or 0 label.
|
|
56
56
|
The new set will have an imbalance equal to:
|
|
57
|
-
IR *
|
|
57
|
+
IR - IR * reduction_factor, where IR is the current imbalance ratio.
|
|
58
|
+
or a IR = 1, if reduction factor is 1.
|
|
58
59
|
|
|
59
|
-
If the
|
|
60
|
+
If the reduction factor is too small, the algorithm defaults to
|
|
60
61
|
obtain a balanced dataset.
|
|
61
62
|
|
|
62
63
|
Parameters
|
|
@@ -65,8 +66,8 @@ def data_sampler(
|
|
|
65
66
|
Data to sample of shape (n_samples, n_features).
|
|
66
67
|
labels : Labels
|
|
67
68
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
reduction_factor : float, default: 0.25
|
|
70
|
+
Reduction factor to apply to the current imbalance ratio
|
|
70
71
|
sampler_fn : str, default: "random_undersampler"
|
|
71
72
|
Sampler function to use to sample the data.
|
|
72
73
|
groups : npt.NDArray, default: np.array([])
|
|
@@ -88,25 +89,36 @@ def data_sampler(
|
|
|
88
89
|
TypeError
|
|
89
90
|
If labels is not npt.NDArray.
|
|
90
91
|
ValueError
|
|
91
|
-
If
|
|
92
|
+
If reduction_factor is less than 0.0 or greater than 1.0
|
|
92
93
|
|
|
93
94
|
"""
|
|
94
|
-
if
|
|
95
|
-
raise ValueError(
|
|
95
|
+
if reduction_factor < 0:
|
|
96
|
+
raise ValueError(
|
|
97
|
+
f"Reduction factor should be positive, but got {reduction_factor}"
|
|
98
|
+
)
|
|
99
|
+
if reduction_factor > 1.0:
|
|
100
|
+
raise ValueError(
|
|
101
|
+
f"Reduction factor should be less than 1, but got {reduction_factor}"
|
|
102
|
+
)
|
|
103
|
+
array_check(labels)
|
|
96
104
|
|
|
97
|
-
# Calculate
|
|
105
|
+
# Calculate target_ratio
|
|
98
106
|
n_samples = len(labels)
|
|
99
|
-
n_pos = np.sum(labels)
|
|
107
|
+
n_pos = np.sum(np.any(labels != 0, axis=1)) # Account for multilabel
|
|
100
108
|
|
|
101
|
-
if
|
|
102
|
-
|
|
109
|
+
if reduction_factor == 0 or n_pos == 0:
|
|
110
|
+
return data, labels, groups # Exit early
|
|
111
|
+
elif reduction_factor == 1:
|
|
112
|
+
target_ratio = 1
|
|
103
113
|
else:
|
|
104
114
|
imbalance_ratio = (n_samples - n_pos) / n_pos
|
|
105
|
-
|
|
115
|
+
target_ratio = imbalance_ratio - imbalance_ratio * reduction_factor
|
|
116
|
+
|
|
117
|
+
target_ratio = 1 if target_ratio < 1 else target_ratio
|
|
106
118
|
|
|
107
119
|
# Dispatch logic
|
|
108
120
|
if sampler_fn == "smote":
|
|
109
|
-
X_gen, y_gen = smote(data, labels,
|
|
121
|
+
X_gen, y_gen = smote(data, labels, target_ratio, kwargs.get("k_neighbors", 5))
|
|
110
122
|
if isinstance(data, DataFrame):
|
|
111
123
|
return (
|
|
112
124
|
concat((data, cast(DataFrame, X_gen))),
|
|
@@ -121,24 +133,24 @@ def data_sampler(
|
|
|
121
133
|
|
|
122
134
|
if sampler_fn == "group_smote":
|
|
123
135
|
return group_smote(
|
|
124
|
-
data, labels,
|
|
136
|
+
data, labels, target_ratio, groups, kwargs.get("k_neighbors", 5)
|
|
125
137
|
)
|
|
126
138
|
|
|
127
139
|
# Index-based samplers
|
|
128
140
|
sampler_map = {
|
|
129
|
-
"random_undersampler": lambda: random_undersampler(labels,
|
|
141
|
+
"random_undersampler": lambda: random_undersampler(labels, target_ratio),
|
|
130
142
|
"group_random_undersampler": lambda: group_random_undersampler(
|
|
131
|
-
labels,
|
|
143
|
+
labels, target_ratio, groups
|
|
132
144
|
),
|
|
133
|
-
"random_oversampler": lambda: random_oversampler(labels,
|
|
145
|
+
"random_oversampler": lambda: random_oversampler(labels, target_ratio),
|
|
134
146
|
"group_random_oversampler": lambda: group_random_oversampler(
|
|
135
|
-
labels,
|
|
147
|
+
labels, target_ratio, groups
|
|
136
148
|
),
|
|
137
149
|
"mean_dist_sampler": lambda: mean_dist_sampler(
|
|
138
|
-
data, labels,
|
|
150
|
+
data, labels, target_ratio, kwargs.get("hard_percent", 0.5)
|
|
139
151
|
),
|
|
140
152
|
"group_mean_dist_sampler": lambda: group_mean_dist_sampler(
|
|
141
|
-
data, labels,
|
|
153
|
+
data, labels, target_ratio, groups, kwargs.get("hard_percent", 0.5)
|
|
142
154
|
),
|
|
143
155
|
}
|
|
144
156
|
|
|
@@ -167,7 +179,7 @@ def random_undersampler(labels: Labels, target_ratio: float) -> npt.NDArray:
|
|
|
167
179
|
labels : Labels
|
|
168
180
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
169
181
|
target_ratio : float
|
|
170
|
-
Ratio of
|
|
182
|
+
Ratio of majority over minority classes to achieve.
|
|
171
183
|
|
|
172
184
|
Returns
|
|
173
185
|
-------
|
|
@@ -179,13 +191,15 @@ def random_undersampler(labels: Labels, target_ratio: float) -> npt.NDArray:
|
|
|
179
191
|
TypeError
|
|
180
192
|
If labels is not npt.NDArray.
|
|
181
193
|
ValueError
|
|
182
|
-
If target_ratio is less than
|
|
194
|
+
If target_ratio is less than 1.
|
|
183
195
|
|
|
184
196
|
"""
|
|
185
197
|
array_check(labels)
|
|
186
198
|
|
|
187
|
-
if target_ratio
|
|
188
|
-
raise ValueError(
|
|
199
|
+
if target_ratio < 1:
|
|
200
|
+
raise ValueError(
|
|
201
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
202
|
+
)
|
|
189
203
|
|
|
190
204
|
# Any row with a 1 is minority; rows with all 0s are majority
|
|
191
205
|
is_min = np.any(labels != 0, axis=1)
|
|
@@ -202,8 +216,7 @@ def random_undersampler(labels: Labels, target_ratio: float) -> npt.NDArray:
|
|
|
202
216
|
return maj_potential_idx.astype(int)
|
|
203
217
|
|
|
204
218
|
# Calculate target majority count
|
|
205
|
-
|
|
206
|
-
n_maj_target = int(np.round(n_min_class / target_ratio))
|
|
219
|
+
n_maj_target = int(np.round(n_min_class * target_ratio))
|
|
207
220
|
n_to_sample = min(n_maj_target, n_maj_available)
|
|
208
221
|
|
|
209
222
|
# Random selection
|
|
@@ -225,7 +238,7 @@ def group_random_undersampler(
|
|
|
225
238
|
labels : Labels
|
|
226
239
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
227
240
|
target_ratio : float
|
|
228
|
-
Ratio of
|
|
241
|
+
Ratio of majority over minority classes to achieve.
|
|
229
242
|
groups : npt.NDArray
|
|
230
243
|
List of groups in which labels belong of shape (n_samples,).
|
|
231
244
|
|
|
@@ -240,14 +253,16 @@ def group_random_undersampler(
|
|
|
240
253
|
If labels is not npt.NDArray.
|
|
241
254
|
ValueError
|
|
242
255
|
If labels and group do not have the same dimension.
|
|
243
|
-
If target_ratio is less than
|
|
256
|
+
If target_ratio is less than 1.
|
|
244
257
|
|
|
245
258
|
"""
|
|
246
259
|
array_check(labels)
|
|
247
260
|
array_dim_check(labels, groups, dim=0)
|
|
248
261
|
|
|
249
|
-
if target_ratio
|
|
250
|
-
raise ValueError(
|
|
262
|
+
if target_ratio < 1:
|
|
263
|
+
raise ValueError(
|
|
264
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
265
|
+
)
|
|
251
266
|
|
|
252
267
|
all_sampled_indices = [] # List for sampled indices
|
|
253
268
|
|
|
@@ -279,7 +294,7 @@ def random_oversampler(labels: Labels, target_ratio: float) -> npt.NDArray:
|
|
|
279
294
|
labels : Labels
|
|
280
295
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
281
296
|
target_ratio : float
|
|
282
|
-
Ratio of
|
|
297
|
+
Ratio of majority over minority classes to achieve.
|
|
283
298
|
|
|
284
299
|
Returns
|
|
285
300
|
-------
|
|
@@ -291,14 +306,16 @@ def random_oversampler(labels: Labels, target_ratio: float) -> npt.NDArray:
|
|
|
291
306
|
TypeError
|
|
292
307
|
If labels is not npt.NDArray.
|
|
293
308
|
ValueError
|
|
294
|
-
If target_ratio is less than
|
|
309
|
+
If target_ratio is less than 1.
|
|
295
310
|
If there is no minority class.
|
|
296
311
|
|
|
297
312
|
"""
|
|
298
313
|
array_check(labels)
|
|
299
314
|
|
|
300
|
-
if target_ratio
|
|
301
|
-
raise ValueError(
|
|
315
|
+
if target_ratio < 1:
|
|
316
|
+
raise ValueError(
|
|
317
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
318
|
+
)
|
|
302
319
|
|
|
303
320
|
# Generate class masks and indices once
|
|
304
321
|
is_min = np.any(labels != 0, axis=1)
|
|
@@ -314,11 +331,9 @@ def random_oversampler(labels: Labels, target_ratio: float) -> npt.NDArray:
|
|
|
314
331
|
raise ValueError("No minority examples found; cannot oversample.")
|
|
315
332
|
|
|
316
333
|
# Calculate target size
|
|
317
|
-
|
|
318
|
-
target_n_min = int(np.round(n_maj_class * target_ratio))
|
|
334
|
+
target_n_min = int(np.round(n_maj_class / target_ratio))
|
|
319
335
|
|
|
320
336
|
# Perform oversampling
|
|
321
|
-
# This selects 'target_n_min' indices from the minority set with replacement.
|
|
322
337
|
oversampled_min_idx = np.random.choice(min_indices, size=target_n_min, replace=True)
|
|
323
338
|
|
|
324
339
|
# Concatenate and ensure integer type
|
|
@@ -337,7 +352,7 @@ def group_random_oversampler(
|
|
|
337
352
|
labels : Labels
|
|
338
353
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
339
354
|
target_ratio : float
|
|
340
|
-
Ratio of
|
|
355
|
+
Ratio of majority over minority classes to achieve.
|
|
341
356
|
groups : npt.NDArray
|
|
342
357
|
List of groups in which labels belong of shape (n_samples,).
|
|
343
358
|
|
|
@@ -352,14 +367,16 @@ def group_random_oversampler(
|
|
|
352
367
|
If labels is not npt.NDArray.
|
|
353
368
|
ValueError
|
|
354
369
|
If labels and group do not have the same dimension.
|
|
355
|
-
If target_ratio is less than
|
|
370
|
+
If target_ratio is less than 1.
|
|
356
371
|
|
|
357
372
|
"""
|
|
358
373
|
array_check(labels)
|
|
359
374
|
array_dim_check(labels, groups, dim=0)
|
|
360
375
|
|
|
361
|
-
if target_ratio
|
|
362
|
-
raise ValueError(
|
|
376
|
+
if target_ratio < 1:
|
|
377
|
+
raise ValueError(
|
|
378
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
379
|
+
)
|
|
363
380
|
|
|
364
381
|
all_sampled_indices = [] # List to contain sampled indices
|
|
365
382
|
unique_groups = np.unique(groups)
|
|
@@ -405,7 +422,7 @@ def mean_dist_sampler(
|
|
|
405
422
|
labels : Labels
|
|
406
423
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
407
424
|
target_ratio : float
|
|
408
|
-
Ratio of
|
|
425
|
+
Ratio of majority over minority classes to achieve.
|
|
409
426
|
hard_percent : float, default: 0.5
|
|
410
427
|
Percentage of examples that are considered hard, between 0 and 1.
|
|
411
428
|
If hard_percent is 0.5, half of the examples are chosen from
|
|
@@ -422,15 +439,17 @@ def mean_dist_sampler(
|
|
|
422
439
|
If labels is not npt.NDArray.
|
|
423
440
|
ValueError
|
|
424
441
|
If hard_percent is not between 0 and 1.
|
|
425
|
-
If target_ratio is less than
|
|
442
|
+
If target_ratio is less than 1.
|
|
426
443
|
|
|
427
444
|
"""
|
|
428
445
|
array_check(labels)
|
|
429
446
|
|
|
430
447
|
if not (0 <= hard_percent <= 1):
|
|
431
448
|
raise ValueError(f"hard_percent must be between 0 and 1, got {hard_percent}")
|
|
432
|
-
if target_ratio
|
|
433
|
-
raise ValueError(
|
|
449
|
+
if target_ratio < 1:
|
|
450
|
+
raise ValueError(
|
|
451
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
452
|
+
)
|
|
434
453
|
|
|
435
454
|
# Identify minority and majority masks
|
|
436
455
|
is_min = np.any(labels != 0, axis=1)
|
|
@@ -457,7 +476,7 @@ def mean_dist_sampler(
|
|
|
457
476
|
sorted_rel_idx = np.argsort(dist)
|
|
458
477
|
|
|
459
478
|
# Determine target counts
|
|
460
|
-
n_maj_target = int(np.round(n_min
|
|
479
|
+
n_maj_target = int(np.round(n_min * target_ratio))
|
|
461
480
|
n_to_sample = min(n_maj_target, n_maj_available)
|
|
462
481
|
|
|
463
482
|
n_hard = int(np.round(n_to_sample * hard_percent))
|
|
@@ -497,7 +516,7 @@ def group_mean_dist_sampler(
|
|
|
497
516
|
labels : Labels
|
|
498
517
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
499
518
|
target_ratio : float
|
|
500
|
-
Ratio of
|
|
519
|
+
Ratio of majority over minority classes to achieve.
|
|
501
520
|
groups : npt.NDArray
|
|
502
521
|
List of groups in which labels belong of shape (n_samples,).
|
|
503
522
|
hard_percent : float, default: 0.5
|
|
@@ -516,11 +535,16 @@ def group_mean_dist_sampler(
|
|
|
516
535
|
If labels is not npt.NDArray.
|
|
517
536
|
ValueError
|
|
518
537
|
If labels and group do not have the same dimension.
|
|
538
|
+
If target ratio is less than 1.
|
|
519
539
|
|
|
520
540
|
"""
|
|
521
541
|
array_check(labels)
|
|
522
542
|
array_dim_check(labels, groups, dim=0)
|
|
523
543
|
|
|
544
|
+
if target_ratio < 1:
|
|
545
|
+
raise ValueError(
|
|
546
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
547
|
+
)
|
|
524
548
|
all_sampled_indices = [] # List for sampled indices
|
|
525
549
|
n_groups = np.unique(groups)
|
|
526
550
|
|
|
@@ -568,7 +592,7 @@ def smote(
|
|
|
568
592
|
labels : Labels
|
|
569
593
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
570
594
|
target_ratio : float
|
|
571
|
-
Ratio of
|
|
595
|
+
Ratio of majority over minority classes to achieve.
|
|
572
596
|
k_neighbors : int
|
|
573
597
|
Number of neighbors to use for SMOTE knn.
|
|
574
598
|
|
|
@@ -584,13 +608,15 @@ def smote(
|
|
|
584
608
|
TypeError
|
|
585
609
|
If labels is not npt.NDArray.
|
|
586
610
|
ValueError
|
|
587
|
-
|
|
611
|
+
It target_ratio is less than 1.
|
|
588
612
|
|
|
589
613
|
"""
|
|
590
614
|
array_check(labels)
|
|
591
615
|
|
|
592
|
-
if target_ratio
|
|
593
|
-
raise ValueError(
|
|
616
|
+
if target_ratio < 1:
|
|
617
|
+
raise ValueError(
|
|
618
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
619
|
+
)
|
|
594
620
|
|
|
595
621
|
# Identify counts
|
|
596
622
|
is_min = np.any(labels != 0, axis=1)
|
|
@@ -598,7 +624,7 @@ def smote(
|
|
|
598
624
|
n_min_existing = np.sum(is_min)
|
|
599
625
|
|
|
600
626
|
# Calculate how many NEW samples we need to generate
|
|
601
|
-
n_target_total_min = int(np.round(n_maj
|
|
627
|
+
n_target_total_min = int(np.round(n_maj / target_ratio))
|
|
602
628
|
n_to_generate = n_target_total_min - n_min_existing
|
|
603
629
|
|
|
604
630
|
if n_to_generate <= 0:
|
|
@@ -652,7 +678,7 @@ def group_smote(
|
|
|
652
678
|
labels : Labels
|
|
653
679
|
Binary prediction labels of shape (n_samples, n_classes).
|
|
654
680
|
target_ratio : float
|
|
655
|
-
Ratio of
|
|
681
|
+
Ratio of majority over minority classes to achieve.
|
|
656
682
|
groups : npt.NDArray
|
|
657
683
|
List of groups in which labels belong of shape (n_samples,).
|
|
658
684
|
k_neighbors : int
|
|
@@ -673,11 +699,16 @@ def group_smote(
|
|
|
673
699
|
If labels is not npt.NDArray
|
|
674
700
|
ValueError
|
|
675
701
|
If labels and group do not have the same dimension.
|
|
702
|
+
It target_ratio is less than 1.
|
|
676
703
|
|
|
677
704
|
"""
|
|
678
705
|
array_check(labels)
|
|
679
706
|
array_dim_check(labels, groups, dim=0)
|
|
680
707
|
|
|
708
|
+
if target_ratio < 1:
|
|
709
|
+
raise ValueError(
|
|
710
|
+
f"Target ratio should be greater than 1, but got {target_ratio}"
|
|
711
|
+
)
|
|
681
712
|
unique_groups = np.unique(groups)
|
|
682
713
|
|
|
683
714
|
# Start with original data to avoid repeated copying
|
|
@@ -129,8 +129,6 @@ def compute_all_CI(model_metrics: ModelMetrics, metric_list: list[str] = []) ->
|
|
|
129
129
|
Model metrics for different folds.
|
|
130
130
|
metric_list : list[str], default: []
|
|
131
131
|
List of metrics to calculate confidence interval.
|
|
132
|
-
**kwargs
|
|
133
|
-
Extra arguments for the compute_CI function.
|
|
134
132
|
|
|
135
133
|
Returns
|
|
136
134
|
-------
|
|
@@ -282,10 +280,12 @@ def compute_pred_metrics(
|
|
|
282
280
|
# Returns array of scores for each label
|
|
283
281
|
scores = func(y_true, y_pred, average=None, zero_division=0.0)
|
|
284
282
|
# Append the weighted average as the last element
|
|
285
|
-
weighted = func(y_true, y_pred, average="weighted")
|
|
283
|
+
weighted = func(y_true, y_pred, average="weighted", zero_division=0.0)
|
|
286
284
|
metric_dict[metric] = np.append(scores, weighted)
|
|
287
285
|
else:
|
|
288
|
-
metric_dict[metric] = [
|
|
286
|
+
metric_dict[metric] = [
|
|
287
|
+
func(y_true, y_pred, average="binary", zero_division=0.0)
|
|
288
|
+
]
|
|
289
289
|
|
|
290
290
|
return metric_dict
|
|
291
291
|
|
|
@@ -157,7 +157,7 @@ class Pipeline:
|
|
|
157
157
|
self.n_labels = len(self.label_list)
|
|
158
158
|
|
|
159
159
|
print_message(
|
|
160
|
-
f"Setting up Pipeline
|
|
160
|
+
f"Setting up Pipeline {self.version} ({self.n_labels} labels)",
|
|
161
161
|
self.logger,
|
|
162
162
|
SCRIPT_NAME,
|
|
163
163
|
)
|
|
@@ -480,7 +480,9 @@ class Pipeline:
|
|
|
480
480
|
weights = self._weight_data(y_tr_label)
|
|
481
481
|
|
|
482
482
|
print_message(
|
|
483
|
-
f"Label: {label} | Fold {i+1}/{n_folds}",
|
|
483
|
+
f"Label: {label} | {fold_key} | Fold {i+1}/{n_folds}",
|
|
484
|
+
self.logger,
|
|
485
|
+
SCRIPT_NAME,
|
|
484
486
|
)
|
|
485
487
|
|
|
486
488
|
# Execute training and evaluation
|
|
@@ -511,6 +513,12 @@ class Pipeline:
|
|
|
511
513
|
X_final, y_work[:, k : k + 1], cv_groups
|
|
512
514
|
)
|
|
513
515
|
weights = self._weight_data(y_tr_final)
|
|
516
|
+
print_message(
|
|
517
|
+
f"Train set size: {len(X_tr_final):,}", self.logger, SCRIPT_NAME
|
|
518
|
+
)
|
|
519
|
+
print_message(
|
|
520
|
+
f"Calibration set size: {len(X_cal_set):,}", self.logger, SCRIPT_NAME
|
|
521
|
+
)
|
|
514
522
|
self.fit_model(X_tr_final, y_tr_final, "predictor", label, weights)
|
|
515
523
|
|
|
516
524
|
if self.calibrator_type:
|
|
@@ -550,14 +558,17 @@ class Pipeline:
|
|
|
550
558
|
Label associated with the model to train.
|
|
551
559
|
fold_key : Any
|
|
552
560
|
Key representing the current fold.
|
|
553
|
-
X_cal : PosProba, default: np.
|
|
561
|
+
X_cal : PosProba, default: np.array([])
|
|
554
562
|
Calibration data of shape (n_samples,) for the calibrator.
|
|
555
|
-
y_cal : Labels, default: np.
|
|
563
|
+
y_cal : Labels, default: np.array([])
|
|
556
564
|
Calibration labels of shape (n_samples,) for the calibrator.
|
|
557
|
-
weights : npt.NDArray, default: np.
|
|
565
|
+
weights : npt.NDArray, default: np.array([])
|
|
558
566
|
Weights to address class imbalance.
|
|
559
567
|
|
|
560
568
|
"""
|
|
569
|
+
print_message(f"Train set size: {len(X_train):,}", self.logger, SCRIPT_NAME)
|
|
570
|
+
print_message(f"Calibration set size: {len(X_cal):,}", self.logger, SCRIPT_NAME)
|
|
571
|
+
print_message(f"Test set size: {len(X_test):,}", self.logger, SCRIPT_NAME)
|
|
561
572
|
# Fit predictor on train set
|
|
562
573
|
self.fit_model(X_train, y_train, "predictor", label, weights)
|
|
563
574
|
|
|
@@ -12,6 +12,7 @@ import pandas as pd
|
|
|
12
12
|
import pytest
|
|
13
13
|
|
|
14
14
|
from medpipe.data.sampler import (
|
|
15
|
+
data_sampler,
|
|
15
16
|
group_mean_dist_sampler,
|
|
16
17
|
group_random_oversampler,
|
|
17
18
|
group_random_undersampler,
|
|
@@ -111,13 +112,101 @@ labels_groups_no_maj = np.array(
|
|
|
111
112
|
)
|
|
112
113
|
|
|
113
114
|
|
|
115
|
+
@pytest.mark.parametrize(
|
|
116
|
+
"labels, data_len",
|
|
117
|
+
[
|
|
118
|
+
(single_labels, 12),
|
|
119
|
+
(bi_labels, 13),
|
|
120
|
+
(tri_labels, 13),
|
|
121
|
+
],
|
|
122
|
+
)
|
|
123
|
+
def test_data_sampler_success(labels, data_len):
|
|
124
|
+
data = pd.DataFrame(np.random.rand(20, 2))
|
|
125
|
+
X, y, grps = data_sampler(data, labels, 0.5)
|
|
126
|
+
|
|
127
|
+
assert len(X) == data_len
|
|
128
|
+
assert len(y) == data_len
|
|
129
|
+
assert len(grps) == 0
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@pytest.mark.parametrize(
|
|
133
|
+
"labels, reduction_factor, data_len",
|
|
134
|
+
[
|
|
135
|
+
(single_labels, 1.0, 8),
|
|
136
|
+
(single_labels, 0.25, 16),
|
|
137
|
+
(bi_labels, 1.0, 12),
|
|
138
|
+
(bi_labels, 0.25, 16),
|
|
139
|
+
(tri_labels, 1.0, 10),
|
|
140
|
+
(tri_labels, 0.25, 16),
|
|
141
|
+
],
|
|
142
|
+
)
|
|
143
|
+
def test_data_sampler_reduction_factor(labels, reduction_factor, data_len):
|
|
144
|
+
data = pd.DataFrame(np.random.rand(20, 2))
|
|
145
|
+
X, y, grps = data_sampler(data, labels, reduction_factor)
|
|
146
|
+
|
|
147
|
+
assert len(X) == data_len
|
|
148
|
+
assert len(y) == data_len
|
|
149
|
+
assert len(grps) == 0
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@pytest.mark.parametrize(
|
|
153
|
+
"sampler_fn, data_len",
|
|
154
|
+
[
|
|
155
|
+
("random_undersampler", [8, 12, 10]),
|
|
156
|
+
("random_oversampler", [32, 28, 30]),
|
|
157
|
+
("mean_dist_sampler", [8, 12, 10]),
|
|
158
|
+
("smote", [32, 28, 20]),
|
|
159
|
+
],
|
|
160
|
+
)
|
|
161
|
+
def test_data_sampler_sampler_fn(sampler_fn, data_len):
|
|
162
|
+
data = pd.DataFrame(np.random.rand(20, 2))
|
|
163
|
+
labels = (single_labels, bi_labels, tri_labels)
|
|
164
|
+
for i in range(3):
|
|
165
|
+
if sampler_fn == "smote":
|
|
166
|
+
X, y, grps = data_sampler(
|
|
167
|
+
data,
|
|
168
|
+
labels[i],
|
|
169
|
+
reduction_factor=1.0,
|
|
170
|
+
sampler_fn=sampler_fn,
|
|
171
|
+
k_neighbors=1,
|
|
172
|
+
)
|
|
173
|
+
else:
|
|
174
|
+
X, y, grps = data_sampler(
|
|
175
|
+
data, labels[i], reduction_factor=1.0, sampler_fn=sampler_fn
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
assert len(X) == data_len[i]
|
|
179
|
+
assert len(y) == data_len[i]
|
|
180
|
+
assert len(grps) == 0
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
@pytest.mark.parametrize(
|
|
184
|
+
"reduction_factor",
|
|
185
|
+
[1.5, -1.0],
|
|
186
|
+
)
|
|
187
|
+
def test_data_sampler_value_error(reduction_factor):
|
|
188
|
+
data = pd.DataFrame(np.random.rand(20, 2))
|
|
189
|
+
with pytest.raises(ValueError):
|
|
190
|
+
data_sampler(data, single_labels, reduction_factor)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
@pytest.mark.parametrize(
|
|
194
|
+
"labels",
|
|
195
|
+
["str", (1, 2, 3), {"a": 1}],
|
|
196
|
+
)
|
|
197
|
+
def test_data_sampler_type_error(labels):
|
|
198
|
+
data = pd.DataFrame(np.random.rand(20, 2))
|
|
199
|
+
with pytest.raises(TypeError):
|
|
200
|
+
data_sampler(data, labels)
|
|
201
|
+
|
|
202
|
+
|
|
114
203
|
# Basic functionality tests
|
|
115
204
|
@pytest.mark.parametrize(
|
|
116
205
|
"target_ratio, labels, n_min_class",
|
|
117
206
|
[
|
|
118
|
-
(
|
|
119
|
-
(
|
|
120
|
-
(
|
|
207
|
+
(2, single_labels, 4),
|
|
208
|
+
(1, bi_labels, 6),
|
|
209
|
+
(3, tri_labels, 5),
|
|
121
210
|
],
|
|
122
211
|
)
|
|
123
212
|
def test_random_undersampler_success(target_ratio, labels, n_min_class):
|
|
@@ -131,22 +220,22 @@ def test_random_undersampler_success(target_ratio, labels, n_min_class):
|
|
|
131
220
|
|
|
132
221
|
assert minority_count == n_min_class
|
|
133
222
|
assert (
|
|
134
|
-
len(sampled_labels) == n_min_class +
|
|
223
|
+
len(sampled_labels) == n_min_class + target_ratio * n_min_class
|
|
135
224
|
) # Total samples length based on target_ratio and class imbalance
|
|
136
225
|
|
|
137
226
|
|
|
138
227
|
@pytest.mark.parametrize(
|
|
139
228
|
"target_ratio, labels, n_maj_class",
|
|
140
229
|
[
|
|
141
|
-
(0
|
|
230
|
+
(2.0, single_labels, 8),
|
|
142
231
|
(1.0, single_labels, 4),
|
|
143
|
-
(
|
|
144
|
-
(0
|
|
232
|
+
(4.0, single_labels, 16),
|
|
233
|
+
(2.0, bi_labels, 12),
|
|
145
234
|
(1.0, bi_labels, 6),
|
|
146
|
-
(
|
|
147
|
-
(0
|
|
235
|
+
(1.5, bi_labels, 9),
|
|
236
|
+
(2.0, tri_labels, 10),
|
|
148
237
|
(1.0, tri_labels, 5),
|
|
149
|
-
(
|
|
238
|
+
(3.0, tri_labels, 15),
|
|
150
239
|
],
|
|
151
240
|
)
|
|
152
241
|
def test_random_undersampler_target_ratio(target_ratio, labels, n_maj_class):
|
|
@@ -163,9 +252,7 @@ def test_random_undersampler_target_ratio(target_ratio, labels, n_maj_class):
|
|
|
163
252
|
# Check if the ratio is close to target ratio
|
|
164
253
|
if minority_count > 0:
|
|
165
254
|
ratio = majority_count / minority_count
|
|
166
|
-
assert np.isclose(
|
|
167
|
-
ratio, 1 / target_ratio, atol=0.1
|
|
168
|
-
) # Allow for slight imprecision
|
|
255
|
+
assert np.isclose(ratio, target_ratio, atol=0.1) # Allow for slight imprecision
|
|
169
256
|
|
|
170
257
|
# Assert that the target ratio is close to what we selected
|
|
171
258
|
assert majority_count == n_maj_class # Should match expected number of majority
|
|
@@ -173,7 +260,7 @@ def test_random_undersampler_target_ratio(target_ratio, labels, n_maj_class):
|
|
|
173
260
|
|
|
174
261
|
# Edge case: No minority class samples
|
|
175
262
|
def test_random_undersampler_no_minority():
|
|
176
|
-
sample_idx = random_undersampler(labels_no_minority, target_ratio=
|
|
263
|
+
sample_idx = random_undersampler(labels_no_minority, target_ratio=1)
|
|
177
264
|
sampled_labels = labels_no_minority[sample_idx]
|
|
178
265
|
|
|
179
266
|
# If no minority class, only majority class samples should be selected
|
|
@@ -186,7 +273,7 @@ def test_random_undersampler_no_minority():
|
|
|
186
273
|
[
|
|
187
274
|
(0.5, labels_empty),
|
|
188
275
|
(0.0, single_labels),
|
|
189
|
-
(0.
|
|
276
|
+
(0.9, bi_labels),
|
|
190
277
|
(-0.1, tri_labels),
|
|
191
278
|
],
|
|
192
279
|
)
|
|
@@ -198,11 +285,11 @@ def test_random_undersampler_value_error(target_ratio, labels):
|
|
|
198
285
|
# Raises a TypeError
|
|
199
286
|
@pytest.mark.parametrize(
|
|
200
287
|
"labels",
|
|
201
|
-
[0
|
|
288
|
+
[1.0, "string"],
|
|
202
289
|
)
|
|
203
290
|
def test_random_undersampler_type_error(labels):
|
|
204
291
|
with pytest.raises(TypeError):
|
|
205
|
-
random_undersampler(labels, target_ratio=
|
|
292
|
+
random_undersampler(labels, target_ratio=1)
|
|
206
293
|
|
|
207
294
|
|
|
208
295
|
# Basic functionality test
|
|
@@ -244,9 +331,9 @@ def test_group_random_undersampler_success(
|
|
|
244
331
|
(1.0, single_labels, single_groups, [1, 3]),
|
|
245
332
|
(1.0, bi_labels, bi_groups, [1, 3, 2]),
|
|
246
333
|
(1.0, tri_labels, tri_groups, [3, 2]),
|
|
247
|
-
(
|
|
248
|
-
(
|
|
249
|
-
(
|
|
334
|
+
(2, single_labels, single_groups, [2, 6]),
|
|
335
|
+
(2, bi_labels, bi_groups, [2, 6, 4]),
|
|
336
|
+
(2, tri_labels, tri_groups, [6, 4]),
|
|
250
337
|
],
|
|
251
338
|
)
|
|
252
339
|
def test_group_random_undersampler_target_ratio(
|
|
@@ -270,7 +357,7 @@ def test_group_random_undersampler_target_ratio(
|
|
|
270
357
|
if minority_count > 0:
|
|
271
358
|
ratio = majority_count / minority_count
|
|
272
359
|
assert np.isclose(
|
|
273
|
-
ratio,
|
|
360
|
+
ratio, target_ratio, atol=0.1
|
|
274
361
|
) # Allow for slight imprecision
|
|
275
362
|
|
|
276
363
|
assert majority_count == n_maj_class # Should match expected number of minority
|
|
@@ -283,20 +370,20 @@ def test_group_random_undersampler_target_ratio(
|
|
|
283
370
|
)
|
|
284
371
|
def test_group_random_undersampler_type_error(labels):
|
|
285
372
|
with pytest.raises(TypeError):
|
|
286
|
-
group_random_undersampler(labels, target_ratio=0
|
|
373
|
+
group_random_undersampler(labels, target_ratio=1.0, groups=single_groups)
|
|
287
374
|
|
|
288
375
|
|
|
289
376
|
# Labels and groups should have the same number of samples
|
|
290
377
|
def test_group_random_undersampler_mismatched_dimensions():
|
|
291
378
|
with pytest.raises(ValueError):
|
|
292
379
|
groups = np.array([0, 1])
|
|
293
|
-
group_random_undersampler(single_labels, target_ratio=0
|
|
380
|
+
group_random_undersampler(single_labels, target_ratio=1.0, groups=groups)
|
|
294
381
|
|
|
295
382
|
|
|
296
383
|
# Edge case: No minority class samples in any group
|
|
297
384
|
def test_group_random_undersampler_no_minority():
|
|
298
385
|
sample_idx = group_random_undersampler(
|
|
299
|
-
labels_no_minority, target_ratio=0
|
|
386
|
+
labels_no_minority, target_ratio=1.0, groups=labels_groups_no_maj
|
|
300
387
|
)
|
|
301
388
|
sampled_labels = labels_no_minority[sample_idx]
|
|
302
389
|
|
|
@@ -307,9 +394,9 @@ def test_group_random_undersampler_no_minority():
|
|
|
307
394
|
@pytest.mark.parametrize(
|
|
308
395
|
"target_ratio, labels, n_maj_class",
|
|
309
396
|
[
|
|
310
|
-
(0
|
|
311
|
-
(0
|
|
312
|
-
(0
|
|
397
|
+
(1.0, single_labels, 16),
|
|
398
|
+
(1.0, bi_labels, 14),
|
|
399
|
+
(1.0, tri_labels, 15),
|
|
313
400
|
],
|
|
314
401
|
)
|
|
315
402
|
def test_random_oversampler_success(target_ratio, labels, n_maj_class):
|
|
@@ -320,7 +407,6 @@ def test_random_oversampler_success(target_ratio, labels, n_maj_class):
|
|
|
320
407
|
is_maj = np.all(sampled_labels == 0, axis=1)
|
|
321
408
|
majority_count = np.sum(is_maj)
|
|
322
409
|
|
|
323
|
-
# In the optimized version, we keep all original majority samples
|
|
324
410
|
assert majority_count == n_maj_class
|
|
325
411
|
# Total length should be n_maj + (n_maj * target_ratio)
|
|
326
412
|
expected_len = n_maj_class + int(np.round(n_maj_class * target_ratio))
|
|
@@ -330,8 +416,8 @@ def test_random_oversampler_success(target_ratio, labels, n_maj_class):
|
|
|
330
416
|
@pytest.mark.parametrize(
|
|
331
417
|
"target_ratio, labels, expected_min_count",
|
|
332
418
|
[
|
|
333
|
-
(0
|
|
334
|
-
(1.0, tri_labels, 15), # 15 maj
|
|
419
|
+
(2.0, tri_labels, 8), # 15 maj / 2.0 = 7.5 -> 8
|
|
420
|
+
(1.0, tri_labels, 15), # 15 maj / 1.0 = 15
|
|
335
421
|
],
|
|
336
422
|
)
|
|
337
423
|
def test_random_oversampler_target_ratio(target_ratio, labels, expected_min_count):
|
|
@@ -344,9 +430,9 @@ def test_random_oversampler_target_ratio(target_ratio, labels, expected_min_coun
|
|
|
344
430
|
minority_count = np.sum(is_min)
|
|
345
431
|
majority_count = np.sum(is_maj)
|
|
346
432
|
|
|
347
|
-
# Check ratio:
|
|
348
|
-
calculated_ratio =
|
|
349
|
-
assert np.isclose(calculated_ratio, target_ratio, atol=0.
|
|
433
|
+
# Check ratio: maj / min should be target_ratio
|
|
434
|
+
calculated_ratio = majority_count / minority_count
|
|
435
|
+
assert np.isclose(calculated_ratio, target_ratio, atol=0.4)
|
|
350
436
|
assert minority_count == expected_min_count
|
|
351
437
|
|
|
352
438
|
|
|
@@ -357,7 +443,7 @@ def test_random_oversampler_target_ratio(target_ratio, labels, expected_min_coun
|
|
|
357
443
|
)
|
|
358
444
|
def test_random_oversampler_type_error(labels):
|
|
359
445
|
with pytest.raises(TypeError):
|
|
360
|
-
random_oversampler(labels, target_ratio=0
|
|
446
|
+
random_oversampler(labels, target_ratio=1.0)
|
|
361
447
|
|
|
362
448
|
|
|
363
449
|
@pytest.mark.parametrize(
|
|
@@ -391,7 +477,7 @@ def test_group_random_oversampler_success(
|
|
|
391
477
|
|
|
392
478
|
def test_group_random_oversampler_no_minority():
|
|
393
479
|
# if no minority, just return majority.
|
|
394
|
-
sample_idx = group_random_oversampler(labels_no_minority,
|
|
480
|
+
sample_idx = group_random_oversampler(labels_no_minority, 1.5, labels_groups_no_maj)
|
|
395
481
|
assert len(sample_idx) == len(labels_no_minority)
|
|
396
482
|
|
|
397
483
|
|
|
@@ -413,14 +499,14 @@ def test_random_oversampler_value_error(target_ratio, labels):
|
|
|
413
499
|
)
|
|
414
500
|
def test_group_random_oversampler_type_error(labels):
|
|
415
501
|
with pytest.raises(TypeError):
|
|
416
|
-
group_random_oversampler(labels, target_ratio=0
|
|
502
|
+
group_random_oversampler(labels, target_ratio=1.0, groups=single_groups)
|
|
417
503
|
|
|
418
504
|
|
|
419
505
|
# Labels and groups should have the same number of samples
|
|
420
506
|
def test_group_random_oversampler_mismatched_dimensions():
|
|
421
507
|
with pytest.raises(ValueError):
|
|
422
508
|
groups = np.array([0, 1])
|
|
423
|
-
group_random_oversampler(single_labels, target_ratio=0
|
|
509
|
+
group_random_oversampler(single_labels, target_ratio=1.0, groups=groups)
|
|
424
510
|
|
|
425
511
|
|
|
426
512
|
def test_mean_dist_sampler_basic():
|
|
@@ -430,10 +516,10 @@ def test_mean_dist_sampler_basic():
|
|
|
430
516
|
labels = np.zeros((10, 2))
|
|
431
517
|
labels[8:] = 1
|
|
432
518
|
|
|
433
|
-
# If target_ratio is
|
|
434
|
-
# we need 2
|
|
435
|
-
res_idx = mean_dist_sampler(data, labels, target_ratio=0
|
|
436
|
-
assert len(res_idx) ==
|
|
519
|
+
# If target_ratio is 3, and we have 2 minority samples,
|
|
520
|
+
# we need 2*3 = 6 majority samples.
|
|
521
|
+
res_idx = mean_dist_sampler(data, labels, target_ratio=3.0, hard_percent=0.5)
|
|
522
|
+
assert len(res_idx) == 8 # 2 minority + 4 majority
|
|
437
523
|
assert isinstance(res_idx, np.ndarray)
|
|
438
524
|
|
|
439
525
|
|
|
@@ -442,9 +528,9 @@ def test_mean_dist_sampler_exceptions():
|
|
|
442
528
|
labels = np.zeros((5, 1))
|
|
443
529
|
|
|
444
530
|
with pytest.raises(ValueError):
|
|
445
|
-
mean_dist_sampler(data, labels, target_ratio=0
|
|
531
|
+
mean_dist_sampler(data, labels, target_ratio=1.0, hard_percent=1.5)
|
|
446
532
|
|
|
447
|
-
with pytest.raises(ValueError
|
|
533
|
+
with pytest.raises(ValueError):
|
|
448
534
|
mean_dist_sampler(data, labels, target_ratio=-0.1)
|
|
449
535
|
|
|
450
536
|
|
|
@@ -494,9 +580,8 @@ def test_smote_invalid_ratio():
|
|
|
494
580
|
with pytest.raises(ValueError):
|
|
495
581
|
smote(data, labels, target_ratio=0, k_neighbors=1)
|
|
496
582
|
|
|
497
|
-
# --- Mock Data Setup ---
|
|
498
|
-
|
|
499
583
|
|
|
584
|
+
# --- Mock Data Setup ---
|
|
500
585
|
def create_smote_data():
|
|
501
586
|
# Group 0: 20 majority, 5 minority (Needs oversampling)
|
|
502
587
|
# Group 1: 20 majority, 20 minority (Already balanced)
|
|
@@ -523,7 +608,7 @@ def test_group_smote_logic():
|
|
|
523
608
|
target_ratio = 1.0 # Want 1:1 ratio
|
|
524
609
|
k_neighbors = 2 # Small k to ensure it works on smaller groups
|
|
525
610
|
|
|
526
|
-
|
|
611
|
+
_, y_res, g_res = group_smote(data, labels, target_ratio, groups, k_neighbors)
|
|
527
612
|
|
|
528
613
|
# Check Group 0 (Was 20 maj, 5 min -> Should now be 20 maj, 20 min)
|
|
529
614
|
g0_mask = g_res == 0
|
|
@@ -545,7 +630,7 @@ def test_group_smote_pandas_compatibility():
|
|
|
545
630
|
data, labels, groups = create_smote_data()
|
|
546
631
|
df_data = pd.DataFrame(data, columns=[f"feat_{i}" for i in range(10)])
|
|
547
632
|
|
|
548
|
-
X_res, y_res,
|
|
633
|
+
X_res, y_res, _ = group_smote(df_data, labels, 1.0, groups, k_neighbors=3)
|
|
549
634
|
|
|
550
635
|
# Verify X_res is still a DataFrame
|
|
551
636
|
assert isinstance(X_res, pd.DataFrame)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|