ID-3学习 代码实现

该项目采用了业界领先的 TDD(TreeNewBee-Driven Development,吹牛逼导向开发模式) 方式。-Rrrrraulista

1. 样例数据集

样例数据集来自周老师《机器学习》上的“西瓜数据集2.0”

数据结构定义如下:

结构体类型定义//update

typedef struct SampleNode{
int SeqNum; //样例编号
bool Type; //样例类别(true 好瓜;false 非好瓜) int Color; //色泽 (1 青绿; 2 乌黑; 3 浅白)
int Root; //根部 (1 蜷缩; 2 稍微蜷缩; 3 硬挺)
int Sounds; //敲击声音 (1 沉闷; 2 浊响; 3 清脆)
int Style; //纹理 (1 清晰; 2 稍微模糊; 3 模糊)
int Struct; //脐部特性 (1 凹陷; 2 稍凹; 3 平坦)
int Touch; //触感 (1 硬滑; 2 软粘;)
};
SampleNode sample[17]={
{ 1 , true , 1 , 1 , 2 , 1 , 1 , 1 },
{ 2 , true , 2 , 1 , 1 , 1 , 1 , 1 },
{ 3 , true , 2 , 1 , 2 , 1 , 1 , 1 },
{ 4 , true , 1 , 1 , 1 , 1 , 1 , 1 },
{ 5 , true , 3 , 1 , 2 , 1 , 1 , 1 },
{ 6 , true , 1 , 2 , 2 , 1 , 2 , 2 },
{ 7 , true , 2 , 2 , 2 , 2 , 2 , 2 },
{ 8 , true , 2 , 2 , 2 , 1 , 2 , 1 },
{ 9 , false , 2 , 2 , 1 , 2 , 2 , 1},
{ 10 , false , 1 , 3 , 3 , 1 , 3 , 2},
{ 11 , false , 3 , 3 , 3 , 3 , 3 , 1},
{ 12 , false , 3 , 1 , 2 , 3 , 3 , 2},
{ 13 , false , 1 , 2 , 2 , 2 , 1 , 1},
{ 14 , false , 3 , 2 , 1 , 2 , 1 , 1},
{ 15 , false , 2 , 2 , 2 , 1 , 2 , 2},
{ 16 , false , 3 , 1 , 2 , 3 , 3 , 1},
{ 17 , false , 1 , 1 , 1 , 2 , 2 , 1},
};

二维数组实现方法

int data[17][7]{//整数类型西瓜数据集二维数组(类别,色泽,根部,声音,纹路,脐部,触感)
{1 , 1 , 1 , 2 , 1 , 1 , 1},
{1 , 2 , 1 , 1 , 1 , 1 , 1},
{1 , 2 , 1 , 2 , 1 , 1 , 1},
{1 , 1 , 1 , 1 , 1 , 1 , 1},
{1 , 3 , 1 , 2 , 1 , 1 , 1},
{1 , 1 , 2 , 2 , 1 , 2 , 2},
{1 , 2 , 2 , 2 , 2 , 2 , 2},
{1 , 2 , 2 , 2 , 1 , 2 , 1},
{0 , 2 , 2 , 1 , 2 , 2 , 1},
{0 , 1 , 3 , 3 , 1 , 3 , 2},
{0 , 3 , 3 , 3 , 3 , 3 , 1},
{0 , 3 , 1 , 2 , 3 , 3 , 2},
{0 , 1 , 2 , 2 , 2 , 1 , 1},
{0 , 3 , 2 , 1 , 2 , 1 , 1},
{0 , 2 , 2 , 2 , 1 , 2 , 2},
{0 , 3 , 1 , 2 , 3 , 3 , 1},
{0 , 1 , 1 , 1 , 2 , 2 , 1},
};

2.信息熵的计算

在二维数组构成的数据集上,先写出对于样本类别的信息熵地计算的基础上,逐步修改,使其具备复用性。

double Entropy(int data[17][7]);	//Declaration of the function
double Entropy(int data[17][7]){ //to calculate the entropy of dataset
int trueNum=0;
for(int i=0;i<17;i++){ //count the number of TRUE numbers, which means 好瓜
if(data[i][0]==1){
trueNum++;
}else{
continue;
}
}
int falseNum=17-trueNum; // Total - true.num = false.num
double p1=trueNum/17.0;
double p2=falseNum/17.0;
if(p1!=0){ //define that 0*log_2(0) = 0
p1=-1*(p1*(log(p1)/log(2)));
}
if(p2!=0){
p2=-1*(p2*(log(p2)/log(2)));
}
double Ent=p1+p2;
return Ent;
}
//main():double ent=Entropy(data);

可以看到,该段代码成功计算了总体数据集的信息熵约为 0.998(与书上数值相同),但是该段代码默认了数据集长度为17,无法应用于子集合计算,同时传递的参数固定(二维数组),如果不解决该问题,则声明函数无意义,于是下面着手修改,使该函数更加具备复用性。

  • 首先为了方便计算数组长度,人为加入数组下界,最后一行所有元素赋值为“-1”
	{-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1},
  • 这样做能简化程序。

而在此基础上使用如下代码

	int SetLength=0;
for(int i=0;num[i][0]!=-1;i++){
SetLength++;
}

可以实现在函数内计算二维数组的行数,提高了数组的复用性能。

//通过这个函数可以计算出数据集种某个属性具有多少种可能取值
int TypeNum(int set[][7],int att){
int SetLength=0; //计算出二维数组行数
for(int i=0;set[i][0]!=-1;i++){
SetLength++;
}
printf("\ntesta=%d",SetLength); //测试用
for(int i=0;i<SetLength;i++){
for(int j=i+1;j<SetLength;j++){
if(set[i][att]==set[j][att]){
SetLength--;
break;
}
}
}
printf("\ntestb=%d",SetLength); //测试用testb
return SetLength;
}

//修改后的信息熵计算函数如下所示
double Entropy(int num[][7]){//计算数据关于的类别的信息熵
int trueNum=0;
int SetLength=0; //计算出了二维数组的行数
for(int i=0;num[i][0]!=-1;i++){
SetLength++;
}
for(int i=0;i<SetLength;i++){
if(num[i][0]==1){
trueNum++;
}else{
continue;
}
}
int falseNum=SetLength-trueNum;
double p1=(double)trueNum/SetLength;
double p2=(double)falseNum/SetLength;
if(p1!=0){
p1=-(p1*(log(p1)/log(2)));
}
if(p2!=0){
p2=-(p2*(log(p2)/log(2)));
}
double Ent=p1+p2;
return Ent;
}

PRaCtice[1]的更多相关文章

  1. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  2. Atitit 数据存储视图的最佳实际best practice attilax总结

    Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论  本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...

  3. The Practice of .NET Cross-Platforms

    0x01 Preface This post is mainly to share the technologies on my practice about the .NET Cross-Platf ...

  4. Exercise 24: More Practice

    puts "Let's practice everything." puts 'You\'d need to know \'bout escapes with \\ that do ...

  5. ConCurrent in Practice小记 (3)

    ConCurrent in Practice小记 (3) 高级同步技巧 Semaphore Semaphore信号量,据说是Dijkstra大神发明的.内部维护一个许可集(Permits Set),用 ...

  6. ConCurrent in Practice小记 (2)

    Java-ConCurrent2.html :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0 ...

  7. ConCurrent in Practice小记 (1)

    ConCurrent in Practice小记 (1) 杂记,随书自己写的笔记: 综述问题 1.线程允许在同一个进程中的资源,包括共享内存,内存句柄,文件句柄.但是每个进程有自己的程序计数器,栈和局 ...

  8. 1.2 基础知识——关于猪皮(GP,Generic Practice)

    摘要: 这是<CMMI快乐之旅>系列文章之一.说起猪皮(GP,Generic Practice),真的让人又爱又恨,中文翻译叫通用实践.CMMI标准中每个级别包含几个PA,每个PA又包含几 ...

  9. 2015年第2本(英文第1本):《The Practice of Programming》

    2015年计划透析10本英文原著,最开始选定的第一本英文书是<Who Moved my Cheese>,可是这本书实在是太短.太简单了,总体的意思就是要顺应变化,要跳出自己的舒适区,全文不 ...

  10. Java Concurrency In Practice -Chapter 2 Thread Safety

    Writing thread-safe code is managing access to state and in particular to shared, mutable state. Obj ...

随机推荐

  1. WordPress站点绑定多个域名

    refer to https://blog.csdn.net/wzl505/article/details/54970321 打开根目录下的 wp-config.php 文件,找到 require_o ...

  2. jquery ajax常用的登录登出

    整理jquery+ajax的登录登出方法. //登录 var currentUserId = -1; $(function() { var timestamp = (new Date()).value ...

  3. POJ - 3658 Artificial Lake

    题意:向N个连续且高度不同的平台灌水,平台各有宽度,且高度各不相同.一开始,先向高度最低的平台灌水,直到灌满溢出,流向其他的平台,直至所有平台都被覆盖.已知每分钟注入高度为1且宽度为1的水,问每个平台 ...

  4. GPLT L3-021 神坛

    在古老的迈瑞城,巍然屹立着 n 块神石.长老们商议,选取 3 块神石围成一个神坛.因为神坛的能量强度与它的面积成反比,因此神坛的面积越小越好.特殊地,如果有两块神石坐标相同,或者三块神石共线,神坛的面 ...

  5. bitcoind

    Bitcoin Core Daemon version v0.15.1.0-g7b57bc998f Usage: bitcoind [options] Start Bitcoin Core Daemo ...

  6. winform操作windows系统计算器

    winform对系统计算器的调用,启动,最大化最小化显示,在mainwindow设置topmost=true时,正常显示计算器并置顶. /// <summary> /// 获取窗体的句柄函 ...

  7. POJ-1308 Is It A Tree?(并查集判断是否是树)

    http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empt ...

  8. sonarqube linux安装总结,集成jenkins

    第一条建议,安装sonarqube首先看好版本号,不同版本号的安装配置可能不同,如果你想走捷径,看官网对应发布的安装使用教程.https://www.sonarqube.org/downloads/ ...

  9. 201771010123汪慧和《面向对象程序设计Java》第十周实验总结

    一.理论部分 1.泛型:也称参数化类型.就是定义类.接口和方法时,通过类型参数指示将要处理的对象类型. 2.泛型程序设计:编写代码可以被很多不同类型的对象所重用. 3.泛型方法: a.除了泛型类外,还 ...

  10. java笔记之输入输出流

    做统计单词个数时的笔记