cross_validate.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. import numpy as np
  17. from federatedml.util import LOGGER
  18. from federatedml.util import consts
  19. class BaseCrossValidator(object):
  20. def __init__(self):
  21. self.mode = None
  22. self.role = None
  23. def split(self, data_inst):
  24. pass
  25. def display_cv_result(self, cv_results):
  26. LOGGER.debug("cv_result: {}".format(cv_results))
  27. if self.role == consts.GUEST or (self.role == consts.HOST and self.mode == consts.HOMO):
  28. format_cv_result = {}
  29. for eval_result in cv_results:
  30. for eval_name, eval_r in eval_result.items():
  31. if not isinstance(eval_r, list):
  32. if eval_name not in format_cv_result:
  33. format_cv_result[eval_name] = []
  34. format_cv_result[eval_name].append(eval_r)
  35. else:
  36. for e_r in eval_r:
  37. e_name = "{}_thres_{}".format(eval_name, e_r[0])
  38. if e_name not in format_cv_result:
  39. format_cv_result[e_name] = []
  40. format_cv_result[e_name].append(e_r[1])
  41. for eval_name, eva_result_list in format_cv_result.items():
  42. mean_value = np.around(np.mean(eva_result_list), 4)
  43. std_value = np.around(np.std(eva_result_list), 4)
  44. LOGGER.info("{},evaluate name: {}, mean: {}, std: {}".format(self.role,
  45. eval_name, mean_value, std_value))