binning_model_migrate.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from federatedml.protobuf import parse_pb_buffer
  17. def extract_woe_array_dict(model_param_dict, host_idx=0):
  18. if len(model_param_dict.get("multiClassResults", {}).get("labels", [])) > 2:
  19. raise ValueError(f"Does not support transforming model trained on multi-label data. Please check.")
  20. host_result = model_param_dict.get("hostResults", [])[host_idx].get("binningResult", {})
  21. woe_array_dict = {}
  22. for col, res in host_result.items():
  23. woe_array_dict[col] = {"woeArray": res.get("woeArray", [])}
  24. return woe_array_dict
  25. def merge_woe_array_dict(pb_name, model_param_pb, model_param_dict, woe_array_dict):
  26. model_param_pb = parse_pb_buffer(pb_name, model_param_pb)
  27. header, anonymous_header = list(model_param_pb.header), list(model_param_pb.header_anonymous)
  28. if len(header) != len(anonymous_header):
  29. raise ValueError(
  30. "Given header length and anonymous header length in model param do not match. "
  31. "Please check!"
  32. )
  33. anonymous_col_name_dict = dict(zip(header, anonymous_header))
  34. for col_name in model_param_pb.binning_result.binning_result:
  35. try:
  36. woe_array = woe_array_dict[anonymous_col_name_dict[col_name]]["woeArray"]
  37. except KeyError:
  38. continue
  39. model_param_pb.binning_result.binning_result[col_name].woe_array[:] = woe_array
  40. model_param_dict["binningResult"]["binningResult"][col_name]["woeArray"] = woe_array
  41. for col_name in model_param_pb.multi_class_result.results[0].binning_result:
  42. try:
  43. woe_array = woe_array_dict[anonymous_col_name_dict[col_name]]["woeArray"]
  44. except KeyError:
  45. continue
  46. model_param_pb.multi_class_result.results[0].binning_result[col_name].woe_array[:] = woe_array
  47. model_param_dict["multiClassResult"]["results"][0]["binningResult"][col_name]["woeArray"] = woe_array
  48. return model_param_pb.SerializeToString(), model_param_dict
  49. def set_model_meta(model_meta_dict):
  50. model_meta_dict.get("transformParam", {})["transformType"] = "woe"
  51. return model_meta_dict