已知:流感训练数据集,预定义两个类别;
求:用ID3算法建立流感的属性描述决策树
流感训练数据集

No.

头痛

肌肉痛

体温

患流感

1

是(1)

是(1)

正常(0)

否(0)

2

是(1)

是(1)

高(1)

是(1)

3

是(1)

是(1)

很高(2)

是(1)

4

否(0)

是(1)

正常(0)

否(0)

5

否(0)

否(0)

高(1)

否(0)

6

否(0)

是(1)

很高(2)

是(1)

7

是(1)

否(0)

高(1)

是(1)

原理分析:

在决策树的每一个非叶子结点划分之前,先计算每一个属性所带来的信息增益,选择最大信息增益的属性来划分,因为信息增益越大,区分样本的能力就越强,越具有代表性其中。

信息熵计算:

信息增益:

计算的结果(草稿上的字丑别喷):

--------------------------------------------------------------------------------------------------------------------------------------------

*************************************************************************************************************

************************实现*********************************************

package ID3Tree;
import java.util.Comparator;;
@SuppressWarnings("rawtypes")
public class Comparisons implements Comparator
{
public int compare(Object a, Object b) throws ClassCastException{
String str1 = (String)a;
String str2 = (String)b;
return str1.compareTo(str2);
}
}
package ID3Tree;

public class Entropy {
//信息熵
public static double getEntropy(int x, int total)
{
if (x == 0)
{
return 0;
}
double x_pi = getShang(x,total);
return -(x_pi*Logs(x_pi));
} public static double Logs(double y)
{
return Math.log(y) / Math.log(2);
} public static double getShang(int x, int total)
{
return x * Double.parseDouble("1.0") / total;
}
}
package ID3Tree;

