instance.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import copy
  23. class Instance(object):
  24. """
  25. Instance object use in all algorithm module
  26. Parameters
  27. ----------
  28. inst_id : int, the id of the instance, reserved fields in this version
  29. weight: float, the weight of the instance
  30. feature : object, ndarray or SparseVector Object in this version
  31. label: None of float, data label
  32. """
  33. def __init__(self, inst_id=None, weight=None, features=None, label=None):
  34. self.inst_id = inst_id
  35. self.weight = weight
  36. self.features = features
  37. self.label = label
  38. def set_weight(self, weight=1.0):
  39. self.weight = weight
  40. def set_label(self, label=1):
  41. self.label = label
  42. def set_feature(self, features):
  43. self.features = features
  44. def copy(self, exclusive_attr=None):
  45. keywords = {"inst_id", "weight", "features", "label"}
  46. if exclusive_attr:
  47. keywords -= set(exclusive_attr)
  48. copy_obj = Instance()
  49. for key in keywords:
  50. if key in exclusive_attr:
  51. continue
  52. attr = getattr(self, key)
  53. setattr(copy_obj, key, attr)
  54. return copy_obj
  55. @property
  56. def with_inst_id(self):
  57. return self.inst_id is not None
  58. @staticmethod
  59. def is_instance():
  60. return True