Skip to content

fairness aware

FAIRNESS_AWARE

Source code in engines/contentFilterEngine/fairness_explainability/fairness_aware.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class FAIRNESS_AWARE:
    def __init__(self):
        """
        Initialize the fairness-aware module.

        Attributes:
            fairness_metrics (dict): A dictionary to store calculated fairness metrics, 
            such as the distribution of recommendations across different user demographics.
        """
        self.fairness_metrics = {}

    def evaluate_fairness(self, recommendations: Dict[int, List[int]], user_attributes: pd.DataFrame) -> Dict[str, float]:
        """
        Evaluate the fairness of the recommendations across different user groups.

        Parameters:
            recommendations (dict): A dictionary mapping user IDs to lists of recommended item IDs.
            user_attributes (pd.DataFrame): A DataFrame containing user demographic information, 
            such as age, gender, and other relevant attributes.

        Returns:
            dict: A dictionary of fairness metrics, providing insights into how recommendations 
            are distributed across different user groups. For example, it may include the 
            distribution of recommendations by gender or age group.
        """
        # Example: Calculate the distribution of recommendations across gender
        gender_distribution = user_attributes['gender'].value_counts(normalize=True).to_dict()
        self.fairness_metrics['gender_distribution'] = gender_distribution
        return self.fairness_metrics

    def ensure_fairness(self, recommendations: Dict[int, List[int]], user_attributes: pd.DataFrame) -> Dict[int, List[int]]:
        """
        Adjust recommendations to ensure fairness across user groups.

        Parameters:
            recommendations (dict): A dictionary mapping user IDs to lists of recommended item IDs.
            user_attributes (pd.DataFrame): A DataFrame containing user demographic information, 
            such as age, gender, and other relevant attributes.

        Returns:
            dict: Adjusted recommendations ensuring fairness, potentially modifying the original 
            recommendations to achieve a more balanced distribution across user groups.
        """
        # Placeholder: Implement logic to adjust recommendations for fairness
        return recommendations

__init__()

Initialize the fairness-aware module.

Attributes:

Name Type Description
fairness_metrics dict

A dictionary to store calculated fairness metrics,

Source code in engines/contentFilterEngine/fairness_explainability/fairness_aware.py
 6
 7
 8
 9
10
11
12
13
14
def __init__(self):
    """
    Initialize the fairness-aware module.

    Attributes:
        fairness_metrics (dict): A dictionary to store calculated fairness metrics, 
        such as the distribution of recommendations across different user demographics.
    """
    self.fairness_metrics = {}

ensure_fairness(recommendations, user_attributes)

Adjust recommendations to ensure fairness across user groups.

Parameters:

Name Type Description Default
recommendations dict

A dictionary mapping user IDs to lists of recommended item IDs.

required
user_attributes DataFrame

A DataFrame containing user demographic information,

required

Returns:

Name Type Description
dict Dict[int, List[int]]

Adjusted recommendations ensuring fairness, potentially modifying the original

Dict[int, List[int]]

recommendations to achieve a more balanced distribution across user groups.

Source code in engines/contentFilterEngine/fairness_explainability/fairness_aware.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def ensure_fairness(self, recommendations: Dict[int, List[int]], user_attributes: pd.DataFrame) -> Dict[int, List[int]]:
    """
    Adjust recommendations to ensure fairness across user groups.

    Parameters:
        recommendations (dict): A dictionary mapping user IDs to lists of recommended item IDs.
        user_attributes (pd.DataFrame): A DataFrame containing user demographic information, 
        such as age, gender, and other relevant attributes.

    Returns:
        dict: Adjusted recommendations ensuring fairness, potentially modifying the original 
        recommendations to achieve a more balanced distribution across user groups.
    """
    # Placeholder: Implement logic to adjust recommendations for fairness
    return recommendations

evaluate_fairness(recommendations, user_attributes)

Evaluate the fairness of the recommendations across different user groups.

Parameters:

Name Type Description Default
recommendations dict

A dictionary mapping user IDs to lists of recommended item IDs.

required
user_attributes DataFrame

A DataFrame containing user demographic information,

required

Returns:

Name Type Description
dict Dict[str, float]

A dictionary of fairness metrics, providing insights into how recommendations

Dict[str, float]

are distributed across different user groups. For example, it may include the

Dict[str, float]

distribution of recommendations by gender or age group.

Source code in engines/contentFilterEngine/fairness_explainability/fairness_aware.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def evaluate_fairness(self, recommendations: Dict[int, List[int]], user_attributes: pd.DataFrame) -> Dict[str, float]:
    """
    Evaluate the fairness of the recommendations across different user groups.

    Parameters:
        recommendations (dict): A dictionary mapping user IDs to lists of recommended item IDs.
        user_attributes (pd.DataFrame): A DataFrame containing user demographic information, 
        such as age, gender, and other relevant attributes.

    Returns:
        dict: A dictionary of fairness metrics, providing insights into how recommendations 
        are distributed across different user groups. For example, it may include the 
        distribution of recommendations by gender or age group.
    """
    # Example: Calculate the distribution of recommendations across gender
    gender_distribution = user_attributes['gender'].value_counts(normalize=True).to_dict()
    self.fairness_metrics['gender_distribution'] = gender_distribution
    return self.fairness_metrics