public class TreeNode {
//父节点
TreeNode parent;
//指向父节点的属性
String parentAttribute;
String nodeName;
String[] attributes;
TreeNode[] childNodes;
}
package ID3Tree;
import java.util.*; public class UtilID3 {
TreeNode root;
private boolean[] flag;
//训练集
private Object[] trainArrays;
//节点索引
private int nodeIndex;
public static void main(String[] args)
{
//初始化训练集数组
Object[] arrays = new Object[]{
new String[]{"是","是","正常","否"},
new String[]{"是","是","高","是"},
new String[]{"是","是","很高","是"},
new String[]{"否","是","正常","否"},
new String[]{"否","否","高","否"},
new String[]{"否","是","很高","是"},
new String[]{"是","否","高","是"}};
UtilID3 ID3Tree = new UtilID3();
ID3Tree.create(arrays, 3);
} //创建
public void create(Object[] arrays, int index)
{
this.trainArrays = arrays;
initial(arrays, index);
createDTree(arrays);
printDTree(root);
} //初始化
public void initial(Object[] dataArray, int index)
{
this.nodeIndex = index; //数据初始化
this.flag = new boolean[((String[])dataArray[0]).length];
for (int i = 0; i<this.flag.length; i++)
{
if (i == index)
{
this.flag[i] = true;
}
else
{
this.flag[i] = false;
}
}
} //创建决策树
public void createDTree(Object[] arrays)
{
Object[] ob = getMaxGain(arrays);
if (this.root == null)
{
this.root = new TreeNode();
root.parent = null;
root.parentAttribute = null;
root.attributes = getAttributes(((Integer)ob[1]).intValue());
root.nodeName = getNodeName(((Integer)ob[1]).intValue());
root.childNodes = new TreeNode[root.attributes.length];
insert(arrays, root);
}
} //插入决策树
public void insert(Object[] arrays, TreeNode parentNode)
{
String[] attributes = parentNode.attributes;
for (int i = 0; i < attributes.length; i++)
{
Object[] Arrays = pickUpAndCreateArray(arrays, attributes[i],getNodeIndex(parentNode.nodeName));
Object[] info = getMaxGain(Arrays);
double gain = ((Double)info[0]).doubleValue();
if (gain != 0)
{
int index = ((Integer)info[1]).intValue();
TreeNode currentNode = new TreeNode();
currentNode.parent = parentNode;
currentNode.parentAttribute = attributes[i];
currentNode.attributes = getAttributes(index);
currentNode.nodeName = getNodeName(index);
currentNode.childNodes = new TreeNode[currentNode.attributes.length];
parentNode.childNodes[i] = currentNode;
insert(Arrays, currentNode);
}
else
{
TreeNode leafNode = new TreeNode();
leafNode.parent = parentNode;
leafNode.parentAttribute = attributes[i];
leafNode.attributes = new String[0];
leafNode.nodeName = getLeafNodeName(Arrays);
leafNode.childNodes = new TreeNode[0];
parentNode.childNodes[i] = leafNode;
}
}
} //输出
public void printDTree(TreeNode node)
{
System.out.println(node.nodeName);
TreeNode[] childs = node.childNodes;
for (int i = 0; i < childs.length; i++)
{
if (childs[i] != null)
{
System.out.println("如果:"+childs[i].parentAttribute);
printDTree(childs[i]);
}
}
} //剪取数组
public Object[] pickUpAndCreateArray(Object[] arrays, String attribute, int index)
{
List<String[]> list = new ArrayList<String[]>();
for (int i = 0; i < arrays.length; i++)
{
String[] strs = (String[])arrays[i];
if (strs[index].equals(attribute))
{
list.add(strs);
}
}
return list.toArray();
} //取得节点名
public String getNodeName(int index)
{
String[] strs = new String[]{"头痛","肌肉痛","体温","患流感"};
for (int i = 0; i < strs.length; i++)
{
if (i == index)
{
return strs[i];
}
}
return null;
} //取得叶子节点名
public String getLeafNodeName(Object[] arrays)
{
if (arrays != null && arrays.length > 0)
{
String[] strs = (String[])arrays[0];
return strs[nodeIndex];
}
return null;
} //取得节点索引
public int getNodeIndex(String name)
{
String[] strs = new String[]{"头痛","肌肉痛","体温","患流感"};
for (int i = 0; i < strs.length; i++)
{
if (name.equals(strs[i]))
{
return i;
}
}
return -1;
} //得到最大信息增益
public Object[] getMaxGain(Object[] arrays)
{
Object[] result = new Object[2];
double gain = 0;
int index = -1;
for (int i = 0; i<this.flag.length; i++)
{
if (!this.flag[i])
{
double value = gain(arrays, i);
if (gain < value)
{
gain = value;
index = i;
}
}
}
result[0] = gain;
result[1] = index;
if (index != -1)
{
this.flag[index] = true;
}
return result;
} //取得属性数组
public String[] getAttributes(int index)
{
@SuppressWarnings("unchecked")
TreeSet<String> set = new TreeSet<String>(new Comparisons());
for (int i = 0; i<this.trainArrays.length; i++)
{
String[] strs = (String[])this.trainArrays[i];
set.add(strs[index]);
}
String[] result = new String[set.size()];
return set.toArray(result); } //计算信息增益
public double gain(Object[] arrays, int index)
{
String[] playBalls = getAttributes(this.nodeIndex);
int[] counts = new int[playBalls.length];
for (int i = 0; i<counts.length; i++)
{
counts[i] = 0;
} for (int i = 0; i<arrays.length; i++)
{
String[] strs = (String[])arrays[i];
for (int j = 0; j<playBalls.length; j++)
{
if (strs[this.nodeIndex].equals(playBalls[j]))
{
counts[j]++;
}
}
} double entropyS = 0;
for (int i = 0;i <counts.length; i++)
{
entropyS = entropyS + Entropy.getEntropy(counts[i], arrays.length);
} String[] attributes = getAttributes(index);
double total = 0;
for (int i = 0; i<attributes.length; i++)
{
total = total + entropy(arrays, index, attributes[i], arrays.length);
}
return entropyS - total;
} public double entropy(Object[] arrays, int index, String attribute, int totals)
{
String[] playBalls = getAttributes(this.nodeIndex);
int[] counts = new int[playBalls.length];
for (int i = 0; i < counts.length; i++)
{
counts[i] = 0;
} for (int i = 0; i < arrays.length; i++)
{
String[] strs = (String[])arrays[i];
if (strs[index].equals(attribute))
{
for (int k = 0; k<playBalls.length; k++)
{
if (strs[this.nodeIndex].equals(playBalls[k]))
{
counts[k]++;
}
}
}
} int total = 0;
double entropy = 0;
for (int i = 0; i < counts.length; i++)
{
total = total +counts[i];
} for (int i = 0; i < counts.length; i++)
{
entropy = entropy + Entropy.getEntropy(counts[i], total);
}
return Entropy.getShang(total, totals)*entropy;
}
}

