首页 > 编程语言 > 详细

转:标准化互信息NMI计算步骤及其Python实现

时间:2020-07-31 14:13:42      阅读:336      评论:0      收藏:0      [点我收藏+]

本文转自:标准化互信息NMI计算步骤及其Python实现

标准化互信息NMI计算步骤

技术分享图片

技术分享图片

Python 实现

代码:

‘‘‘  利用Python实现NMI计算‘‘‘
import math
import numpy as np
from sklearn import metrics
def NMI(A,B):
    # 样本点数
    total = len(A)
    A_ids = set(A)
    B_ids = set(B)
    # 互信息计算
    MI = 0
    eps = 1.4e-45
    for idA in A_ids:
        for idB in B_ids:
            idAOccur = np.where(A==idA)    # 输出满足条件的元素的下标
            idBOccur = np.where(B==idB)
            idABOccur = np.intersect1d(idAOccur,idBOccur)   # Find the intersection of two arrays.
            px = 1.0*len(idAOccur[0])/total
            py = 1.0*len(idBOccur[0])/total
            pxy = 1.0*len(idABOccur)/total
            MI = MI + pxy*math.log(pxy/(px*py)+eps,2)
    # 标准化互信息
    Hx = 0
    for idA in A_ids:
        idAOccurCount = 1.0*len(np.where(A==idA)[0])
        Hx = Hx - (idAOccurCount/total)*math.log(idAOccurCount/total+eps,2)
        Hy = 0
    for idB in B_ids:
        idBOccurCount = 1.0*len(np.where(B==idB)[0])
        Hy = Hy - (idBOccurCount/total)*math.log(idBOccurCount/total+eps,2)
    MIhat = 2.0*MI/(Hx+Hy)
    return MIhat


if __name__ == ‘__main__‘:
    A = np.array([1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3])
    B = np.array([1,2,1,1,1,1,1,2,2,2,2,3,1,1,3,3,3])
    print(NMI(A,B))
    print(metrics.normalized_mutual_info_score(A,B))   # 直接调用sklearn中的函数

运行结果:

0.3645617718571898
0.3646247961942429

 

转:标准化互信息NMI计算步骤及其Python实现

原文:https://www.cnblogs.com/picassooo/p/13409146.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!