upgini 1.1.261a3250.post2__tar.gz → 1.1.262a3250.post4__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.

Files changed (87) hide show
  1. {upgini-1.1.261a3250.post2/src/upgini.egg-info → upgini-1.1.262a3250.post4}/PKG-INFO +1 -1
  2. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/setup.py +1 -1
  3. upgini-1.1.262a3250.post4/src/upgini/autofe/date.py +51 -0
  4. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/feature.py +1 -1
  5. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/operand.py +2 -0
  6. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/unary.py +15 -8
  7. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/vector.py +5 -3
  8. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/data_source/data_source_publisher.py +10 -1
  9. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/dataset.py +20 -57
  10. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/features_enricher.py +1 -1
  11. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/search_task.py +1 -1
  12. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/datetime_utils.py +1 -1
  13. upgini-1.1.262a3250.post4/src/upgini/utils/target_utils.py +183 -0
  14. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4/src/upgini.egg-info}/PKG-INFO +1 -1
  15. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_autofe_operands.py +0 -1
  16. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_etalon_validation.py +5 -3
  17. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_features_enricher.py +1 -0
  18. upgini-1.1.262a3250.post4/tests/test_target_utils.py +134 -0
  19. upgini-1.1.261a3250.post2/src/upgini/autofe/date.py +0 -42
  20. upgini-1.1.261a3250.post2/src/upgini/utils/target_utils.py +0 -74
  21. upgini-1.1.261a3250.post2/tests/test_target_utils.py +0 -74
  22. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/LICENSE +0 -0
  23. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/README.md +0 -0
  24. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/pyproject.toml +0 -0
  25. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/setup.cfg +0 -0
  26. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/__init__.py +0 -0
  27. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/ads.py +0 -0
  28. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/ads_management/__init__.py +0 -0
  29. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/ads_management/ads_manager.py +0 -0
  30. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/__init__.py +0 -0
  31. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/all_operands.py +0 -0
  32. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/binary.py +0 -0
  33. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/autofe/groupby.py +0 -0
  34. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/data_source/__init__.py +0 -0
  35. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/errors.py +0 -0
  36. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/http.py +0 -0
  37. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/mdc/__init__.py +0 -0
  38. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/mdc/context.py +0 -0
  39. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/metadata.py +0 -0
  40. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/metrics.py +0 -0
  41. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/normalizer/__init__.py +0 -0
  42. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/normalizer/phone_normalizer.py +0 -0
  43. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/resource_bundle/__init__.py +0 -0
  44. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/resource_bundle/exceptions.py +0 -0
  45. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/resource_bundle/strings.properties +0 -0
  46. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/resource_bundle/strings_widget.properties +0 -0
  47. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/sampler/__init__.py +0 -0
  48. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/sampler/base.py +0 -0
  49. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/sampler/random_under_sampler.py +0 -0
  50. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/sampler/utils.py +0 -0
  51. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/spinner.py +0 -0
  52. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/__init__.py +0 -0
  53. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/base_search_key_detector.py +0 -0
  54. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/blocked_time_series.py +0 -0
  55. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/country_utils.py +0 -0
  56. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/custom_loss_utils.py +0 -0
  57. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/cv_utils.py +0 -0
  58. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/deduplicate_utils.py +0 -0
  59. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/display_utils.py +0 -0
  60. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/email_utils.py +0 -0
  61. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/fallback_progress_bar.py +0 -0
  62. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/features_validator.py +0 -0
  63. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/format.py +0 -0
  64. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/ip_utils.py +0 -0
  65. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/phone_utils.py +0 -0
  66. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/postal_code_utils.py +0 -0
  67. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/progress_bar.py +0 -0
  68. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/sklearn_ext.py +0 -0
  69. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/track_info.py +0 -0
  70. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/utils/warning_counter.py +0 -0
  71. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini/version_validator.py +0 -0
  72. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini.egg-info/SOURCES.txt +0 -0
  73. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini.egg-info/dependency_links.txt +0 -0
  74. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini.egg-info/requires.txt +0 -0
  75. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/src/upgini.egg-info/top_level.txt +0 -0
  76. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_binary_dataset.py +0 -0
  77. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_blocked_time_series.py +0 -0
  78. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_categorical_dataset.py +0 -0
  79. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_continuous_dataset.py +0 -0
  80. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_country_utils.py +0 -0
  81. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_custom_loss_utils.py +0 -0
  82. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_datetime_utils.py +0 -0
  83. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_email_utils.py +0 -0
  84. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_metrics.py +0 -0
  85. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_phone_utils.py +0 -0
  86. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_postal_code_utils.py +0 -0
  87. {upgini-1.1.261a3250.post2 → upgini-1.1.262a3250.post4}/tests/test_widget.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: upgini
3
- Version: 1.1.261a3250.post2
3
+ Version: 1.1.262a3250.post4
4
4
  Summary: Intelligent data search & enrichment for Machine Learning
5
5
  Home-page: https://upgini.com/
6
6
  Author: Upgini Developers
@@ -40,7 +40,7 @@ def send_log(msg: str):
40
40
 
