metrics.py 文件源码

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

项目:FreeDiscovery 作者: FreeDiscovery 项目源码 文件源码
def _scale_cosine_similarity(x, metric='cosine', inverse=False):
    """ Given a cosine similarity on L2 normalized data,
    appriximately convert it to Jaccard similarity, and/or
    normalize it to the [0, 1] interval

    Parameters
    ----------
    x : {float, ndarray}
      the cosine similarity value
    metric : str
      the conversion to apply one of ['cosine', 'jaccard']
    inverse : bool
      perform the inverse de-normalization operation
    """
    valid_metrics = ['cosine', 'jaccard', 'cosine_norm', 'jaccard_norm',
                     'cosine-positive']
    if metric not in valid_metrics:
        raise ValueError('metric {} not supported, must be in {}'
                         .format(metric, valid_metrics))
    if metric == 'cosine':
        return x
    elif metric == 'cosine-positive':
        if isinstance(x, (int, float)):
            return max(x, 0.0)
        else:
            return np.fmax(x, 0.0)

    if metric.startswith('jaccard'):
        if not inverse:
            x = cosine2jaccard_similarity(x)
        else:
            x = jaccard2cosine_similarity(x)

    if metric.endswith('norm'):
        x = _normalize_similarity(x, metric=metric.split('_')[0],
                                  inverse=inverse)

    return x
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号