决策树之id3算法

决策树的id3算法是之前学机器学习的一个作业,今天拿出来复习了一遍,想了想,贴到博客里共享吧

先把id3算法的过程贴出来

ID3(Examples, Target_attributes, Attributes)
Examples are the training examples. Target_attribute is the attribute whose
value is to be predicted by the tree. Attributes is a list of other attributes that
may be tested by the learned decision tree. Returns a decision tree that correctly
classifies the given Examples.
Create a Root node for the tree
If all Examples are positive, Return the single‐node tree Root, with label = +
If all Examples are negative, Return the single‐node tree Root, with label = ‐
If Attributes is empty, Return the single‐node tree Root, with label = most
common value of Target_attribute in Examples
Otherwise Begin
   A ← the attribute from Attributes that best classifies Examples
    The decision attribute for Root ← A
   For each possible value, vi, of A                                                        
       Add a new tree branch below Root, corresponding to the test A = vi
        Let Examplesvi be the subset of Examples that have value vi for A
        If Examplesvi is empty
         Then below this new branch add a leaf node with label = most commonvalue of Target_attribute in Examples
          Else below this new branch add the subtreeID3(Examplesvi, Target_attribute, Attributes ‐ {A})
End
Return 
RootNote: The best attribute is the one with highest information gain//信息增益,怎么算,自己查书吧

作业的元数据是这样的一张图:

下边是用C++写的源代码:

#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;

#define ROW 14
#define COL 5
#define log2 0.69314718055

typedef struct TNode
{
	char data[15];
	char weight[15]; 
	TNode * firstchild,*nextsibling;
}*tree;

typedef struct LNode
{
	char  OutLook[15];
	char  Temperature[15];
	char  Humidity[15];
	char  Wind[15];
	char  PlayTennis[5];

	LNode *next;
}*link;
typedef struct AttrNode
{
	char   attributes[15];//属性
	int	   attr_Num;//属性的个数

	AttrNode *next;
}*Attributes;

char * Examples[ROW][COL] = {//"OverCast","Cool","High","Strong","No",
					//	"Rain","Hot","Normal","Strong","Yes",
						"Sunny","Hot","High","Weak","No",
						  "Sunny","Hot","High","Strong","No",
						  "OverCast","Hot","High","Weak","Yes",
						  "Rain","Mild","High","Weak","Yes",
						"Rain","Cool","Normal","Weak","Yes",
						"Rain","Cool","Normal","Strong","No",
						"OverCast","Cool","Normal","Strong","Yes",
						"Sunny","Mild","High","Weak","No",
						"Sunny","Cool","Normal","Weak","Yes",
						"Rain","Mild","Normal","Weak","Yes",
						"Sunny","Mild","Normal","Strong","Yes",
						"OverCast","Mild","Normal","Strong","Yes",
						"OverCast","Hot","Normal","Weak","Yes",
						"Rain","Mild","High","Strong","No"
						};
char * Attributes_kind[4] = {"OutLook","Temperature","Humidity","Wind"};
int	   Attr_kind[4] = {3,3,2,2};

char * OutLook_kind[3] = {"Sunny","OverCast","Rain"};
char * Temperature_kind[3] = {"Hot","
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值