upgini 1.1.261a3250.post2__tar.gz → 1.1.262__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 upgini might be problematic. Click here for more details.
- {upgini-1.1.261a3250.post2/src/upgini.egg-info → upgini-1.1.262}/PKG-INFO +1 -1
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/setup.py +1 -1
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/all_operands.py +0 -3
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/data_source/data_source_publisher.py +10 -1
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/dataset.py +21 -58
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/features_enricher.py +1 -1
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/search_task.py +1 -1
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/datetime_utils.py +1 -1
- upgini-1.1.262/src/upgini/utils/target_utils.py +183 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262/src/upgini.egg-info}/PKG-INFO +1 -1
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini.egg-info/SOURCES.txt +0 -2
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_etalon_validation.py +5 -3
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_features_enricher.py +1 -0
- upgini-1.1.262/tests/test_target_utils.py +134 -0
- upgini-1.1.261a3250.post2/src/upgini/autofe/date.py +0 -42
- upgini-1.1.261a3250.post2/src/upgini/utils/target_utils.py +0 -74
- upgini-1.1.261a3250.post2/tests/test_autofe_operands.py +0 -28
- upgini-1.1.261a3250.post2/tests/test_target_utils.py +0 -74
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/LICENSE +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/README.md +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/pyproject.toml +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/setup.cfg +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/ads.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/ads_management/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/ads_management/ads_manager.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/binary.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/feature.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/groupby.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/operand.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/unary.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/autofe/vector.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/data_source/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/errors.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/http.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/mdc/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/mdc/context.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/metadata.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/metrics.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/normalizer/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/normalizer/phone_normalizer.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/resource_bundle/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/resource_bundle/exceptions.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/resource_bundle/strings.properties +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/resource_bundle/strings_widget.properties +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/sampler/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/sampler/base.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/sampler/random_under_sampler.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/sampler/utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/spinner.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/__init__.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/base_search_key_detector.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/blocked_time_series.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/country_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/custom_loss_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/cv_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/deduplicate_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/display_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/email_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/fallback_progress_bar.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/features_validator.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/format.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/ip_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/phone_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/postal_code_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/progress_bar.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/sklearn_ext.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/track_info.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/utils/warning_counter.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/version_validator.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini.egg-info/dependency_links.txt +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini.egg-info/requires.txt +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini.egg-info/top_level.txt +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_binary_dataset.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_blocked_time_series.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_categorical_dataset.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_continuous_dataset.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_country_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_custom_loss_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_datetime_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_email_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_metrics.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_phone_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_postal_code_utils.py +0 -0
- {upgini-1.1.261a3250.post2 → upgini-1.1.262}/tests/test_widget.py +0 -0
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
from typing import Dict
|
|
2
|
-
from upgini.autofe.date import DateDiff, DateDiffFuture
|
|
3
2
|
from upgini.autofe.groupby import GroupByThenAgg, GroupByThenRank
|
|
4
3
|
from upgini.autofe.operand import Operand
|
|
5
4
|
from upgini.autofe.unary import Abs, Log, Residual, Sqrt, Square, Sigmoid, Floor, Freq
|
|
@@ -36,8 +35,6 @@ ALL_OPERANDS: Dict[str, Operand] = {
|
|
|
36
35
|
Operand(name="GroupByThenNUnique", output_type="int", is_vectorizable=True, is_grouping=True),
|
|
37
36
|
Operand(name="GroupByThenFreq", output_type="float", is_grouping=True),
|
|
38
37
|
Sim(),
|
|
39
|
-
DateDiff(),
|
|
40
|
-
DateDiffFuture(),
|
|
41
38
|
]
|
|
42
39
|
}
|
|
43
40
|
|
{upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/data_source/data_source_publisher.py
RENAMED
|
@@ -40,7 +40,7 @@ class DataSourcePublisher:
|
|
|
40
40
|
if logs_enabled:
|
|
41
41
|
self.logger = LoggerFactory().get_logger(endpoint, api_key)
|
|
42
42
|
else:
|
|
43
|
-
self.logger = logging.getLogger()
|
|
43
|
+
self.logger = logging.getLogger("muted_logger")
|
|
44
44
|
self.logger.setLevel("FATAL")
|
|
45
45
|
|
|
46
46
|
def place(
|
|
@@ -170,6 +170,7 @@ class DataSourcePublisher:
|
|
|
170
170
|
print(msg)
|
|
171
171
|
self.logger.info(msg)
|
|
172
172
|
self._rest_client.stop_ads_management_task(task_id, trace_id)
|
|
173
|
+
raise
|
|
173
174
|
except Exception:
|
|
174
175
|
self.logger.exception("Failed to register data table")
|
|
175
176
|
raise
|
|
@@ -289,6 +290,7 @@ class DataSourcePublisher:
|
|
|
289
290
|
raise ValidationError("One of arguments: bq_table_id or search_keys should be presented")
|
|
290
291
|
if bq_table_id is not None and search_keys is not None:
|
|
291
292
|
raise ValidationError("Only one argument could be presented: bq_table_id or search_keys")
|
|
293
|
+
task_id = None
|
|
292
294
|
try:
|
|
293
295
|
search_keys = [k.value.value for k in search_keys] if search_keys else None
|
|
294
296
|
request = {"bqTableId": bq_table_id, "searchKeys": search_keys}
|
|
@@ -303,6 +305,13 @@ class DataSourcePublisher:
|
|
|
303
305
|
raise Exception("Failed to register ADS: " + status_response["errorMessage"])
|
|
304
306
|
|
|
305
307
|
print("Uploading successfully finished")
|
|
308
|
+
except KeyboardInterrupt:
|
|
309
|
+
if task_id is not None:
|
|
310
|
+
msg = f"Stopping AdsManagementTask {task_id}"
|
|
311
|
+
print(msg)
|
|
312
|
+
self.logger.info(msg)
|
|
313
|
+
self._rest_client.stop_ads_management_task(task_id, trace_id)
|
|
314
|
+
raise
|
|
306
315
|
except Exception:
|
|
307
316
|
self.logger.exception(f"Failed to upload table {bq_table_id}")
|
|
308
317
|
raise
|
|
@@ -39,10 +39,10 @@ from upgini.metadata import (
|
|
|
39
39
|
)
|
|
40
40
|
from upgini.normalizer.phone_normalizer import PhoneNormalizer
|
|
41
41
|
from upgini.resource_bundle import ResourceBundle, get_custom_bundle
|
|
42
|
-
from upgini.sampler.random_under_sampler import RandomUnderSampler
|
|
43
42
|
from upgini.search_task import SearchTask
|
|
44
43
|
from upgini.utils import combine_search_keys, find_numbers_with_decimal_comma
|
|
45
44
|
from upgini.utils.email_utils import EmailSearchKeyConverter
|
|
45
|
+
from upgini.utils.target_utils import balance_undersample
|
|
46
46
|
|
|
47
47
|
try:
|
|
48
48
|
from upgini.utils.progress_bar import CustomProgressBar as ProgressBar
|
|
@@ -60,7 +60,9 @@ class Dataset: # (pd.DataFrame):
|
|
|
60
60
|
FIT_SAMPLE_WITH_EVAL_SET_ROWS = 200_000
|
|
61
61
|
FIT_SAMPLE_WITH_EVAL_SET_THRESHOLD = 200_000
|
|
62
62
|
MIN_SAMPLE_THRESHOLD = 5_000
|
|
63
|
-
IMBALANCE_THESHOLD = 0.
|
|
63
|
+
IMBALANCE_THESHOLD = 0.6
|
|
64
|
+
BINARY_BOOTSTRAP_LOOPS = 5
|
|
65
|
+
MULTICLASS_BOOTSTRAP_LOOPS = 2
|
|
64
66
|
MIN_TARGET_CLASS_ROWS = 100
|
|
65
67
|
MAX_MULTICLASS_CLASS_COUNT = 100
|
|
66
68
|
MIN_SUPPORTED_DATE_TS = 946684800000 # 2000-01-01
|
|
@@ -460,10 +462,8 @@ class Dataset: # (pd.DataFrame):
|
|
|
460
462
|
self.task_type == ModelTaskType.BINARY and len(train_segment) > self.MIN_SAMPLE_THRESHOLD
|
|
461
463
|
):
|
|
462
464
|
count = len(train_segment)
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
target_column = self.etalon_def_checked.get(FileColumnMeaningType.TARGET.value, "")
|
|
466
|
-
target = train_segment[target_column].copy()
|
|
465
|
+
target_column = self.etalon_def_checked.get(FileColumnMeaningType.TARGET.value, TARGET)
|
|
466
|
+
target = train_segment[target_column]
|
|
467
467
|
target_classes_count = target.nunique()
|
|
468
468
|
|
|
469
469
|
if target_classes_count > self.MAX_MULTICLASS_CLASS_COUNT:
|
|
@@ -473,12 +473,9 @@ class Dataset: # (pd.DataFrame):
|
|
|
473
473
|
self.logger.warning(msg)
|
|
474
474
|
raise ValidationError(msg)
|
|
475
475
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
if current_class_count < min_class_count:
|
|
480
|
-
min_class_count = current_class_count
|
|
481
|
-
min_class_value = v
|
|
476
|
+
vc = target.value_counts()
|
|
477
|
+
min_class_value = vc.index[len(vc) - 1]
|
|
478
|
+
min_class_count = vc[min_class_value]
|
|
482
479
|
|
|
483
480
|
if min_class_count < self.MIN_TARGET_CLASS_ROWS:
|
|
484
481
|
msg = self.bundle.get("dataset_rarest_class_less_min").format(
|
|
@@ -491,53 +488,19 @@ class Dataset: # (pd.DataFrame):
|
|
|
491
488
|
min_class_threshold = min_class_percent * count
|
|
492
489
|
|
|
493
490
|
if min_class_count < min_class_threshold:
|
|
494
|
-
msg = self.bundle.get("dataset_rarest_class_less_threshold").format(
|
|
495
|
-
min_class_value, min_class_count, min_class_threshold, min_class_percent * 100
|
|
496
|
-
)
|
|
497
|
-
self.logger.warning(msg)
|
|
498
|
-
print(msg)
|
|
499
|
-
self.warning_counter.increment()
|
|
500
|
-
|
|
501
|
-
train_segment = train_segment.copy().sort_values(by=SYSTEM_RECORD_ID)
|
|
502
|
-
if self.task_type == ModelTaskType.MULTICLASS:
|
|
503
|
-
# Sort classes by rows count and find 25% quantile class
|
|
504
|
-
classes = target.value_counts().index
|
|
505
|
-
quantile25_idx = int(0.75 * len(classes))
|
|
506
|
-
quantile25_class = classes[quantile25_idx]
|
|
507
|
-
count_of_quantile25_class = len(target[target == quantile25_class])
|
|
508
|
-
msg = self.bundle.get("imbalance_multiclass").format(quantile25_class, count_of_quantile25_class)
|
|
509
|
-
self.logger.warning(msg)
|
|
510
|
-
print(msg)
|
|
511
|
-
# 25% and lower classes will stay as is. Higher classes will be downsampled
|
|
512
|
-
parts = []
|
|
513
|
-
for class_idx in range(quantile25_idx):
|
|
514
|
-
sampled = train_segment[train_segment[target_column] == classes[class_idx]].sample(
|
|
515
|
-
n=count_of_quantile25_class, random_state=self.random_state
|
|
516
|
-
)
|
|
517
|
-
parts.append(sampled)
|
|
518
|
-
for class_idx in range(quantile25_idx, len(classes)):
|
|
519
|
-
parts.append(train_segment[train_segment[target_column] == classes[class_idx]])
|
|
520
|
-
resampled_data = pd.concat(parts)
|
|
521
|
-
elif self.task_type == ModelTaskType.BINARY and min_class_count < self.MIN_SAMPLE_THRESHOLD / 2:
|
|
522
|
-
minority_class = train_segment[train_segment[target_column] == min_class_value]
|
|
523
|
-
majority_class = train_segment[train_segment[target_column] != min_class_value]
|
|
524
|
-
sampled_majority_class = majority_class.sample(
|
|
525
|
-
n=self.MIN_SAMPLE_THRESHOLD - min_class_count, random_state=self.random_state
|
|
526
|
-
)
|
|
527
|
-
resampled_data = train_segment[
|
|
528
|
-
(train_segment[SYSTEM_RECORD_ID].isin(minority_class[SYSTEM_RECORD_ID]))
|
|
529
|
-
| (train_segment[SYSTEM_RECORD_ID].isin(sampled_majority_class[SYSTEM_RECORD_ID]))
|
|
530
|
-
]
|
|
531
|
-
else:
|
|
532
|
-
sampler = RandomUnderSampler(random_state=self.random_state)
|
|
533
|
-
X = train_segment[SYSTEM_RECORD_ID]
|
|
534
|
-
X = X.to_frame(SYSTEM_RECORD_ID)
|
|
535
|
-
new_x, _ = sampler.fit_resample(X, target) # type: ignore
|
|
536
|
-
resampled_data = train_segment[train_segment[SYSTEM_RECORD_ID].isin(new_x[SYSTEM_RECORD_ID])]
|
|
537
|
-
|
|
538
|
-
self.data = resampled_data
|
|
539
|
-
self.logger.info(f"Shape after rebalance resampling: {self.data.shape}")
|
|
540
491
|
self.imbalanced = True
|
|
492
|
+
self.data = balance_undersample(
|
|
493
|
+
df=train_segment,
|
|
494
|
+
target_column=target_column,
|
|
495
|
+
task_type=self.task_type,
|
|
496
|
+
random_state=self.random_state,
|
|
497
|
+
imbalance_threshold=self.IMBALANCE_THESHOLD,
|
|
498
|
+
binary_bootstrap_loops=self.BINARY_BOOTSTRAP_LOOPS,
|
|
499
|
+
multiclass_bootstrap_loops=self.MULTICLASS_BOOTSTRAP_LOOPS,
|
|
500
|
+
logger=self.logger,
|
|
501
|
+
bundle=self.bundle,
|
|
502
|
+
warning_counter=self.warning_counter,
|
|
503
|
+
)
|
|
541
504
|
|
|
542
505
|
# Resample over fit threshold
|
|
543
506
|
if not self.imbalanced and EVAL_SET_INDEX in self.data.columns:
|
|
@@ -220,7 +220,7 @@ class FeaturesEnricher(TransformerMixin):
|
|
|
220
220
|
if logs_enabled:
|
|
221
221
|
self.logger = LoggerFactory().get_logger(endpoint, self._api_key, client_ip, client_visitorid)
|
|
222
222
|
else:
|
|
223
|
-
self.logger = logging.getLogger()
|
|
223
|
+
self.logger = logging.getLogger("muted_logger")
|
|
224
224
|
self.logger.setLevel("FATAL")
|
|
225
225
|
|
|
226
226
|
if len(kwargs) > 0:
|
|
@@ -57,7 +57,7 @@ class SearchTask:
|
|
|
57
57
|
if logger is not None:
|
|
58
58
|
self.logger = logger
|
|
59
59
|
else:
|
|
60
|
-
self.logger = logging.getLogger()
|
|
60
|
+
self.logger = logging.getLogger("muted_logger")
|
|
61
61
|
self.logger.setLevel("FATAL")
|
|
62
62
|
self.provider_metadata_v2: Optional[List[ProviderTaskMetadataV2]] = None
|
|
63
63
|
self.unused_features_for_generation: Optional[List[str]] = None
|
|
@@ -44,7 +44,7 @@ class DateTimeSearchKeyConverter:
|
|
|
44
44
|
if logger is not None:
|
|
45
45
|
self.logger = logger
|
|
46
46
|
else:
|
|
47
|
-
self.logger = logging.getLogger()
|
|
47
|
+
self.logger = logging.getLogger("muted_logger")
|
|
48
48
|
self.logger.setLevel("FATAL")
|
|
49
49
|
self.generated_features: List[str] = []
|
|
50
50
|
self.bundle = bundle or get_custom_bundle()
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Optional, Union
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pandas as pd
|
|
6
|
+
from pandas.api.types import is_numeric_dtype
|
|
7
|
+
|
|
8
|
+
from upgini.errors import ValidationError
|
|
9
|
+
from upgini.metadata import SYSTEM_RECORD_ID, ModelTaskType
|
|
10
|
+
from upgini.resource_bundle import ResourceBundle, bundle, get_custom_bundle
|
|
11
|
+
from upgini.sampler.random_under_sampler import RandomUnderSampler
|
|
12
|
+
from upgini.utils.warning_counter import WarningCounter
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def correct_string_target(y: Union[pd.Series, np.ndarray]) -> Union[pd.Series, np.ndarray]:
|
|
16
|
+
if isinstance(y, pd.Series):
|
|
17
|
+
return y.astype(str).astype("category").cat.codes
|
|
18
|
+
elif isinstance(y, np.ndarray):
|
|
19
|
+
return pd.Series(y).astype(str).astype("category").cat.codes.values
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def define_task(
|
|
23
|
+
y: pd.Series, has_date: bool = False, logger: Optional[logging.Logger] = None, silent: bool = False
|
|
24
|
+
) -> ModelTaskType:
|
|
25
|
+
if logger is None:
|
|
26
|
+
logger = logging.getLogger()
|
|
27
|
+
target = y.dropna()
|
|
28
|
+
if is_numeric_dtype(target):
|
|
29
|
+
target = target.loc[np.isfinite(target)]
|
|
30
|
+
else:
|
|
31
|
+
target = target.loc[target != ""]
|
|
32
|
+
if len(target) == 0:
|
|
33
|
+
raise ValidationError(bundle.get("empty_target"))
|
|
34
|
+
target_items = target.nunique()
|
|
35
|
+
if target_items == 1:
|
|
36
|
+
raise ValidationError(bundle.get("dataset_constant_target"))
|
|
37
|
+
if target_items == 2:
|
|
38
|
+
task = ModelTaskType.BINARY
|
|
39
|
+
else:
|
|
40
|
+
try:
|
|
41
|
+
target = pd.to_numeric(target)
|
|
42
|
+
is_numeric = True
|
|
43
|
+
except Exception:
|
|
44
|
+
is_numeric = False
|
|
45
|
+
|
|
46
|
+
# If any value is non numeric - multiclass
|
|
47
|
+
if not is_numeric:
|
|
48
|
+
task = ModelTaskType.MULTICLASS
|
|
49
|
+
else:
|
|
50
|
+
if target.nunique() <= 50 and is_int_encoding(target.unique()):
|
|
51
|
+
task = ModelTaskType.MULTICLASS
|
|
52
|
+
elif has_date:
|
|
53
|
+
task = ModelTaskType.REGRESSION
|
|
54
|
+
else:
|
|
55
|
+
non_zero_target = target[target != 0]
|
|
56
|
+
target_items = non_zero_target.nunique()
|
|
57
|
+
target_ratio = target_items / len(non_zero_target)
|
|
58
|
+
if (
|
|
59
|
+
(target.dtype.kind == "f" and np.any(target != target.astype(int))) # any non integer
|
|
60
|
+
or target_items > 50
|
|
61
|
+
or target_ratio > 0.2
|
|
62
|
+
):
|
|
63
|
+
task = ModelTaskType.REGRESSION
|
|
64
|
+
else:
|
|
65
|
+
task = ModelTaskType.MULTICLASS
|
|
66
|
+
|
|
67
|
+
logger.info(f"Detected task type: {task}")
|
|
68
|
+
if not silent:
|
|
69
|
+
print(bundle.get("target_type_detected").format(task))
|
|
70
|
+
return task
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def is_int_encoding(unique_values):
|
|
74
|
+
return set(unique_values) == set(range(len(unique_values))) or set(unique_values) == set(
|
|
75
|
+
range(1, len(unique_values) + 1)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def balance_undersample(
|
|
80
|
+
df: pd.DataFrame,
|
|
81
|
+
target_column: str,
|
|
82
|
+
task_type: ModelTaskType,
|
|
83
|
+
random_state: int,
|
|
84
|
+
imbalance_threshold: int = 0.2,
|
|
85
|
+
min_sample_threshold: int = 5000,
|
|
86
|
+
binary_bootstrap_loops: int = 5,
|
|
87
|
+
multiclass_bootstrap_loops: int = 2,
|
|
88
|
+
logger: Optional[logging.Logger] = None,
|
|
89
|
+
bundle: Optional[ResourceBundle] = None,
|
|
90
|
+
warning_counter: Optional[WarningCounter] = None,
|
|
91
|
+
) -> pd.DataFrame:
|
|
92
|
+
if logger is None:
|
|
93
|
+
logger = logging.getLogger("muted_logger")
|
|
94
|
+
logger.setLevel("FATAL")
|
|
95
|
+
bundle = bundle or get_custom_bundle()
|
|
96
|
+
if SYSTEM_RECORD_ID not in df.columns:
|
|
97
|
+
raise Exception("System record id must be presented for undersampling")
|
|
98
|
+
|
|
99
|
+
count = len(df)
|
|
100
|
+
target = df[target_column].copy()
|
|
101
|
+
target_classes_count = target.nunique()
|
|
102
|
+
|
|
103
|
+
vc = target.value_counts()
|
|
104
|
+
max_class_value = vc.index[0]
|
|
105
|
+
min_class_value = vc.index[len(vc) - 1]
|
|
106
|
+
max_class_count = vc[max_class_value]
|
|
107
|
+
min_class_count = vc[min_class_value]
|
|
108
|
+
|
|
109
|
+
min_class_percent = imbalance_threshold / target_classes_count
|
|
110
|
+
min_class_threshold = min_class_percent * count
|
|
111
|
+
|
|
112
|
+
resampled_data = df
|
|
113
|
+
df = df.copy().sort_values(by=SYSTEM_RECORD_ID)
|
|
114
|
+
if task_type == ModelTaskType.MULTICLASS:
|
|
115
|
+
# Sort classes by rows count and find 25% quantile class
|
|
116
|
+
classes = vc.index
|
|
117
|
+
quantile25_idx = int(0.75 * len(classes)) - 1
|
|
118
|
+
quantile25_class = classes[quantile25_idx]
|
|
119
|
+
quantile25_class_cnt = vc[quantile25_class]
|
|
120
|
+
|
|
121
|
+
if max_class_count > (quantile25_class_cnt * multiclass_bootstrap_loops):
|
|
122
|
+
msg = bundle.get("imbalance_multiclass").format(quantile25_class, quantile25_class_cnt)
|
|
123
|
+
logger.warning(msg)
|
|
124
|
+
print(msg)
|
|
125
|
+
if warning_counter:
|
|
126
|
+
warning_counter.increment()
|
|
127
|
+
|
|
128
|
+
# 25% and lower classes will stay as is. Higher classes will be downsampled
|
|
129
|
+
sample_strategy = dict()
|
|
130
|
+
for class_idx in range(quantile25_idx):
|
|
131
|
+
# compare class count with count_of_quantile25_class * 2
|
|
132
|
+
class_value = classes[class_idx]
|
|
133
|
+
class_count = vc[class_value]
|
|
134
|
+
sample_strategy[class_value] = min(class_count, quantile25_class_cnt * multiclass_bootstrap_loops)
|
|
135
|
+
sampler = RandomUnderSampler(
|
|
136
|
+
sampling_strategy=sample_strategy, random_state=random_state
|
|
137
|
+
)
|
|
138
|
+
X = df[SYSTEM_RECORD_ID]
|
|
139
|
+
X = X.to_frame(SYSTEM_RECORD_ID)
|
|
140
|
+
new_x, _ = sampler.fit_resample(X, target) # type: ignore
|
|
141
|
+
|
|
142
|
+
resampled_data = df[df[SYSTEM_RECORD_ID].isin(new_x[SYSTEM_RECORD_ID])]
|
|
143
|
+
elif len(df) > min_sample_threshold and min_class_count < min_sample_threshold / 2:
|
|
144
|
+
msg = bundle.get("dataset_rarest_class_less_threshold").format(
|
|
145
|
+
min_class_value, min_class_count, min_class_threshold, min_class_percent * 100
|
|
146
|
+
)
|
|
147
|
+
logger.warning(msg)
|
|
148
|
+
print(msg)
|
|
149
|
+
if warning_counter:
|
|
150
|
+
warning_counter.increment()
|
|
151
|
+
|
|
152
|
+
# fill up to min_sample_threshold by majority class
|
|
153
|
+
minority_class = df[df[target_column] == min_class_value]
|
|
154
|
+
majority_class = df[df[target_column] != min_class_value]
|
|
155
|
+
sample_size = min(len(majority_class), min_sample_threshold - min_class_count)
|
|
156
|
+
sampled_majority_class = majority_class.sample(
|
|
157
|
+
n=sample_size, random_state=random_state
|
|
158
|
+
)
|
|
159
|
+
resampled_data = df[
|
|
160
|
+
(df[SYSTEM_RECORD_ID].isin(minority_class[SYSTEM_RECORD_ID]))
|
|
161
|
+
| (df[SYSTEM_RECORD_ID].isin(sampled_majority_class[SYSTEM_RECORD_ID]))
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
elif max_class_count > min_class_count * binary_bootstrap_loops:
|
|
165
|
+
msg = bundle.get("dataset_rarest_class_less_threshold").format(
|
|
166
|
+
min_class_value, min_class_count, min_class_threshold, min_class_percent * 100
|
|
167
|
+
)
|
|
168
|
+
logger.warning(msg)
|
|
169
|
+
print(msg)
|
|
170
|
+
if warning_counter:
|
|
171
|
+
warning_counter.increment()
|
|
172
|
+
|
|
173
|
+
sampler = RandomUnderSampler(
|
|
174
|
+
sampling_strategy={max_class_value: binary_bootstrap_loops * min_class_count}, random_state=random_state
|
|
175
|
+
)
|
|
176
|
+
X = df[SYSTEM_RECORD_ID]
|
|
177
|
+
X = X.to_frame(SYSTEM_RECORD_ID)
|
|
178
|
+
new_x, _ = sampler.fit_resample(X, target) # type: ignore
|
|
179
|
+
|
|
180
|
+
resampled_data = df[df[SYSTEM_RECORD_ID].isin(new_x[SYSTEM_RECORD_ID])]
|
|
181
|
+
|
|
182
|
+
logger.info(f"Shape after rebalance resampling: {resampled_data}")
|
|
183
|
+
return resampled_data
|
|
@@ -23,7 +23,6 @@ src/upgini/ads_management/ads_manager.py
|
|
|
23
23
|
src/upgini/autofe/__init__.py
|
|
24
24
|
src/upgini/autofe/all_operands.py
|
|
25
25
|
src/upgini/autofe/binary.py
|
|
26
|
-
src/upgini/autofe/date.py
|
|
27
26
|
src/upgini/autofe/feature.py
|
|
28
27
|
src/upgini/autofe/groupby.py
|
|
29
28
|
src/upgini/autofe/operand.py
|
|
@@ -64,7 +63,6 @@ src/upgini/utils/sklearn_ext.py
|
|
|
64
63
|
src/upgini/utils/target_utils.py
|
|
65
64
|
src/upgini/utils/track_info.py
|
|
66
65
|
src/upgini/utils/warning_counter.py
|
|
67
|
-
tests/test_autofe_operands.py
|
|
68
66
|
tests/test_binary_dataset.py
|
|
69
67
|
tests/test_blocked_time_series.py
|
|
70
68
|
tests/test_categorical_dataset.py
|
|
@@ -260,11 +260,13 @@ def test_imbalanced_target():
|
|
|
260
260
|
}
|
|
261
261
|
dataset.task_type = ModelTaskType.MULTICLASS
|
|
262
262
|
dataset._Dataset__resample()
|
|
263
|
-
assert len(dataset) ==
|
|
263
|
+
assert len(dataset) == 1800
|
|
264
264
|
value_counts = dataset.data["target"].value_counts()
|
|
265
265
|
assert len(value_counts) == 4
|
|
266
|
-
|
|
267
|
-
|
|
266
|
+
assert value_counts["a"] == 100
|
|
267
|
+
assert value_counts["b"] == 400
|
|
268
|
+
assert value_counts["c"] == 500
|
|
269
|
+
assert value_counts["d"] == 800
|
|
268
270
|
|
|
269
271
|
|
|
270
272
|
def test_fail_on_small_class_observations():
|
|
@@ -2163,6 +2163,7 @@ def test_idempotent_order_with_imbalanced_dataset(requests_mock: Mocker):
|
|
|
2163
2163
|
pass
|
|
2164
2164
|
|
|
2165
2165
|
actual_result_df = result_wrapper.df.sort_values(by="system_record_id").reset_index(drop=True)
|
|
2166
|
+
# actual_result_df.to_parquet(expected_result_path)
|
|
2166
2167
|
assert_frame_equal(actual_result_df, expected_result_df)
|
|
2167
2168
|
|
|
2168
2169
|
for i in range(5):
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import pytest
|
|
4
|
+
from pandas.testing import assert_frame_equal
|
|
5
|
+
|
|
6
|
+
from upgini.errors import ValidationError
|
|
7
|
+
from upgini.metadata import SYSTEM_RECORD_ID, TARGET, ModelTaskType
|
|
8
|
+
from upgini.resource_bundle import bundle
|
|
9
|
+
from upgini.utils.target_utils import balance_undersample, define_task
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_invalid_target():
|
|
13
|
+
y = pd.Series(["", "", ""])
|
|
14
|
+
with pytest.raises(ValidationError, match=bundle.get("empty_target")):
|
|
15
|
+
define_task(y)
|
|
16
|
+
|
|
17
|
+
y = pd.Series([np.nan, np.inf, -np.inf])
|
|
18
|
+
with pytest.raises(ValidationError, match=bundle.get("empty_target")):
|
|
19
|
+
define_task(y)
|
|
20
|
+
|
|
21
|
+
y = pd.Series([1, 1, 1, 1, 1])
|
|
22
|
+
with pytest.raises(ValidationError, match=bundle.get("dataset_constant_target")):
|
|
23
|
+
define_task(y)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_define_binary_task_type():
|
|
27
|
+
y = pd.Series([0, 1, 0, 1, 0, 1])
|
|
28
|
+
assert define_task(y, False) == ModelTaskType.BINARY
|
|
29
|
+
assert define_task(y, True) == ModelTaskType.BINARY
|
|
30
|
+
|
|
31
|
+
y = pd.Series(["a", "b", "a", "b", "a"])
|
|
32
|
+
assert define_task(y, False) == ModelTaskType.BINARY
|
|
33
|
+
assert define_task(y, True) == ModelTaskType.BINARY
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_define_multiclass_task_type():
|
|
37
|
+
y = pd.Series(range(1, 51))
|
|
38
|
+
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
39
|
+
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
40
|
+
|
|
41
|
+
y = pd.Series([float(x) for x in range(1, 51)])
|
|
42
|
+
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
43
|
+
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
44
|
+
|
|
45
|
+
y = pd.Series(range(0, 50))
|
|
46
|
+
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
47
|
+
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
48
|
+
|
|
49
|
+
y = pd.Series(["a", "b", "c", "b", "a"])
|
|
50
|
+
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
51
|
+
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
52
|
+
|
|
53
|
+
y = pd.Series(["0", "1", "2", "3", "a"])
|
|
54
|
+
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
55
|
+
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
56
|
+
|
|
57
|
+
y = pd.Series([0.0, 3.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 3.0, 5.0, 3.0])
|
|
58
|
+
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_define_regression_task_type():
|
|
62
|
+
y = pd.Series([0.0, 3.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 3.0, 5.0, 3.0])
|
|
63
|
+
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
64
|
+
|
|
65
|
+
y = pd.Series([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.5])
|
|
66
|
+
assert define_task(y, False) == ModelTaskType.REGRESSION
|
|
67
|
+
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
68
|
+
|
|
69
|
+
y = pd.Series([0, 1, 2, 3, 4, 5, 6, 8])
|
|
70
|
+
assert define_task(y, False) == ModelTaskType.REGRESSION
|
|
71
|
+
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
72
|
+
|
|
73
|
+
y = pd.Series([0.0, 3.0, 5.0, 0.0, 5.0, 0.0, 3.0])
|
|
74
|
+
assert define_task(y, False) == ModelTaskType.REGRESSION
|
|
75
|
+
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_balance_undersampling_binary():
|
|
79
|
+
df = pd.DataFrame({SYSTEM_RECORD_ID: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], TARGET: [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]})
|
|
80
|
+
balanced_df = balance_undersample(
|
|
81
|
+
df, TARGET, ModelTaskType.BINARY, 42, imbalance_threshold=0.1, min_sample_threshold=2
|
|
82
|
+
)
|
|
83
|
+
# Get all minority class and 5x of majority class if minority class count (1)
|
|
84
|
+
# more or equal to min_sample_threshold/2 (1)
|
|
85
|
+
expected_df = pd.DataFrame({
|
|
86
|
+
SYSTEM_RECORD_ID: [1, 2, 3, 7, 9, 10],
|
|
87
|
+
TARGET: [0, 1, 0, 0, 0, 0]
|
|
88
|
+
})
|
|
89
|
+
assert_frame_equal(balanced_df.sort_values(by=SYSTEM_RECORD_ID).reset_index(drop=True), expected_df)
|
|
90
|
+
|
|
91
|
+
balanced_df = balance_undersample(
|
|
92
|
+
df, TARGET, ModelTaskType.BINARY, 42, imbalance_threshold=0.1, min_sample_threshold=8
|
|
93
|
+
)
|
|
94
|
+
# Get all minority class and fill up to min_sample_threshold (8) by majority class
|
|
95
|
+
expected_df = pd.DataFrame({
|
|
96
|
+
SYSTEM_RECORD_ID: [1, 2, 3, 4, 6, 7, 9, 10],
|
|
97
|
+
TARGET: [0, 1, 0, 0, 0, 0, 0, 0]
|
|
98
|
+
})
|
|
99
|
+
assert_frame_equal(balanced_df.sort_values(by=SYSTEM_RECORD_ID).reset_index(drop=True), expected_df)
|
|
100
|
+
|
|
101
|
+
df = pd.DataFrame({"system_record_id": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], TARGET: [0, 1, 0, 0, 0, 0, 0, 0, 1, 0]})
|
|
102
|
+
balanced_df = balance_undersample(
|
|
103
|
+
df, "target", ModelTaskType.BINARY, 42, imbalance_threshold=0.1, min_sample_threshold=4
|
|
104
|
+
)
|
|
105
|
+
# Get full dataset if majority class count (8) less than x5 of minority class count (2)
|
|
106
|
+
assert_frame_equal(balanced_df, df)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_balance_undersaampling_multiclass():
|
|
110
|
+
df = pd.DataFrame({
|
|
111
|
+
SYSTEM_RECORD_ID: [1, 2, 3, 4, 5, 6],
|
|
112
|
+
TARGET: ["a", "b", "c", "c", "b", "c"]
|
|
113
|
+
# a - 1, b - 2, c - 3
|
|
114
|
+
})
|
|
115
|
+
balanced_df = balance_undersample(
|
|
116
|
+
df, TARGET, ModelTaskType.MULTICLASS, 42, imbalance_threshold=0.1, min_sample_threshold=10
|
|
117
|
+
)
|
|
118
|
+
# Get full dataset if majority class count (3) less than x2 of 25% class (b) count (2)
|
|
119
|
+
assert_frame_equal(balanced_df, df)
|
|
120
|
+
|
|
121
|
+
df = pd.DataFrame({
|
|
122
|
+
SYSTEM_RECORD_ID: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
|
|
123
|
+
TARGET: ["a", "b", "c", "c", "c", "b", "c", "d", "d", "d", "c"]
|
|
124
|
+
# a - 1, b - 2, c - 5, d - 3
|
|
125
|
+
})
|
|
126
|
+
balanced_df = balance_undersample(
|
|
127
|
+
df, TARGET, ModelTaskType.MULTICLASS, 42, imbalance_threshold=0.1, min_sample_threshold=10
|
|
128
|
+
)
|
|
129
|
+
expected_df = pd.DataFrame({
|
|
130
|
+
SYSTEM_RECORD_ID: [1, 2, 3, 4, 5, 6, 8, 9, 10, 11],
|
|
131
|
+
TARGET: ["a", "b", "c", "c", "c", "b", "d", "d", "d", "c"]
|
|
132
|
+
})
|
|
133
|
+
# Get all of 25% quantile class (b) and minor classes (a) and x2 (or all if less) of major classes
|
|
134
|
+
assert_frame_equal(balanced_df.sort_values(by=SYSTEM_RECORD_ID).reset_index(drop=True), expected_df)
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
import pandas as pd
|
|
3
|
-
|
|
4
|
-
from upgini.autofe.operand import PandasOperand, VectorizableMixin
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class DateDiff(PandasOperand, VectorizableMixin):
|
|
8
|
-
name = "date_diff"
|
|
9
|
-
is_binary = True
|
|
10
|
-
has_symmetry_importance = True
|
|
11
|
-
is_vectorizable = True
|
|
12
|
-
unit: str = "D"
|
|
13
|
-
|
|
14
|
-
def calculate_binary(self, left: pd.Series, right: pd.Series) -> pd.Series:
|
|
15
|
-
return self.__replace_negative((left - right) / np.timedelta64(1, self.unit))
|
|
16
|
-
|
|
17
|
-
def calculate_group(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame:
|
|
18
|
-
group_column, value_columns = self.validate_calculation(data.columns, **kwargs)
|
|
19
|
-
d1 = data[value_columns]
|
|
20
|
-
d2 = data[group_column]
|
|
21
|
-
|
|
22
|
-
return self.__replace_negative(d1.sub(d2, axis=0) / np.timedelta64(1, self.unit))
|
|
23
|
-
|
|
24
|
-
def __replace_negative(self, df):
|
|
25
|
-
df[df < 0] = None
|
|
26
|
-
return df
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class DateDiffFuture(PandasOperand):
|
|
30
|
-
name = "date_diff_future"
|
|
31
|
-
is_binary = True
|
|
32
|
-
has_symmetry_importance = True
|
|
33
|
-
is_vectorizable = False
|
|
34
|
-
unit: str = "D"
|
|
35
|
-
|
|
36
|
-
def calculate_binary(self, left: pd.Series, right: pd.Series) -> pd.Series:
|
|
37
|
-
future = pd.to_datetime(dict(day=right.dt.day, month=right.dt.month, year=left.dt.year))
|
|
38
|
-
before = future[future < left]
|
|
39
|
-
future[future < left] = pd.to_datetime(dict(day=before.dt.day, month=before.dt.month, year=before.dt.year + 1))
|
|
40
|
-
diff = (future - left) / np.timedelta64(1, self.unit)
|
|
41
|
-
|
|
42
|
-
return diff
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
from typing import Optional, Union
|
|
3
|
-
|
|
4
|
-
import numpy as np
|
|
5
|
-
import pandas as pd
|
|
6
|
-
from pandas.api.types import is_numeric_dtype
|
|
7
|
-
|
|
8
|
-
from upgini.errors import ValidationError
|
|
9
|
-
from upgini.metadata import ModelTaskType
|
|
10
|
-
from upgini.resource_bundle import bundle
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def correct_string_target(y: Union[pd.Series, np.ndarray]) -> Union[pd.Series, np.ndarray]:
|
|
14
|
-
if isinstance(y, pd.Series):
|
|
15
|
-
return y.astype(str).astype("category").cat.codes
|
|
16
|
-
elif isinstance(y, np.ndarray):
|
|
17
|
-
return pd.Series(y).astype(str).astype("category").cat.codes.values
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def define_task(
|
|
21
|
-
y: pd.Series, has_date: bool = False, logger: Optional[logging.Logger] = None, silent: bool = False
|
|
22
|
-
) -> ModelTaskType:
|
|
23
|
-
if logger is None:
|
|
24
|
-
logger = logging.getLogger()
|
|
25
|
-
target = y.dropna()
|
|
26
|
-
if is_numeric_dtype(target):
|
|
27
|
-
target = target.loc[np.isfinite(target)]
|
|
28
|
-
else:
|
|
29
|
-
target = target.loc[target != ""]
|
|
30
|
-
if len(target) == 0:
|
|
31
|
-
raise ValidationError(bundle.get("empty_target"))
|
|
32
|
-
target_items = target.nunique()
|
|
33
|
-
if target_items == 1:
|
|
34
|
-
raise ValidationError(bundle.get("dataset_constant_target"))
|
|
35
|
-
if target_items == 2:
|
|
36
|
-
task = ModelTaskType.BINARY
|
|
37
|
-
else:
|
|
38
|
-
try:
|
|
39
|
-
target = pd.to_numeric(target)
|
|
40
|
-
is_numeric = True
|
|
41
|
-
except Exception:
|
|
42
|
-
is_numeric = False
|
|
43
|
-
|
|
44
|
-
# If any value is non numeric - multiclass
|
|
45
|
-
if not is_numeric:
|
|
46
|
-
task = ModelTaskType.MULTICLASS
|
|
47
|
-
else:
|
|
48
|
-
if target.nunique() <= 50 and is_int_encoding(target.unique()):
|
|
49
|
-
task = ModelTaskType.MULTICLASS
|
|
50
|
-
elif has_date:
|
|
51
|
-
task = ModelTaskType.REGRESSION
|
|
52
|
-
else:
|
|
53
|
-
non_zero_target = target[target != 0]
|
|
54
|
-
target_items = non_zero_target.nunique()
|
|
55
|
-
target_ratio = target_items / len(non_zero_target)
|
|
56
|
-
if (
|
|
57
|
-
(target.dtype.kind == "f" and np.any(target != target.astype(int))) # any non integer
|
|
58
|
-
or target_items > 50
|
|
59
|
-
or target_ratio > 0.2
|
|
60
|
-
):
|
|
61
|
-
task = ModelTaskType.REGRESSION
|
|
62
|
-
else:
|
|
63
|
-
task = ModelTaskType.MULTICLASS
|
|
64
|
-
|
|
65
|
-
logger.info(f"Detected task type: {task}")
|
|
66
|
-
if not silent:
|
|
67
|
-
print(bundle.get("target_type_detected").format(task))
|
|
68
|
-
return task
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def is_int_encoding(unique_values):
|
|
72
|
-
return set(unique_values) == set(range(len(unique_values))) or set(unique_values) == set(
|
|
73
|
-
range(1, len(unique_values) + 1)
|
|
74
|
-
)
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import pandas as pd
|
|
2
|
-
from upgini.autofe.date import DateDiff, DateDiffFuture
|
|
3
|
-
|
|
4
|
-
from datetime import datetime
|
|
5
|
-
from pandas.testing import assert_series_equal
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def test_date_diff():
|
|
9
|
-
df = pd.DataFrame(
|
|
10
|
-
[[datetime(1993, 12, 10), datetime(2022, 10, 10)], [datetime(2023, 10, 10), datetime(2022, 10, 10)]],
|
|
11
|
-
columns=["date1", "date2"],
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
operand = DateDiff()
|
|
15
|
-
expected_result = pd.Series([10531, None])
|
|
16
|
-
assert_series_equal(operand.calculate_binary(df.date2, df.date1), expected_result)
|
|
17
|
-
assert_series_equal(operand.calculate_group(df, main_column="date1")["date2"].rename(None), expected_result)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def test_date_diff_future():
|
|
21
|
-
df = pd.DataFrame(
|
|
22
|
-
[[datetime(1993, 12, 10), datetime(2022, 10, 10)], [datetime(1993, 4, 10), datetime(2022, 10, 10)]],
|
|
23
|
-
columns=["date1", "date2"],
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
operand = DateDiffFuture()
|
|
27
|
-
expected_result = pd.Series([61.0, 182.0])
|
|
28
|
-
assert_series_equal(operand.calculate_binary(df.date2, df.date1), expected_result)
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
import pandas as pd
|
|
3
|
-
import pytest
|
|
4
|
-
|
|
5
|
-
from upgini.errors import ValidationError
|
|
6
|
-
from upgini.metadata import ModelTaskType
|
|
7
|
-
from upgini.resource_bundle import bundle
|
|
8
|
-
from upgini.utils.target_utils import define_task
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def test_invalid_target():
|
|
12
|
-
y = pd.Series(["", "", ""])
|
|
13
|
-
with pytest.raises(ValidationError, match=bundle.get("empty_target")):
|
|
14
|
-
define_task(y)
|
|
15
|
-
|
|
16
|
-
y = pd.Series([np.nan, np.inf, -np.inf])
|
|
17
|
-
with pytest.raises(ValidationError, match=bundle.get("empty_target")):
|
|
18
|
-
define_task(y)
|
|
19
|
-
|
|
20
|
-
y = pd.Series([1, 1, 1, 1, 1])
|
|
21
|
-
with pytest.raises(ValidationError, match=bundle.get("dataset_constant_target")):
|
|
22
|
-
define_task(y)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def test_define_binary_task_type():
|
|
26
|
-
y = pd.Series([0, 1, 0, 1, 0, 1])
|
|
27
|
-
assert define_task(y, False) == ModelTaskType.BINARY
|
|
28
|
-
assert define_task(y, True) == ModelTaskType.BINARY
|
|
29
|
-
|
|
30
|
-
y = pd.Series(["a", "b", "a", "b", "a"])
|
|
31
|
-
assert define_task(y, False) == ModelTaskType.BINARY
|
|
32
|
-
assert define_task(y, True) == ModelTaskType.BINARY
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def test_define_multiclass_task_type():
|
|
36
|
-
y = pd.Series(range(1, 51))
|
|
37
|
-
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
38
|
-
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
39
|
-
|
|
40
|
-
y = pd.Series([float(x) for x in range(1, 51)])
|
|
41
|
-
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
42
|
-
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
43
|
-
|
|
44
|
-
y = pd.Series(range(0, 50))
|
|
45
|
-
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
46
|
-
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
47
|
-
|
|
48
|
-
y = pd.Series(["a", "b", "c", "b", "a"])
|
|
49
|
-
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
50
|
-
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
51
|
-
|
|
52
|
-
y = pd.Series(["0", "1", "2", "3", "a"])
|
|
53
|
-
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
54
|
-
assert define_task(y, True) == ModelTaskType.MULTICLASS
|
|
55
|
-
|
|
56
|
-
y = pd.Series([0.0, 3.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 3.0, 5.0, 3.0])
|
|
57
|
-
assert define_task(y, False) == ModelTaskType.MULTICLASS
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def test_define_regression_task_type():
|
|
61
|
-
y = pd.Series([0.0, 3.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 5.0, 0.0, 5.0, 0.0, 3.0, 0.0, 3.0, 5.0, 3.0])
|
|
62
|
-
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
63
|
-
|
|
64
|
-
y = pd.Series([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.5])
|
|
65
|
-
assert define_task(y, False) == ModelTaskType.REGRESSION
|
|
66
|
-
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
67
|
-
|
|
68
|
-
y = pd.Series([0, 1, 2, 3, 4, 5, 6, 8])
|
|
69
|
-
assert define_task(y, False) == ModelTaskType.REGRESSION
|
|
70
|
-
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
71
|
-
|
|
72
|
-
y = pd.Series([0.0, 3.0, 5.0, 0.0, 5.0, 0.0, 3.0])
|
|
73
|
-
assert define_task(y, False) == ModelTaskType.REGRESSION
|
|
74
|
-
assert define_task(y, True) == ModelTaskType.REGRESSION
|
|
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
|
{upgini-1.1.261a3250.post2 → upgini-1.1.262}/src/upgini/resource_bundle/strings_widget.properties
RENAMED
|
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
|