Skip to content

Sorter

Sorts the elements based on their average shapley values or in ascending order by calling

df.sort_index(axis=1)

Args: ascending (bool): I noticed although in the DataFrame itself the Shapley values are at their right places, but the order of elements are shuffled (probably during the calculation). This causes headache and is potentially dangerous if you're using a list of indices as elements that you'll translate to np or normal lists within your game. so assuming the elements were in ascending order like np.arange or range, this will save you from the pain.

shapley_table (pd.DataFrame):
    Unsorted shapley table.

Returns:

Type Description
DataFrame

sorted shapley table.

Source code in msapy/utils.py
@typechecked
def sorter(shapley_table: pd.DataFrame, ascending: Optional[bool] = False) -> pd.DataFrame:
    """
    Sorts the elements based on their average shapley values or in ascending order by calling:
        `df.sort_index(axis=1)`
    Args:
        ascending (bool):
            I noticed although in the DataFrame itself the Shapley values are at their right places, but the order of
            elements are shuffled (probably during the calculation). This causes headache and is potentially dangerous
            if you're using a list of indices as elements that you'll translate to np or normal lists within your game.
            so assuming the elements were in ascending order like np.arange or range, this will save you from the pain.

        shapley_table (pd.DataFrame):
            Unsorted shapley table.

    Returns:
         (pd.DataFrame): sorted shapley table.
    """
    if ascending:
        return shapley_table.sort_index(axis=1)
    else:
        return shapley_table.reindex(shapley_table.mean().sort_values().index, axis=1)