math.py 文件源码

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

项目:2020plus 作者: KarchinLab 项目源码 文件源码
def kl_divergence(p, q):
    """Compute the Kullback-Leibler (KL) divergence for discrete distributions.

    Parameters
    ----------
    p : np.array
        "Ideal"/"true" Probability distribution
    q : np.array
        Approximation of probability distribution p

    Returns
    -------
    kl : float
        KL divergence of approximating p with the distribution q
    """
    # make sure numpy arrays are floats
    p = p.astype(float)
    q = q.astype(float)

    # compute kl divergence
    kl = np.sum(np.where(p!=0, p*np.log2(p/q), 0))
    return kl
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号