Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _obtain_conditionings(self, dataset):
"""Obtain the necessary conditioning boolean vectors to compute
instance level weights.
"""
# conditioning
priv_cond = utils.compute_boolean_conditioning_vector(
dataset.protected_attributes,
dataset.protected_attribute_names,
condition=self.privileged_groups)
unpriv_cond = utils.compute_boolean_conditioning_vector(
dataset.protected_attributes,
dataset.protected_attribute_names,
condition=self.unprivileged_groups)
fav_cond = dataset.labels.ravel() == dataset.favorable_label
unfav_cond = dataset.labels.ravel() == dataset.unfavorable_label
# combination of label and privileged/unpriv. groups
cond_p_fav = np.logical_and(fav_cond, priv_cond)
cond_p_unfav = np.logical_and(unfav_cond, priv_cond)
cond_up_fav = np.logical_and(fav_cond, unpriv_cond)
cond_up_unfav = np.logical_and(unfav_cond, unpriv_cond)
def _obtain_conditionings(self, dataset):
"""Obtain the necessary conditioning boolean vectors to compute
instance level weights.
"""
# conditioning
priv_cond = utils.compute_boolean_conditioning_vector(
dataset.protected_attributes,
dataset.protected_attribute_names,
condition=self.privileged_groups)
unpriv_cond = utils.compute_boolean_conditioning_vector(
dataset.protected_attributes,
dataset.protected_attribute_names,
condition=self.unprivileged_groups)
fav_cond = dataset.labels.ravel() == dataset.favorable_label
unfav_cond = dataset.labels.ravel() == dataset.unfavorable_label
# combination of label and privileged/unpriv. groups
cond_p_fav = np.logical_and(fav_cond, priv_cond)
cond_p_unfav = np.logical_and(unfav_cond, priv_cond)
cond_up_fav = np.logical_and(fav_cond, unpriv_cond)
cond_up_unfav = np.logical_and(unfav_cond, unpriv_cond)
return (priv_cond, unpriv_cond, fav_cond, unfav_cond,
cond_p_fav, cond_p_unfav, cond_up_fav, cond_up_unfav)
groups.
alpha (int): See :meth:`generalized_entropy_index`.
References:
.. [2] T. Speicher, H. Heidari, N. Grgic-Hlaca, K. P. Gummadi, A. Singla, A. Weller, and M. B. Zafar,
"A Unified Approach to Quantifying Algorithmic Unfairness: Measuring Individual and Group Unfairness via Inequality Indices,"
ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 2018.
"""
b = np.zeros(self.dataset.labels.size, dtype=np.float64)
for group in groups:
classified_group = utils.compute_boolean_conditioning_vector(
self.classified_dataset.protected_attributes,
self.classified_dataset.protected_attribute_names,
condition=group)
true_group = utils.compute_boolean_conditioning_vector(
self.dataset.protected_attributes,
self.dataset.protected_attribute_names,
condition=group)
# ignore if there are no members of this group present
if not np.any(true_group):
continue
y_pred = self.classified_dataset.labels[classified_group].ravel()
y_true = self.dataset.labels[true_group].ravel()
y_pred = (y_pred == self.classified_dataset.favorable_label).astype(
np.float64)
y_true = (y_true == self.dataset.favorable_label).astype(np.float64)
b[true_group] = np.mean(1 + y_pred - y_true)
if alpha == 1:
return np.mean(np.log((b / np.mean(b))**b) / np.mean(b))
elif alpha == 0:
A_ub = np.array([[ 1, 0, 0, 0],
[-1, 0, 0, 0],
[ 0, 1, 0, 0],
[ 0, -1, 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, -1, 0],
[ 0, 0, 0, 1],
[ 0, 0, 0, -1]], dtype=np.float64)
b_ub = np.array([1, 0, 1, 0, 1, 0, 1, 0], dtype=np.float64)
# Create boolean conditioning vectors for protected groups
cond_vec_priv = utils.compute_boolean_conditioning_vector(
dataset_pred.protected_attributes,
dataset_pred.protected_attribute_names,
self.privileged_groups)
cond_vec_unpriv = utils.compute_boolean_conditioning_vector(
dataset_pred.protected_attributes,
dataset_pred.protected_attribute_names,
self.unprivileged_groups)
sconst = np.ravel(
dataset_pred.labels[cond_vec_priv] == dataset_pred.favorable_label)
sflip = np.ravel(
dataset_pred.labels[cond_vec_priv] == dataset_pred.unfavorable_label)
oconst = np.ravel(
dataset_pred.labels[cond_vec_unpriv] == dataset_pred.favorable_label)
oflip = np.ravel(
dataset_pred.labels[cond_vec_unpriv] == dataset_pred.unfavorable_label)
y_true = dataset_true.labels.ravel()
sm_tn = np.logical_and(sflip,