决策树ID3算法的java实现(基本适用所有的ID3)的更多相关文章

  1. 决策树ID3算法的java实现(基本试用所有的ID3)

    已知:流感训练数据集,预定义两个类别: 求:用ID3算法建立流感的属性描述决策树 流感训练数据集 No. 头痛 肌肉痛 体温 患流感 1 是(1) 是(1) 正常(0) 否(0) 2 是(1) 是(1 ...

  2. 决策树ID3算法的java实现

    决策树的分类过程和人的决策过程比较相似,就是先挑“权重”最大的那个考虑,然后再往下细分.比如你去看医生,症状是流鼻涕,咳嗽等,那么医生就会根据你的流鼻涕这个权重最大的症状先认为你是感冒,接着再根据你咳 ...

  3. ID3算法(Java实现)

    数据存储文件:buycomputer.properties #数据个数 datanum=14 #属性及属性值 nodeAndAttribute=年龄:青/中/老,收入:高/中/低,学生:是/否,信誉: ...

  4. ID3算法(2)

    今天,我来讲解的是决策树.对于决策树来说,主要有两种算法:ID3算法和C4.5算法.C4.5算法是 对ID3算法的改进.今天主要先讲ID3算法,之后会讲C4.5算法和随机森林等. Contents   ...

  5. ID3算法Java实现

    ID3算法java实现 1 ID3算法概述 1.1 信息熵 熵是无序性(或不确定性)的度量指标.假如事件A的全概率划分是(A1,A2,...,An),每部分发生的概率是(p1,p2,...,pn).那 ...

  6. 数据挖掘之决策树ID3算法(C#实现)

    决策树是一种非常经典的分类器,它的作用原理有点类似于我们玩的猜谜游戏.比如猜一个动物: 问:这个动物是陆生动物吗? 答:是的. 问:这个动物有鳃吗? 答:没有. 这样的两个问题顺序就有些颠倒,因为一般 ...

  7. 决策树 -- ID3算法小结

          ID3算法(Iterative Dichotomiser 3 迭代二叉树3代),是一个由Ross Quinlan发明的用于决策树的算法:简单理论是越是小型的决策树越优于大的决策树. 算法归 ...

  8. 决策树-预测隐形眼镜类型 (ID3算法,C4.5算法,CART算法,GINI指数,剪枝,随机森林)

    1. 1.问题的引入 2.一个实例 3.基本概念 4.ID3 5.C4.5 6.CART 7.随机森林 2. 我们应该设计什么的算法,使得计算机对贷款申请人员的申请信息自动进行分类,以决定能否贷款? ...

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

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

随机推荐

  1. Web中的积累:外观模式 Facade

    摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 壹 前言 目测好久没写文章了,距离上一篇文章也有二十多天.我是怎么了?哈 ...

  2. python练习三—解析xml

    使用python解析xml,主要使用sax的ContentHandler中的标签开始和标签结束的方法驱动,然后在开始(或者结束)事件中决定使用什么处理方法,使用dispatcher来决定并分发到指定方 ...

  3. IDEA的几个常用配置,日常开发必备。

    用了IDEA有很长时间了,身边的同事朋友也都慢慢的开始都从Eclipse切换到IDEA了,其实无论是Eclipse还是IntelliJ IDEA都是开发工具而已,各自都有优点.但是刚从Eclipse切 ...

  4. 伪指令 ADR 与 LDR 的区别

    指令简介: adr r0, _start 得到的是 _start 的当前执行位置,由 pc+offset 决定 ldr r0, =_start 得到的是绝对的地址,链接时决定 程序示例: ldr r0 ...

  5. 我的asp.net core目录

    推荐 Asp.NETCore轻松学系列阅读指引目录(asp.net core 2.2) 官方文档翻译 http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore- ...

  6. c#金额转换成中文大写金额

    2018-08-24 转别人 c#金额转换成中文大写金额 /// <summary> /// 金额转换成中文大写金额 /// </summary> /// <param ...

  7. 《c#图解教程》

    书名 <c#图解教程> 图片 时间 2017-10-12月 学习 第20章的异步编程很好,在项目里面很有用.东西有点多时间久了不用就忘了

  8. [android] smartimageview&常见的开源代码

    github上搜索开源框架android-smarty-imageview,下载压缩包,拷贝我们之前写的网络图片查看器布局. 解压下载包里面的数据,找到java源码拷贝到我们的项目里,这时我们可以看到 ...

  9. 4.1 explain 之 id

    一.id 是什么 select 查询的序列化,包含一组数字,表示查询中执行select子句或操作的顺序 二.三种情况 a. id相同,执行顺序由上至下 b. 如果是子查询,id的序号会递增,id值越大 ...

  10. 改变Tomcat在地址栏上显示的小猫图标

    部署在Tomcat上的项目通常在地址栏会显示一个小猫的图标,那么如何改变这个图标呢? 第一步.制作自己显示的图标 这里使用的是在线制作的方式,推荐一个在线制作的网站---比特虫:http://www. ...