data_preparation.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:keras-utilities 作者: cbaziotis 项目源码 文件源码
def get_class_weights2(y, smooth_factor=0):
    """
    Returns the normalized 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()}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号