def test_gei_alpha_more_than_one(self): np.random.seed(1) # test bounds metric = BinaryFairnessMetrics.GeneralizedEntropyIndex() my_dict = {} for _ in range(1000): random = np.random.choice([0, 1], 10) if str(random) in my_dict: continue else: my_dict[str(random)] = (random[:5], random[5:]) with self.assertWarns( RuntimeWarning): # division by 0 in certain instances. # enumerate through all the combinations alpha = 3 results = [] vals = [] for y_true, y_pred in my_dict.values(): vals.append([y_true, y_pred]) results.append(metric.get_score(y_true, y_pred, alpha=alpha)) assert min(results) == 0 assert max(results) == (np.power(5, alpha - 1) - 1) / (alpha * (alpha - 1))
def test_gei_normal_invalid(self): # Metric metric = BinaryFairnessMetrics.GeneralizedEntropyIndex() y_true = np.array([0, 1, 1, 0, 1, 1, 1, 0, 1, -1]) y_pred = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) with self.assertRaises(ValueError): metric.get_score(y_true, y_pred)
def test_gei_normal_list(self): # Metric metric = BinaryFairnessMetrics.GeneralizedEntropyIndex() y_true = [0, 1, 1, 0, 1, 1, 1, 0, 1, 0] y_pred = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] score = metric.get_score(y_true, y_pred) assert isinstance(score, float) assert np.isclose(score, 0.302, atol=0.01)
def test_gei_normal_df(self): my_df = pd.DataFrame.from_dict({ 'y_true': [0, 1, 1, 0, 1, 1, 1, 0, 1, 0], 'y_pred': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] }) # Metric metric = BinaryFairnessMetrics.GeneralizedEntropyIndex() # Score score = metric.get_score(my_df['y_true'], my_df['y_pred']) assert isinstance(score, float) assert np.isclose(score, 0.302, atol=0.01)