You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

my_label.py 1.3KB

3 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import abc
  2. class MyBaseLabel(abc.ABC):
  3. @abc.abstractmethod
  4. def _int2str_item(self, int_inp):
  5. pass
  6. @abc.abstractmethod
  7. def _str2int_item(self, str_inp):
  8. pass
  9. def int2str(self, _input):
  10. if isinstance(_input, list):
  11. return [self._int2str_item(item) for item in _input]
  12. return self._int2str_item(_input)
  13. def str2int(self, _input):
  14. if isinstance(_input, list):
  15. return [self._str2int_item(item) for item in _input]
  16. return self._str2int_item(_input)
  17. class MyDummyLabel(MyBaseLabel):
  18. def _int2str_item(self, int_inp):
  19. return int_inp
  20. def _str2int_item(self, str_inp):
  21. return str_inp
  22. class MyClassLabel(MyBaseLabel):
  23. def __init__(self, names):
  24. self.names = names
  25. def _int2str_item(self, int_inp):
  26. return self.names[int_inp]
  27. def _str2int_item(self, str_inp):
  28. if str_inp not in self.names:
  29. return None
  30. return self.names.index(str_inp)
  31. class MyRegresionLabel(MyBaseLabel):
  32. def _int2str_item(self, int_inp):
  33. return "%.1f" % round(int_inp, 1)
  34. def _str2int_item(self, str_inp):
  35. try:
  36. return float(str_inp)
  37. except ValueError as ex:
  38. return None