2
2
Useful utilities for ensemble
3
3
"""
4
4
5
- from typing import Any , List
5
+ from typing import Any , List , Optional
6
6
import tensorflow as tf
7
7
import keras
8
8
import numpy as np
@@ -94,7 +94,19 @@ def __voting_weight_single(self, array: NDArray) -> float:
94
94
result = array * weight
95
95
return float (np .sum (result ))
96
96
97
- def predict (self , input_data : NDArray , voting_policy : str = "None" ) -> NDArray :
97
+ def __voting_most (self , array : NDArray ) -> NDArray :
98
+ return_result = []
99
+ for items in array :
100
+ items_ = items > 0.5
101
+ result = 0
102
+ for i in items_ :
103
+ result += 1 if i else - 1
104
+ return_result .append (1 if result > 0 else 0 )
105
+ return np .array (return_result )
106
+
107
+ def predict (
108
+ self , input_data : NDArray , voting_policy : Optional [str ] = "None"
109
+ ) -> NDArray :
98
110
"""
99
111
Input data is expected to be a 2D array that the first layer is different input data (into the trained models)
100
112
"""
@@ -105,9 +117,11 @@ def predict(self, input_data: NDArray, voting_policy: str = "None") -> NDArray:
105
117
self .predictions = np .transpose (np .array (predictions ))
106
118
if voting_policy == "weight" :
107
119
return self .__voting_weight (self .predictions )
120
+ elif voting_policy == "most" :
121
+ return self .__voting_most (self .predictions )
108
122
elif voting_policy == "average" :
109
123
return self .__voting_average (self .predictions )
110
- elif voting_policy == " None" :
124
+ elif voting_policy is None :
111
125
return self .predictions
112
126
else :
113
127
raise ValueError ("voting_policy must be none, weight, most, or average" )
0 commit comments