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# -*- encoding: utf-8 -*- 

2# pylint: disable=E0203,E1101,C0111 

3""" 

4@file 

5@brief Runtime operator. 

6""" 

7import numpy 

8from ._op import OpRunUnaryNum, RuntimeTypeError 

9 

10 

11class Imputer(OpRunUnaryNum): 

12 

13 atts = {'imputed_value_floats': numpy.empty(0, dtype=numpy.float32), 

14 'imputed_value_int64s': numpy.empty(0, dtype=numpy.int64), 

15 'replaced_value_float': 0., 

16 'replaced_value_int64': 0} 

17 

18 def __init__(self, onnx_node, desc=None, **options): 

19 OpRunUnaryNum.__init__(self, onnx_node, desc=desc, 

20 expected_attributes=Imputer.atts, 

21 **options) 

22 if len(self.imputed_value_floats) > 0: 

23 self.values = self.imputed_value_floats 

24 self.replace = self.replaced_value_float 

25 elif len(self.imputed_value_int64s) > 0: 

26 self.values = self.imputed_value_int64s 

27 self.replace = self.replaced_value_int64 

28 else: 

29 raise ValueError("Missing are not defined.") # pragma: no cover 

30 

31 def _run(self, x): # pylint: disable=W0221 

32 if len(x.shape) != 2: 

33 raise RuntimeTypeError( 

34 "x must be a matrix but shape is {}".format(x.shape)) 

35 if self.values.shape[0] not in (x.shape[1], 1): 

36 raise RuntimeTypeError( # pragma: no cover 

37 "Dimension mismatch {} != {}".format( 

38 self.values.shape[0], x.shape[1])) 

39 x = x.copy() 

40 if numpy.isnan(self.replace): 

41 for i in range(0, x.shape[1]): 

42 val = self.values[min(i, self.values.shape[0] - 1)] 

43 x[numpy.isnan(x[:, i]), i] = val 

44 else: 

45 for i in range(0, x.shape[1]): 

46 val = self.values[min(i, self.values.shape[0] - 1)] 

47 x[x[:, i] == self.replace, i] = val 

48 

49 return (x, )