data_transform_param.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. from pipeline.param.base_param import BaseParam
  19. class DataTransformParam(BaseParam):
  20. """
  21. Define data_transform parameters that used in federated ml.
  22. Parameters
  23. ----------
  24. input_format : str, accepted 'dense','sparse' 'tag' only in this version. default: 'dense'.
  25. please have a look at this tutorial at "DataTransform" section of federatedml/util/README.md.
  26. Formally,
  27. dense input format data should be set to "dense",
  28. svm-light input format data should be set to "sparse",
  29. tag or tag:value input format data should be set to "tag".
  30. delimitor : str, the delimitor of data input, default: ','
  31. data_type : str, the data type of data input, accepted 'float','float64','int','int64','str','long'
  32. "default: "float64"
  33. exclusive_data_type : dict, the key of dict is col_name, the value is data_type, use to specified special data type
  34. of some features.
  35. tag_with_value: bool, use if input_format is 'tag', if tag_with_value is True,
  36. input column data format should be tag[delimitor]value, otherwise is tag only
  37. tag_value_delimitor: str, use if input_format is 'tag' and 'tag_with_value' is True,
  38. delimitor of tag[delimitor]value column value.
  39. missing_fill : bool, need to fill missing value or not, accepted only True/False, default: True
  40. default_value : None or single object type or list, the value to replace missing value.
  41. if None, it will use default value define in federatedml/feature/imputer.py,
  42. if single object, will fill missing value with this object,
  43. if list, it's length should be the sample of input data' feature dimension,
  44. means that if some column happens to have missing values, it will replace it
  45. the value by element in the identical position of this list.
  46. default: None
  47. missing_fill_method: None or str, the method to replace missing value, should be one of [None, 'min', 'max', 'mean', 'designated'], default: None
  48. missing_impute: None or list, element of list can be any type, or auto generated if value is None, define which values to be consider as missing, default: None
  49. outlier_replace: bool, need to replace outlier value or not, accepted only True/False, default: True
  50. outlier_replace_method: None or str, the method to replace missing value, should be one of [None, 'min', 'max', 'mean', 'designated'], default: None
  51. outlier_impute: None or list, element of list can be any type, which values should be regard as missing value, default: None
  52. outlier_replace_value: None or single object type or list, the value to replace outlier.
  53. if None, it will use default value define in federatedml/feature/imputer.py,
  54. if single object, will replace outlier with this object,
  55. if list, it's length should be the sample of input data' feature dimension,
  56. means that if some column happens to have outliers, it will replace it
  57. the value by element in the identical position of this list.
  58. default: None
  59. with_label : bool, True if input data consist of label, False otherwise. default: 'false'
  60. label_name : str, column_name of the column where label locates, only use in dense-inputformat. default: 'y'
  61. label_type : object, accepted 'int','int64','float','float64','long','str' only,
  62. use when with_label is True. default: 'false'
  63. output_format : str, accepted 'dense','sparse' only in this version. default: 'dense'
  64. with_match_id: bool, True if dataset has match_id, default: False
  65. """
  66. def __init__(self, input_format="dense", delimitor=',', data_type='float64',
  67. exclusive_data_type=None,
  68. tag_with_value=False, tag_value_delimitor=":",
  69. missing_fill=False, default_value=0, missing_fill_method=None,
  70. missing_impute=None, outlier_replace=False, outlier_replace_method=None,
  71. outlier_impute=None, outlier_replace_value=0,
  72. with_label=False, label_name='y',
  73. label_type='int', output_format='dense', need_run=True,
  74. with_match_id=False, match_id_name='', match_id_index=0):
  75. self.input_format = input_format
  76. self.delimitor = delimitor
  77. self.data_type = data_type
  78. self.exclusive_data_type = exclusive_data_type
  79. self.tag_with_value = tag_with_value
  80. self.tag_value_delimitor = tag_value_delimitor
  81. self.missing_fill = missing_fill
  82. self.default_value = default_value
  83. self.missing_fill_method = missing_fill_method
  84. self.missing_impute = missing_impute
  85. self.outlier_replace = outlier_replace
  86. self.outlier_replace_method = outlier_replace_method
  87. self.outlier_impute = outlier_impute
  88. self.outlier_replace_value = outlier_replace_value
  89. self.with_label = with_label
  90. self.label_name = label_name
  91. self.label_type = label_type
  92. self.output_format = output_format
  93. self.need_run = need_run
  94. self.with_match_id = with_match_id
  95. self.match_id_name = match_id_name
  96. self.match_id_index = match_id_index
  97. def check(self):
  98. descr = "data_transform param's"
  99. self.input_format = self.check_and_change_lower(self.input_format,
  100. ["dense", "sparse", "tag"],
  101. descr)
  102. self.output_format = self.check_and_change_lower(self.output_format,
  103. ["dense", "sparse"],
  104. descr)
  105. self.data_type = self.check_and_change_lower(self.data_type,
  106. ["int", "int64", "float", "float64", "str", "long"],
  107. descr)
  108. if type(self.missing_fill).__name__ != 'bool':
  109. raise ValueError("data_transform param's missing_fill {} not supported".format(self.missing_fill))
  110. if self.missing_fill_method is not None:
  111. self.missing_fill_method = self.check_and_change_lower(self.missing_fill_method,
  112. ['min', 'max', 'mean', 'designated'],
  113. descr)
  114. if self.outlier_replace_method is not None:
  115. self.outlier_replace_method = self.check_and_change_lower(self.outlier_replace_method,
  116. ['min', 'max', 'mean', 'designated'],
  117. descr)
  118. if type(self.with_label).__name__ != 'bool':
  119. raise ValueError("data_transform param's with_label {} not supported".format(self.with_label))
  120. if self.with_label:
  121. if not isinstance(self.label_name, str):
  122. raise ValueError("data_transform param's label_name {} should be str".format(self.label_name))
  123. self.label_type = self.check_and_change_lower(self.label_type,
  124. ["int", "int64", "float", "float64", "str", "long"],
  125. descr)
  126. if self.exclusive_data_type is not None and not isinstance(self.exclusive_data_type, dict):
  127. raise ValueError("exclusive_data_type is should be None or a dict")
  128. return True