sparse_vector.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. ################################################################################
  19. #
  20. #
  21. ################################################################################
  22. # =============================================================================
  23. # Sparse Feature
  24. # =============================================================================
  25. class SparseVector(object):
  26. """
  27. Sparse storage data format of federatedml
  28. Parameters
  29. ----------
  30. sparse_vec : dict, record (indice, data) kv tuples
  31. shape : the real feature shape of data
  32. """
  33. def __init__(self, indices=None, data=None, shape=0):
  34. self.sparse_vec = dict(zip(indices, data))
  35. self.shape = shape
  36. def get_data(self, pos, default_val=None):
  37. return self.sparse_vec.get(pos, default_val)
  38. def count_non_zeros(self):
  39. return len(self.sparse_vec)
  40. def count_zeros(self):
  41. return self.shape - len(self.sparse_vec)
  42. def get_shape(self):
  43. return self.shape
  44. def set_shape(self, shape):
  45. self.shape = shape
  46. def get_all_data(self):
  47. for idx, data in self.sparse_vec.items():
  48. yield idx, data
  49. def get_sparse_vector(self):
  50. return self.sparse_vec
  51. def set_sparse_vector(self, sparse_vec):
  52. self.sparse_vec = sparse_vec
  53. @staticmethod
  54. def is_sparse_vector():
  55. return True