entropy.py 文件源码

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

项目:cluster_paraphrases 作者: acocos 项目源码 文件源码
def mutual_information(jointfreq, rowfreq=None, colfreq=None, unit='bit'):
    """
    Calculates the Mutual Information (I) of a joint frequency. The marginal
    frequencies can be given or are calculated from the joint frequency.

    Arguments:

        - jointfreq (``numpy.ndarray``) A normalized ``JointFreq`` instance or
          ``numpy.ndarray`` of rank-2, which is a joint probability distribution
          function of two random variables.
        - rowfreq (``numpy.ndarray``) [default: ``None``] A normalized marginal 
          probability distribution function for the variable along the axis =0. 
        - colfreq (``numpy.ndarray``) [default: ``None``] A normalized marginal 
          probability distribution function for the variable along the axis =1.
        - unit (``str``) [defualt: ``"bit"``] Unit of the returned information.   
    """
    log = get_base(unit)
    rowfreq = rowfreq or np.sum(jointfreq, axis=1)
    colfreq = colfreq or np.sum(jointfreq, axis=0)
    indfreq = np.dot(rowfreq[None].transpose(), colfreq[None])
    non_zero = jointfreq != 0.
    jntf = jointfreq[non_zero]
    indf = indfreq[non_zero]
    return np.sum(jntf * log(jntf/indf))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号