Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Common class for classifiers supporting strings. 

4""" 

5import numpy 

6 

7 

8class _ClassifierCommon: 

9 """ 

10 Labels strings are not natively implemented in C++ runtime. 

11 The class stores the strings labels, replaces them by 

12 integer, calls the C++ codes and then replaces them by strings. 

13 """ 

14 

15 def _post_process_label_attributes(self): 

16 """ 

17 Replaces string labels by int64 labels. 

18 It creates attributes *_classlabels_int64s_string*. 

19 """ 

20 name_int = 'classlabels_int64s' if hasattr( 

21 self, 'classlabels_int64s') else 'classlabels_ints' 

22 if (hasattr(self, 'classlabels_strings') and 

23 len(self.classlabels_strings) > 0): # pylint: disable=E0203 

24 if hasattr(self, name_int) and len(getattr(self, name_int)) != 0: 

25 raise RuntimeError( # pragma: no cover 

26 "'%s' must be empty if " 

27 "'classlabels_strings' is not." % name_int) 

28 setattr(self, name_int, numpy.arange(len(self.classlabels_strings), # pylint: disable=E0203 

29 dtype=numpy.int64)) 

30 self._classlabels_int64s_string = self.classlabels_strings # pylint: disable=E0203 

31 self.classlabels_strings = numpy.empty( 

32 shape=(0, ), dtype=numpy.str_) 

33 else: 

34 self._classlabels_int64s_string = None 

35 

36 def _post_process_predicted_label(self, label, scores): 

37 """ 

38 Replaces int64 predicted labels by the corresponding 

39 strings. 

40 """ 

41 if self._classlabels_int64s_string is not None: 

42 label = numpy.array( 

43 [self._classlabels_int64s_string[i] for i in label]) 

44 return label, scores