标题:NMI(标准化互信息)
只看楼主
super_lz
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2017-7-12
结帖率:90%
已结贴  问题点数:14 回复次数:2 
NMI(标准化互信息)
有谁知道 NMI(标准化互信息) 的C语言实现吗?
搜索更多相关主题的帖子: 标准 信息 C语言 
2018-01-08 14:21
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
得分:7 
http://


看不懂,不过他们的代码也没多少,你要是懂NMI的话应该也能写得出来吧。。。

φ(゜▽゜*)♪
2018-01-08 14:38
自学的数学
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:46
帖 子:967
专家分:4146
注 册:2017-11-15
得分:7 
package clusters;  
  
import *;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
 
public class NormalizedMutualInformation {  
    static String path = "/home/fhqplzj/IdeaProjects/Vein/src/main/resources/nmi_data";  
  
    static void loadData(List<List<Integer>> lists) {  
        try {  
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));  
            String line;  
            while ((line = bufferedReader.readLine()) != null) {  
                String[] data = line.split("\\s+");  
                ArrayList<Integer> integers = new ArrayList<>();  
                for (String s : data) {  
                    integers.add(Integer.parseInt(s));  
                }  
                lists.add(integers);  
            }  
            bufferedReader.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public static void main(String[] args) {  
        List<List<Integer>> lists = new ArrayList<>();  
        loadData(lists);  
        int K = lists.size();  
        int N = 0;  
        int[] clusters = new int[K];  
        for (int i = 0; i < K; i++) {  
            clusters[i] = lists.get(i).size();  
            N += clusters[i];  
        }  
        Map<Integer, Integer> map = new HashMap<>();  
        for (List<Integer> list : lists) {  
            for (Integer integer : list) {  
                map.put(integer, map.getOrDefault(integer, 0) + 1);  
            }  
        }  
        double clusterEntropy = 0;  
        for (int cluster : clusters) {  
            double tmp = 1.0 * cluster / N;  
            clusterEntropy -= (tmp * (Math.log(tmp) / Math.log(2)));  
        }  
        System.out.println("clusterEntropy = " + clusterEntropy);  
        double classEntropy = 0;  
        for (Integer integer : map.values()) {  
            double tmp = 1.0 * integer / N;  
            classEntropy -= (tmp * (Math.log(tmp) / Math.log(2)));  
        }  
        System.out.println("classEntropy = " + classEntropy);  
        double totalEntropy = 0;  
        Map<Integer, Integer> tmpMap = new HashMap<>();  
        for (int i = 0; i < K; i++) {  
            int wk = clusters[i];  
            tmpMap.clear();  
            for (Integer integer : lists.get(i)) {  
                tmpMap.put(integer, tmpMap.getOrDefault(integer, 0) + 1);  
            }  
            for (Map.Entry<Integer, Integer> entry : tmpMap.entrySet()) {  
                int cj = map.get(entry.getKey());  
                int value = entry.getValue();  
                totalEntropy += (1.0 * value / N * (Math.log(1.0 * N * value / (wk * cj)) / Math.log(2)));  
            }  
        }  
        System.out.println("totalEntropy = " + totalEntropy);  
        double nmi = 2 * totalEntropy / (clusterEntropy + classEntropy);  
        System.out.println("nmi = " + nmi);  
    }  
}  
2018-01-08 17:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-484068-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.302684 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved