此文不对理论做相关阐述,仅涉及代码实现:

1.熵计算公式:

P为正例,Q为反例

Entropy(S)   = -PLog2(P) - QLog2(Q);

2.信息增量计算:

Gain(S,Sv) = Entropy(S) - (|Sv|/|S|)ΣEntropy(Sv);

举例:

转化数据输入:

  1. 5 14
  2. Outlook Sunny Sunny Overcast Rain Rain Rain Overcast Sunny Sunny Rain Sunny Overcast Overcast Rain
  3. Temperature Hot Hot Hot Mild Cool Cool Cool Mild Cool Mild Mild Mild Hot Mild
  4. Humidity High High High High Normal Normal Normal High Normal Normal Normal High Normal High
  5. Wind Weak Strong Weak Weak Weak Strong Strong Weak Weak Weak Strong Strong Weak Strong
  6. PlayTennis No No Yes Yes Yes No Yes No Yes Yes Yes Yes Yes No
  7. Outlook Temperature Humidity Wind PlayTennis
  1. package com.qunar.data.tree;
  2.  
  3. /**
  4. * *********************************************************
  5. * <p/>
  6. * Author: XiJun.Gong
  7. * Date: 2016-09-02 15:28
  8. * Version: default 1.0.0
  9. * Class description:
  10. * <p>统计该类型出现的次数</p>
  11. * <p/>
  12. * *********************************************************
  13. */
  14. public class CountMap<T> {
  15.  
  16. private T key; //类型
  17. private int value; //出现的次数
  18.  
  19. public CountMap() {
  20. this(null, 0);
  21. }
  22.  
  23. public CountMap(T key, int value) {
  24. this.key = key;
  25. this.value = value;
  26. }
  27.  
  28. public T getKey() {
  29. return key;
  30. }
  31.  
  32. public void setKey(T key) {
  33. this.key = key;
  34. }
  35.  
  36. public int getValue() {
  37. return value;
  38. }
  39.  
  40. public void setValue(int value) {
  41. this.value = value;
  42. }
  43. }
  1. package com.qunar.data.tree;
  2.  
  3. import com.google.common.collect.ArrayListMultimap;
  4. import com.google.common.collect.Maps;
  5. import com.google.common.collect.Multimap;
  6. import com.google.common.collect.Sets;
  7.  
  8. import java.util.*;
  9.  
  10. /**
  11. * *********************************************************
  12. * <p/>
  13. * Author: XiJun.Gong
  14. * Date: 2016-09-02 14:24
  15. * Version: default 1.0.0
  16. * Class description:
  17. * <p>决策树</p>
  18. * <p/>
  19. * *********************************************************
  20. */
  21.  
  22. public class DecisionTree<T, K> {
  23.  
  24. private static String positiveExampleType = "Yes";
  25. private static String counterExampleType = "No";
  26.  
  27. public double pLog2(final double p) {
  28. if (0 == p) return 0;
  29. return p * (Math.log(p) / Math.log(2));
  30. }
  31.  
  32. /**
  33. * 熵计算
  34. *
  35. * @param positiveExample 正例个数
  36. * @param counterExample 反例个数
  37. * @return 熵值
  38. */
  39. public double entropy(final double positiveExample, final double counterExample) {
  40.  
  41. double total = positiveExample + counterExample;
  42. double positiveP = positiveExample / total;
  43. double counterP = counterExample / total;
  44. return -1d * (pLog2(positiveP) + pLog2(counterP));
  45. }
  46.  
  47. /**
  48. * @param features 特征列表
  49. * @param results 对应结果
  50. * @return 将信息整合成新的格式
  51. */
  52. public Multimap<T, CountMap<K>> merge(final List<T> features, final List<T> results) {
  53. //数据转化
  54. Multimap<T, CountMap<K>> InfoMap = ArrayListMultimap.create();
  55. Iterator result = results.iterator();
  56. for (T feature : features) {
  57. K res = (K) result.next();
  58. boolean tag = false;
  59. Collection<CountMap<K>> countMaps = InfoMap.get(feature);
  60. for (CountMap countMap : countMaps) {
  61. if (countMap.getKey().equals(res)) {
  62. /*修改值*/
  63. int num = countMap.getValue() + 1;
  64. InfoMap.remove(feature, countMap);
  65. InfoMap.put(feature, new CountMap<K>(res, num));
  66. tag = true;
  67. break;
  68. }
  69. }
  70. if (!tag)
  71. InfoMap.put(feature, new CountMap<K>(res, 1));
  72. }
  73.  
  74. return InfoMap;
  75. }
  76.  
  77. /**
  78. * 信息增益
  79. *
  80. * @param infoMap 因素(Outlook,Temperature,Humidity,Wind)对应的结果
  81. * @param dataTable 输入的数据表
  82. * @param type 因素中的类型(Outlook{Sunny,Overcast,Rain})
  83. * @param entropyS 总的熵值
  84. * @param totalSize 总的样本数
  85. * @return 信息增益
  86. */
  87. public double gain(Multimap<T, CountMap<K>> infoMap,
  88. Map<K, List<T>> dataTable,
  89. final String type,
  90. double entropyS,
  91. final int totalSize) {
  92. //去重
  93. Set<T> subTypes = Sets.newHashSet();
  94. subTypes.addAll(dataTable.get(type));
  95. /*计算*/
  96. for (T subType : subTypes) {
  97. Collection<CountMap<K>> countMaps = infoMap.get(subType);
  98. double subSize = 0;
  99. double positiveExample = 0;
  100. double counterExample = 0;
  101. for (CountMap<K> countMap : countMaps) {
  102. subSize += countMap.getValue();
  103. if (positiveExampleType.equals(countMap.getKey()))
  104. positiveExample = countMap.getValue();
  105. else
  106. counterExample = countMap.getValue();
  107. }
  108. entropyS -= (subSize / totalSize) * entropy(positiveExample, counterExample);
  109. }
  110. return entropyS;
  111. }
  112.  
  113. /**
  114. * 计算
  115. *
  116. * @param dataTable 数据表
  117. * @param types 因素列表{Outlook,Temperature,Humidity,Wind}
  118. * @param resultType 结果(PlayTennis)
  119. * @return 返回信息增益集合
  120. */
  121. public Map<String, Double> calculate(Map<K, List<T>> dataTable, List<K> types, K resultType) {
  122.  
  123. Map<String, Double> answer = Maps.newHashMap();
  124. List<T> results = dataTable.get(resultType);
  125. int totalSize = results.size();
  126. int positiveExample = 0;
  127. int counterExample = 0;
  128. double entropyS = 0d;
  129. for (T ExampleType : results) {
  130. if (positiveExampleType.equals(ExampleType)) {
  131. ++positiveExample;
  132. continue;
  133. }
  134. ++counterExample;
  135. }
  136. /*计算总的熵*/
  137. entropyS = entropy(positiveExample, counterExample);
  138.  
  139. Multimap<T, CountMap<K>> infoMap;
  140. for (K type : types) {
  141. infoMap = merge(dataTable.get(type), results);
  142. double _gain = gain(infoMap, dataTable, (String) type, entropyS, totalSize);
  143. answer.put((String) type, _gain);
  144. }
  145. return answer;
  146. }
  147.  
  148. }  package com.qunar.data.tree;
  1. import com.google.common.collect.Lists;
  2. import com.google.common.collect.Maps;
  3.  
  4. import java.util.*;
  5.  
  6. /**
  7. * *********************************************************
  8. * <p/>
  9. * Author: XiJun.Gong
  10. * Date: 2016-09-02 16:43
  11. * Version: default 1.0.0
  12. * Class description:
  13. * <p/>
  14. * *********************************************************
  15. */
  16. public class Main {
  17.  
  18. public static void main(String args[]) {
  19.  
  20. Scanner scanner = new Scanner(System.in);
  21. while (scanner.hasNext()) {
  22. DecisionTree<String, String> dt = new DecisionTree();
  23. Map<String, List<String>> dataTable = Maps.newHashMap();
  24. /*Map<String, List<String>> dataTable = Maps.newHashMap();*/
  25. List<String> types = Lists.newArrayList();
  26. String resultType;
  27. int factorSize = scanner.nextInt();
  28. int demoSize = scanner.nextInt();
  29. String type;
  30.  
  31. for (int i = 0; i < factorSize; i++) {
  32. List<String> demos = Lists.newArrayList();
  33. type = scanner.next();
  34. for (int j = 0; j < demoSize; j++) {
  35. demos.add(scanner.next());
  36. }
  37. dataTable.put(type, demos);
  38. }
  39. for (int i = 1; i < factorSize; i++) {
  40. types.add(scanner.next());
  41. }
  42. resultType = scanner.next();
  43. Map<String, Double> ans = dt.calculate(dataTable, types, resultType);
  44. List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(ans.entrySet());
  45. Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
  46.  
  47. @Override
  48. public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
  49. return (o2.getValue() > o1.getValue() ? 1 : -1);
  50. }
  51. });
  52.  
  53. for (Map.Entry<String, Double> iterator : list) {
  54. System.out.println(iterator.getKey() + "= " + iterator.getValue());
  55. }
  56. }
  57. }
  58.  
  59. }
  60. /**
  61. *使用举例:*
  62. 5 14
  63. Outlook Sunny Sunny Overcast Rain Rain Rain Overcast Sunny Sunny Rain Sunny Overcast Overcast Rain
  64. Temperature Hot Hot Hot Mild Cool Cool Cool Mild Cool Mild Mild Mild Hot Mild
  65. Humidity High High High High Normal Normal Normal High Normal Normal Normal High Normal High
  66. Wind Weak Strong Weak Weak Weak Strong Strong Weak Weak Weak Strong Strong Weak Strong
  67. PlayTennis No No Yes Yes Yes No Yes No Yes Yes Yes Yes Yes No
  68. Outlook Temperature Humidity Wind PlayTennis
  69. */

结果:

  1. Outlook= 0.2467498197744391
  2. Humidity= 0.15183550136234136
  3. Wind= 0.04812703040826927
  4. Temperature= 0.029222565658954647

机器学习之决策树熵&信息增量求解算法实现的更多相关文章

  1. 机器学习之决策树(ID3)算法与Python实现

    机器学习之决策树(ID3)算法与Python实现 机器学习中,决策树是一个预测模型:他代表的是对象属性与对象值之间的一种映射关系.树中每个节点表示某个对象,而每个分叉路径则代表的某个可能的属性值,而每 ...

  2. 【Machine Learning·机器学习】决策树之ID3算法(Iterative Dichotomiser 3)

    目录 1.什么是决策树 2.如何构造一棵决策树? 2.1.基本方法 2.2.评价标准是什么/如何量化评价一个特征的好坏? 2.3.信息熵.信息增益的计算 2.4.决策树构建方法 3.算法总结 @ 1. ...

  3. 决策树笔记:使用ID3算法

    决策树笔记:使用ID3算法 决策树笔记:使用ID3算法 机器学习 先说一个偶然的想法:同样的一堆节点构成的二叉树,平衡树和非平衡树的区别,可以认为是"是否按照重要度逐渐降低"的顺序 ...

  4. 从决策树学习谈到贝叶斯分类算法、EM、HMM --别人的,拷来看看

    从决策树学习谈到贝叶斯分类算法.EM.HMM     引言 最近在面试中,除了基础 &  算法 & 项目之外,经常被问到或被要求介绍和描述下自己所知道的几种分类或聚类算法(当然,这完全 ...

  5. 从决策树学习谈到贝叶斯分类算法、EM、HMM

    从决策树学习谈到贝叶斯分类算法.EM.HMM                (Machine Learning & Recommend Search交流新群:172114338) 引言 log ...

  6. 机器学习实战 -- 决策树(ID3)

    机器学习实战 -- 决策树(ID3)   ID3是什么我也不知道,不急,知道他是干什么的就行   ID3是最经典最基础的一种决策树算法,他会将每一个特征都设为决策节点,有时候,一个数据集中,某些特征属 ...

  7. pyhton机器学习入门基础(机器学习与决策树)

    //2019.07.26#scikit-learn数据挖掘工具包1.Scikit learn是基于python的数据挖掘和机器学习的工具包,方便实现数据的数据分析与高级操作,是数据分析里面非常重要的工 ...

  8. 机器学习实战---决策树CART回归树实现

    机器学习实战---决策树CART简介及分类树实现 一:对比分类树 CART回归树和CART分类树的建立算法大部分是类似的,所以这里我们只讨论CART回归树和CART分类树的建立算法不同的地方.首先,我 ...

  9. Python机器学习笔记:奇异值分解(SVD)算法

    完整代码及其数据,请移步小编的GitHub 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/MachineLearningNote 奇异值分解(Singu ...

随机推荐

  1. [转]java二维码生成与解析代码实现

    转载地址:点击打开链接 二维码,是一种采用黑白相间的平面几何图形通过相应的编码算法来记录文字.图片.网址等信息的条码图片.如下图 二维码的特点: 1.  高密度编码,信息容量大 可容纳多达1850个大 ...

  2. 仿网易新闻 ViewPager 实现图片自动轮播

    新闻 App 首页最上方一般会循环播放热点图片,如下图所示. 本文主要介绍了利用 ViewPager 实现轮播图片,图片下方加上小圆点指示器标记当前位置,并利用 Timer+Handler 实现了自动 ...

  3. Java 导入Excel文件到数据库

    原文:http://www.jb51.net/article/44021.htm 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两 ...

  4. RobotFramework中加载自定义python包中的library(一个py文件中有多个类)

    结构如下: appsdk\ appsdk.py(这里面有多个类,包括appsdk,appsdksync等类) __init__.py ... ① 有个appsdk的文件夹(符合python包的定义) ...

  5. C#:额外知识点

    6.写入.输出二进制数据(指定地址 与 内存交互数据) public void WriteSerialize(object data, int offset, int length) { Binary ...

  6. QT常见数据类型操作

    平常使用QStringList,都是通过at()访问其中的元素,然后试图也通过它修改元素,编译器报错,原来要使用下标访问修改: //accessRecList.at(3)=strSimilarity; ...

  7. svn: 期望文件系统格式在“1”到“4”之间;发现格式“6”

    svn 客户端浏览的时候出现了这个提示,经查,发现是Subversion 1.7 < TortoiseSVN 1.8导致的,需要更换版本,应该是 Version(Subversion) > ...

  8. Bug跟踪方法

     Bug跟踪函数调用方法 StackTraceElement mSte = new Exception().getStackTrace()[1]; Log.e("mmm", mSt ...

  9. mate 标签的 http-equiv

    http-equiv 1. 页面的描述 <meta http-equiv="description" content="This is my page"& ...

  10. Concurrency vs. Parallelism

    http://getakka.net/docs/concepts/terminology Terminology and Concepts In this chapter we attempt to ...