pearson_adapter.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. import numpy as np
  16. from federatedml.feature.feature_selection.model_adapter import isometric_model
  17. from federatedml.feature.feature_selection.model_adapter.adapter_base import BaseAdapter
  18. from federatedml.util import consts
  19. class PearsonMetricInfo(object):
  20. def __init__(
  21. self, local_corr, col_names, corr=None, host_col_names=None, parties=None
  22. ):
  23. self.local_corr = local_corr
  24. self.col_names = col_names
  25. self.corr = corr
  26. self.host_col_names = host_col_names
  27. self.parties = parties
  28. @property
  29. def host_party_id(self):
  30. assert isinstance(self.parties, list) and len(self.parties) == 2
  31. return self.parties[1][1]
  32. class PearsonAdapter(BaseAdapter):
  33. def convert(self, model_meta, model_param):
  34. col_names = list(model_param.names)
  35. result = isometric_model.IsometricModel()
  36. # corr
  37. local_corr = np.array(model_param.local_corr).reshape(
  38. model_param.shape, model_param.shape
  39. )
  40. if model_param.corr:
  41. corr = np.array(model_param.corr).reshape(*model_param.shapes)
  42. host_names = list(list(model_param.all_names)[1].names)
  43. parties = list(model_param.parties)
  44. else:
  45. corr = None
  46. host_names = None
  47. parties = None
  48. pearson_metric = PearsonMetricInfo(
  49. local_corr=local_corr,
  50. col_names=col_names,
  51. corr=corr,
  52. host_col_names=host_names,
  53. parties=parties,
  54. )
  55. result.add_metric_value(metric_name=consts.PEARSON, metric_info=pearson_metric)
  56. # local vif
  57. local_vif = model_param.local_vif
  58. if local_vif:
  59. single_info = isometric_model.SingleMetricInfo(
  60. values=local_vif, col_names=col_names
  61. )
  62. result.add_metric_value(metric_name=consts.VIF, metric_info=single_info)
  63. return result