ID3算法

1.1、核心思想

在决策树各个结点上应用信息增益准则选择特征,递归地构建决策树。

1.2、具体方法

从根结点(root node)开始,对结点计算所有可能的特征信息增益,选择信息增益最大的特征作为结点的特征,由该特征的不同取值建立子结点;再对子结点递归地调用以上方法,构建决策树;直到所有特征的信息增益均很小或没有特征可以选择未知。最后得到一个决策树。

1.3、算法流程

输入:训练数据集D,特征集A,阈值ϵ

输出:决策树T

1.4、ID3算法的缺点

  1. ID3 没有剪枝策略,容易过拟合
  2. 信息增益准则对可取值数目较多的特征有所偏好
  3. 只能用于处理离散分布的特征
  4. 没有考虑缺失值

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ID3算法是一种决策树学习算法,用于分类和预测。下面是Python实现ID3算法的基本步骤: 1. 导入相应的库: ``` import pandas as pd import numpy as np import math ``` 2. 定义一个函数来计算数据集的熵: ``` def entropy(target_col): elements, counts = np.unique(target_col, return_counts=True) entropy = np.sum( [(-counts[i]/np.sum(counts)) * np.log2(counts[i]/np.sum(counts)) for i in range(len(elements))] ) return entropy ``` 3. 定义一个函数来计算数据集中每个特征的信息增益: ``` def info_gain(data, split_attribute_name, target_name="class"): total_ent = entropy(data[target_name]) vals, counts = np.unique(data[split_attribute_name], return_counts=True) weighted_ent = np.sum( [(counts[i]/np.sum(counts)) * entropy(data.where(data[split_attribute_name] == vals[i]).dropna()[target_name]) for i in range(len(vals))] ) info_gain = total_ent - weighted_ent return info_gain ``` 4. 定义一个函数来选择具有最高信息增益的特征: ``` def ID3(data, original_data, features, target_attribute_name="class", parent_node_class=None): if len(np.unique(data[target_attribute_name])) <= 1: return np.unique(data[target_attribute_name])[0] elif len(data) == 0: return np.unique(original_data[target_attribute_name])[ np.argmax(np.unique(original_data[target_attribute_name], return_counts=True)[1])] elif len(features) == 0: return parent_node_class else: parent_node_class = np.unique(data[target_attribute_name])[ np.argmax(np.unique(data[target_attribute_name], return_counts=True)[1])] item_values = [info_gain(data, feature, target_attribute_name) for feature in features] best_feature_index = np.argmax(item_values) best_feature = features[best_feature_index] tree = {best_feature: {}} features = [i for i in features if i != best_feature] for value in np.unique(data[best_feature]): sub_data = data.where(data[best_feature] == value).dropna() subtree = ID3(sub_data, original_data, features, target_attribute_name, parent_node_class) tree[best_feature][value] = subtree return tree ``` 5. 最后,我们使用训练数据来构建一棵决策树: ``` def train_test_split(dataset, split_ratio=0.8): train_size = int(split_ratio * len(dataset)) train_set = dataset.sample(train_size) test_set = dataset.drop(train_set.index) return train_set.reset_index(drop=True), test_set.reset_index(drop=True) data = pd.read_csv("dataset.csv") train_data, test_data = train_test_split(data) tree = ID3(train_data, train_data, train_data.columns[:-1]) ``` 这样我们就可以得到一个基本的ID3算法实现。需要注意的是,这个实现并不包括一些优化技巧,例如剪枝等,因此在实际应用中可能需要进行一定的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NDLilaco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值