Coverage for mlinsights/mlmodel/_extended_features_polynomial.py: 100%

55 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-28 08:46 +0100

1""" 

2@file 

3@brief Implements new features such as polynomial features. 

4""" 

5from itertools import combinations, chain 

6from itertools import combinations_with_replacement as combinations_w_r 

7 

8 

9def _transform_iall(degree, bias, XP, X, multiply, final): 

10 "Computes the polynomial features" 

11 if bias: 

12 XP[:, 0] = 1 

13 pos = 1 

14 else: 

15 pos = 0 

16 

17 n = X.shape[1] 

18 for d in range(0, degree): 

19 if d == 0: 

20 XP[:, pos:pos + n] = X 

21 index = list(range(pos, pos + n)) 

22 pos += n 

23 index.append(pos) 

24 else: 

25 new_index = [] 

26 end = index[-1] 

27 for i in range(0, n): 

28 a = index[i] 

29 new_index.append(pos) 

30 new_pos = pos + end - a 

31 multiply(XP[:, a:end], X[:, i:i + 1], 

32 XP[:, pos:new_pos]) 

33 pos = new_pos 

34 

35 new_index.append(pos) 

36 index = new_index 

37 

38 return final(XP) 

39 

40 

41def _transform_ionly(degree, bias, XP, X, multiply, final): 

42 "Computes the polynomial features" 

43 if bias: 

44 XP[:, 0] = 1 

45 pos = 1 

46 else: 

47 pos = 0 

48 

49 n = X.shape[1] 

50 for d in range(0, degree): 

51 if d == 0: 

52 XP[:, pos:pos + n] = X 

53 index = list(range(pos, pos + n)) 

54 pos += n 

55 index.append(pos) 

56 else: 

57 new_index = [] 

58 end = index[-1] 

59 for i in range(0, n): 

60 a = index[i] 

61 new_index.append(pos) 

62 dec = index[i + 1] - index[i] 

63 new_pos = pos + end - a - dec 

64 if new_pos <= pos: 

65 break 

66 multiply(XP[:, a + dec:end], X[:, i:i + 1], 

67 XP[:, pos:new_pos]) 

68 pos = new_pos 

69 

70 new_index.append(pos) 

71 index = new_index 

72 

73 return final(XP) 

74 

75 

76def _combinations_poly(n_features, degree, interaction_only, include_bias): 

77 "Computes all polynomial features combinations." 

78 comb = (combinations if interaction_only else combinations_w_r) 

79 start = int(not include_bias) 

80 return chain.from_iterable(comb(range(n_features), i) 

81 for i in range(start, degree + 1))