41
41
 
42
42
  here = Path(__file__).parent.resolve()
43
- version = "1.1.261a3250.post2"
43
+ version = "1.1.262a3250.post4"
44
44
  try:
45
45
  send_log(f"Start setup PyLib version {version}")
46
46
  setup(
@@ -0,0 +1,51 @@
1
+ from typing import Optional, Union
2
+ import numpy as np
3
+ import pandas as pd
4
+
5
+ from upgini.autofe.operand import PandasOperand
6
+
7
+
8
+ class DateDiffMixin:
9
+ diff_unit: str = "D"
10
+ left_unit: Optional[str] = None
11
+ right_unit: Optional[str] = None
12
+
13
+ def _convert_to_date(
14
+ self, x: Union[pd.DataFrame, pd.Series], unit: Optional[str]
15
+ ) -> Union[pd.DataFrame, pd.Series]:
16
+ if isinstance(x, pd.DataFrame):
17
+ return x.apply(lambda y: self._convert_to_date(y, unit), axis=1)
18
+
19
+ return pd.to_datetime(x, unit=unit)
20
+
21
+
22
+ class DateDiff(PandasOperand, DateDiffMixin):
23
+ name = "date_diff"
24
+ is_binary = True
25
+ has_symmetry_importance = True
26
+
27
+ def calculate_binary(self, left: pd.Series, right: pd.Series) -> pd.Series:
28
+ left = self._convert_to_date(left, self.left_unit)
29
+ right = self._convert_to_date(right, self.right_unit)
30
+ return self.__replace_negative((left - right) / np.timedelta64(1, self.diff_unit))
31
+
32
+ def __replace_negative(self, x: Union[pd.DataFrame, pd.Series]):
33
+ x[x < 0] = None
34
+ return x
35
+
36
+
37
+ class DateDiffFuture(PandasOperand, DateDiffMixin):
38
+ name = "date_diff_future"
39
+ is_binary = True
40
+ has_symmetry_importance = True
41
+ is_vectorizable = False
42
+
43
+ def calculate_binary(self, left: pd.Series, right: pd.Series) -> pd.Series:
44
+ left = self._convert_to_date(left, self.left_unit)
45
+ right = self._convert_to_date(right, self.right_unit)
46
+ future = pd.to_datetime(dict(day=right.dt.day, month=right.dt.month, year=left.dt.year))
47
+ before = future[future < left]
48
+ future[future < left] = pd.to_datetime(dict(day=before.dt.day, month=before.dt.month, year=before.dt.year + 1))
49
+ diff = (future - left) / np.timedelta64(1, self.diff_unit)
50
+
51
+ return diff
@@ -305,7 +305,7 @@ class FeatureGroup:
305
305
  grouped_features = []
306
306
 
307
307
  def groupby_func(f: Feature) -> Tuple[Operand, Union[Column, Feature]]:
308
- return (f.op, f.children[0] if f.op.is_unary or f.op.is_vector else f.children[1])
308
+ return (f.op, f.children[0 if not f.op.is_vectorizable else f.op.group_index])
309
309
 
310
310
  for op_child, features in itertools.groupby(candidates, groupby_func):
311
311
  op, main_child = op_child
@@ -73,6 +73,8 @@ class PandasOperand(Operand, abc.ABC):
73
73
 
74
74
 
75
75
  class VectorizableMixin(Operand):
76
+ group_index: int = 1
77
+
76
78
  def validate_calculation(self, input_columns: List[str], **kwargs) -> Tuple[str, List[str]]:
77
79
  if not kwargs.get(MAIN_COLUMN):
78
80
  raise ValueError(f"Expected argument {MAIN_COLUMN} for grouping operator {self.name} not found")
@@ -1,12 +1,13 @@
1
- from upgini.autofe.operand import PandasOperand
1
+ from upgini.autofe.operand import PandasOperand, VectorizableMixin
2
2
  import numpy as np
3
3
  import pandas as pd
4
4
 
5
5
 
6
- class Abs(PandasOperand):
6
+ class Abs(PandasOperand, VectorizableMixin):
7
7
  name = "abs"
8
8
  is_unary = True
9
9
  is_vectorizable = True
10
+ group_index = 0
10
11
 
11
12
  def calculate_unary(self, data: pd.Series) -> pd.Series:
12
13
  return data.abs()
@@ -15,11 +16,12 @@ class Abs(PandasOperand):
15
16
  return data.abs()
16
17
 
17
18
 
18
- class Log(PandasOperand):
19
+ class Log(PandasOperand, VectorizableMixin):
19
20
  name = "log"
20
21
  is_unary = True
21
22
  is_vectorizable = True
22
23
  output_type = "float"
24
+ group_index = 0
23
25
 
24
26
  def calculate_unary(self, data: pd.Series) -> pd.Series:
25
27
  return self._round_value(np.log(np.abs(data.replace(0, np.nan))), 10)
@@ -28,11 +30,12 @@ class Log(PandasOperand):
28
30
  return self._round_value(np.log(data.replace(0, np.nan).abs()), 10)
29
31
 
30
32
 
31
- class Sqrt(PandasOperand):
33
+ class Sqrt(PandasOperand, VectorizableMixin):
32
34
  name = "sqrt"
33
35
  is_unary = True
34
36
  is_vectorizable = True
35
37
  output_type = "float"
38
+ group_index = 0
36
39
 
37
40
  def calculate_unary(self, data: pd.Series) -> pd.Series:
38
41
  return self._round_value(np.sqrt(np.abs(data)))
@@ -41,10 +44,11 @@ class Sqrt(PandasOperand):
41
44
  return self._round_value(np.sqrt(data.abs()))
42
45
 
43
46
 
44
- class Square(PandasOperand):
47
+ class Square(PandasOperand, VectorizableMixin):
45
48
  name = "square"
46
49
  is_unary = True
47
50
  is_vectorizable = True
51
+ group_index = 0
48
52
 
49
53
  def calculate_unary(self, data: pd.Series) -> pd.Series:
50
54
  return np.square(data)
@@ -53,11 +57,12 @@ class Square(PandasOperand):
53
57
  return np.square(data)
54
58
 
55
59
 
56
- class Sigmoid(PandasOperand):
60
+ class Sigmoid(PandasOperand, VectorizableMixin):
57
61
  name = "sigmoid"
58
62
  is_unary = True
59
63
  is_vectorizable = True
60
64
  output_type = "float"
65
+ group_index = 0
61
66
 
62
67
  def calculate_unary(self, data: pd.Series) -> pd.Series:
63
68
  return self._round_value(1 / (1 + np.exp(-data)))
@@ -66,12 +71,13 @@ class Sigmoid(PandasOperand):
66
71
  return self._round_value(1 / (1 + np.exp(-data)))
67
72
 
68
73
 
69
- class Floor(PandasOperand):
74
+ class Floor(PandasOperand, VectorizableMixin):
70
75
  name = "floor"
71
76
  is_unary = True
72
77
  is_vectorizable = True
73
78
  output_type = "int"
74
79
  input_type = "continuous"
80
+ group_index = 0
75
81
 
76
82
  def calculate_unary(self, data: pd.Series) -> pd.Series:
77
83
  return np.floor(data)
@@ -80,11 +86,12 @@ class Floor(PandasOperand):
80
86
  return np.floor(data)
81
87
 
82
88
 
83
- class Residual(PandasOperand):
89
+ class Residual(PandasOperand, VectorizableMixin):
84
90
  name = "residual"
85
91
  is_unary = True
86
92
  is_vectorizable = True
87
93
  input_type = "continuous"
94
+ group_index = 0
88
95
 
89
96
  def calculate_unary(self, data: pd.Series) -> pd.Series:
90
97
  return data - np.floor(data)
@@ -1,20 +1,22 @@
1
1
  from typing import List
2
2
  import pandas as pd
3
- from upgini.autofe.operand import PandasOperand
3
+ from upgini.autofe.operand import PandasOperand, VectorizableMixin
4
4
 
5
5
 
6
- class Mean(PandasOperand):
6
+ class Mean(PandasOperand, VectorizableMixin):
7
7
  name = "mean"
8
8
  output_type = "float"
9
9
  is_vector = True
10
+ group_index = 0
10
11
 
11
12
  def calculate_vector(self, data: List[pd.Series]) -> pd.Series:
12
13
  return pd.DataFrame(data).T.fillna(0).mean(axis=1)
13
14
 
14
15
 
15
- class Sum(PandasOperand):
16
+ class Sum(PandasOperand, VectorizableMixin):
16
17
  name = "sum"
17
18
  is_vector = True
19
+ group_index = 0
18
20
 
19
21
  def calculate_vector(self, data: List[pd.Series]) -> pd.Series:
20
22
  return pd.DataFrame(data).T.fillna(0).sum(axis=1)
@@ -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
@@ -61,6 +61,8 @@ class Dataset: # (pd.DataFrame):
61
61
  FIT_SAMPLE_WITH_EVAL_SET_THRESHOLD = 200_000
62
62
  MIN_SAMPLE_THRESHOLD = 5_000
63
63
  IMBALANCE_THESHOLD = 0.4
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
- min_class_count = count
464
- min_class_value = None
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
- unique_target = target.unique()
477
- for v in list(unique_target): # type: ignore
478
- current_class_count = len(train_segment.loc[target == v])
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: upgini
3
- Version: 1.1.261a3250.post2
3
+ Version: 1.1.262a3250.post4
4
4
  Summary: Intelligent data search & enrichment for Machine Learning
5
5
  Home-page: https://upgini.com/
6
6
  Author: Upgini Developers
@@ -14,7 +14,6 @@ def test_date_diff():
14
14
  operand = DateDiff()
15
15
  expected_result = pd.Series([10531, None])
16
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
17
 
19
18
 
20
19
  def test_date_diff_future():
@@ -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) == 400
263
+ assert len(dataset) == 1800
264
264
  value_counts = dataset.data["target"].value_counts()
265
265
  assert len(value_counts) == 4
266
- for label in dataset.data["target"].unique():
267
- assert value_counts[label] == 100
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,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