dataio_param.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 DataIOParam(BaseParam):
  20. """
  21. Define dataio parameters that used in federated ml.
  22. This module is not supported to use in training task since Fate-v1.9.0, use data transform instead.
  23. Parameters
  24. ----------
  25. input_format : str, accepted 'dense','sparse' 'tag' only in this version. default: 'dense'.
  26. please have a look at this tutorial at "DataIO" section of federatedml/util/README.md.
  27. Formally,
  28. dense input format data should be set to "dense",
  29. svm-light input format data should be set to "sparse",
  30. tag or tag:value input format data should be set to "tag".
  31. delimitor : str, the delimitor of data input, default: ','
  32. data_type : str, the data type of data input, accepted 'float','float64','int','int64','str','long'
  33. "default: "float64"
  34. exclusive_data_type : dict, the key of dict is col_name, the value is data_type, use to specified special data type
  35. of some features.
  36. tag_with_value: bool, use if input_format is 'tag', if tag_with_value is True,
  37. input column data format should be tag[delimitor]value, otherwise is tag only
  38. tag_value_delimitor: str, use if input_format is 'tag' and 'tag_with_value' is True,
  39. delimitor of tag[delimitor]value column value.
  40. missing_fill : bool, need to fill missing value or not, accepted only True/False, default: True
  41. default_value : None or single object type or list, the value to replace missing value.
  42. if None, it will use default value define in federatedml/feature/imputer.py,
  43. if single object, will fill missing value with this object,
  44. if list, it's length should be the sample of input data' feature dimension,
  45. means that if some column happens to have missing values, it will replace it
  46. the value by element in the identical position of this list.
  47. default: None
  48. missing_fill_method: None or str, the method to replace missing value, should be one of [None, 'min', 'max', 'mean', 'designated'], default: None
  49. 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
  50. outlier_replace: bool, need to replace outlier value or not, accepted only True/False, default: True
  51. outlier_replace_method: None or str, the method to replace missing value, should be one of [None, 'min', 'max', 'mean', 'designated'], default: None
  52. outlier_impute: None or list, element of list can be any type, which values should be regard as missing value, default: None
  53. outlier_replace_value: None or single object type or list, the value to replace outlier.
  54. if None, it will use default value define in federatedml/feature/imputer.py,
  55. if single object, will replace outlier with this object,
  56. if list, it's length should be the sample of input data' feature dimension,
  57. means that if some column happens to have outliers, it will replace it
  58. the value by element in the identical position of this list.
  59. default: None
  60. with_label : bool, True if input data consist of label, False otherwise. default: 'false'
  61. label_name : str, column_name of the column where label locates, only use in dense-inputformat. default: 'y'
  62. label_type : object, accepted 'int','int64','float','float64','long','str' only,
  63. use when with_label is True. default: 'false'
  64. output_format : str, accepted 'dense','sparse' only in this version. default: 'dense'
  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. self.input_format = input_format
  75. self.delimitor = delimitor
  76. self.data_type = data_type
  77. self.exclusive_data_type = exclusive_data_type
  78. self.tag_with_value = tag_with_value
  79. self.tag_value_delimitor = tag_value_delimitor
  80. self.missing_fill = missing_fill
  81. self.default_value = default_value
  82. self.missing_fill_method = missing_fill_method
  83. self.missing_impute = missing_impute
  84. self.outlier_replace = outlier_replace
  85. self.outlier_replace_method = outlier_replace_method
  86. self.outlier_impute = outlier_impute
  87. self.outlier_replace_value = outlier_replace_value
  88. self.with_label = with_label
  89. self.label_name = label_name
  90. self.label_type = label_type
  91. self.output_format = output_format
  92. self.need_run = need_run
  93. def check(self):
  94. descr = "dataio param's"
  95. self.input_format = self.check_and_change_lower(self.input_format,
  96. ["dense", "sparse", "tag"],
  97. descr)
  98. self.output_format = self.check_and_change_lower(self.output_format,
  99. ["dense", "sparse"],
  100. descr)
  101. self.data_type = self.check_and_change_lower(self.data_type,
  102. ["int", "int64", "float", "float64", "str", "long"],
  103. descr)
  104. if type(self.missing_fill).__name__ != 'bool':
  105. raise ValueError("dataio param's missing_fill {} not supported".format(self.missing_fill))
  106. if self.missing_fill_method is not None:
  107. self.missing_fill_method = self.check_and_change_lower(self.missing_fill_method,
  108. ['min', 'max', 'mean', 'designated'],
  109. descr)
  110. if self.outlier_replace_method is not None:
  111. self.outlier_replace_method = self.check_and_change_lower(self.outlier_replace_method,
  112. ['min', 'max', 'mean', 'designated'],
  113. descr)
  114. if type(self.with_label).__name__ != 'bool':
  115. raise ValueError("dataio param's with_label {} not supported".format(self.with_label))
  116. if self.with_label:
  117. if not isinstance(self.label_name, str):
  118. raise ValueError("dataio param's label_name {} should be str".format(self.label_name))
  119. self.label_type = self.check_and_change_lower(self.label_type,
  120. ["int", "int64", "float", "float64", "str", "long"],
  121. descr)
  122. if self.exclusive_data_type is not None and not isinstance(self.exclusive_data_type, dict):
  123. raise ValueError("exclusive_data_type is should be None or a dict")
  124. return True