Created
January 23, 2017 11:55
-
-
Save cbaziotis/f8ffa9922081493418287ee14830e6e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_class_weights(y, smooth_factor=0): | |
""" | |
Returns the weights for each class based on the frequencies of the samples | |
:param smooth_factor: factor that smooths extremely uneven weights | |
:param y: list of true labels (the labels must be hashable) | |
:return: dictionary with the weight for each class | |
""" | |
counter = Counter(y) | |
if smooth_factor > 0: | |
p = max(counter.values()) * smooth_factor | |
for k in counter.keys(): | |
counter[k] += p | |
majority = max(counter.values()) | |
return {cls: float(majority / count) for cls, count in counter.items()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment