PRaCtice[1]
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]的更多相关文章
- 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 ...
- Atitit 数据存储视图的最佳实际best practice attilax总结
Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论 本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...
- The Practice of .NET Cross-Platforms
0x01 Preface This post is mainly to share the technologies on my practice about the .NET Cross-Platf ...
- Exercise 24: More Practice
puts "Let's practice everything." puts 'You\'d need to know \'bout escapes with \\ that do ...
- ConCurrent in Practice小记 (3)
ConCurrent in Practice小记 (3) 高级同步技巧 Semaphore Semaphore信号量,据说是Dijkstra大神发明的.内部维护一个许可集(Permits Set),用 ...
- ConCurrent in Practice小记 (2)
Java-ConCurrent2.html :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0 ...
- ConCurrent in Practice小记 (1)
ConCurrent in Practice小记 (1) 杂记,随书自己写的笔记: 综述问题 1.线程允许在同一个进程中的资源,包括共享内存,内存句柄,文件句柄.但是每个进程有自己的程序计数器,栈和局 ...
- 1.2 基础知识——关于猪皮(GP,Generic Practice)
摘要: 这是<CMMI快乐之旅>系列文章之一.说起猪皮(GP,Generic Practice),真的让人又爱又恨,中文翻译叫通用实践.CMMI标准中每个级别包含几个PA,每个PA又包含几 ...
- 2015年第2本(英文第1本):《The Practice of Programming》
2015年计划透析10本英文原著,最开始选定的第一本英文书是<Who Moved my Cheese>,可是这本书实在是太短.太简单了,总体的意思就是要顺应变化,要跳出自己的舒适区,全文不 ...
- 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 ...
随机推荐
- 实盘交易哪些代码BUG产生严重亏损
1.少写一个return,疯狂开平2900次,以爆仓结束 2.在上生产环境前,不小心误删除了一个“!”(我至今也不清楚为什么会误删了,晕),导致平仓逻辑一直不满足,不止损,让我损失惨重! 3.卖单变买 ...
- 读书笔记 - js高级程序设计 - 第七章 函数表达式
闭包 有权访问另一个函数作用域中的变量的函数 匿名函数 函数没有名字 少用闭包 由于闭包会携带包含它的函数的作用域,因此会比其它函数占用更多的内存.过度使用闭包可能会导致内存占用过多,我们建议读者 ...
- Q8:String to Integer (atoi)
8. String to Integer (atoi) 官方的链接:8. String to Integer (atoi) Description : Implement atoi to conver ...
- .NET CORE 获取微信支付回调
1.获取微信支付的回调的数据 Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Reques ...
- 查看电脑连接的WiFi的密码
这里提供两种办法:图形界面操作版.命令行操作版 方法一: 打开控制面板 点击红色框部分 方法二 打开命令行:输入命令netsh wlan show profiles "连接的WiFi的名称& ...
- Eclipse上传Git远程仓库,并且增加Maven Dependencies
前言: 遇见问题了,公司一台电脑,家里一台电脑,当有项目在进行的时候,又不想把电脑背来背去的,就像一个人玩单机,这个时候GIT就可以帮你解决这个问题.当GIT准备就绪的时候,新的问题来了git下载下载 ...
- 网页时不时打不开?试试阿里DNS 233.5.5.5 /233.6..6.6
最经上网都是用手机热点,但发现用谷歌浏览器时,时不时打不开网页.最后发现是DNS的问题,原来我的dns是8.8.8.8. 最后更改成阿里的DNS 233.5.5.5 /233.6..6.6,打开网页流 ...
- Python说文解字_杂谈04
1. 鸭子类型: 当你看到一只鸟走来像鸭子,游泳起来像鸭子,叫起来也像鸭子,他么他就可以叫做鸭子.任何可迭代的对象.一样的方法,可以用可迭代的话,就可以迭代的组合打印.__getitem__可以塞到任 ...
- 17。3.12---re模块--正则表达式操作指南
1----python re模块(Regular Expressioin正则表达式)提供了一个与perl等编程语言类似的正则匹配操作,他是一个处理python字符串的强有力的工具,有自己的语法和独立的 ...
- MyBatis从入门到精通(第2章):MyBatis XML方式的基本用法【insert用法、update用法、delete用法】
2.4 insert 用法 2.4.1 简单的 insert方法 在接口 UserMapper.java 中添加如下方法. /** * 新增用户 * @param sysUser * @retur ...
