决策树的简单信息

ID3算法 ,C4.5 ,CART

ID3 信息论,信息熵 p(X)为概率,0-1范围所以熵永远大于0

H(X)=-\sum_{x\epsilon \chi } p(x)log_{2}p(x)

单调性,发生概率越高的时间,所携带的信息熵越低

非负性

 累加性 两个独立事件同时发生的信息熵H(A,B)=H(A)+H(B)

该算法利用信息增益来决定优先使用哪个特征进行分裂,信息增益越大就进行该特征分类

信息增益 :划分之前的信息熵减去划分之后的信息熵叫信息增益

A对D划分的期望信息为:

inf_{oA}(D)=\sum_{j=1}^{v}\frac{|D_{j}|}{|D|}info(D_{j})

优点ID3复杂度不高

ID3算法对于离散型特征比如多加一类1,2,3...处理不好,引入C4.5算法

C4.5 算信息增益率

CART:gini系数 在decisiontreeclassifier函数里,尽可能分类的数据纯没有杂质

容易过拟合 要剪枝

max_depth 控制树的最大深度

min_sample_split=8 最小分割样本数

min_sample_leaf=4

随机森林 就是 很多树的结果平均,使结果不会过拟合

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息增益决策树是一种常用的机器学习算法,用于分类和回归问题。下面是一个简单信息增益决策树的代码实现示例: ```python import numpy as np class DecisionTree: def __init__(self): self.tree = {} def calc_entropy(self, y): classes, counts = np.unique(y, return_counts=True) probabilities = counts / len(y) entropy = -np.sum(probabilities * np.log2(probabilities)) return entropy def calc_information_gain(self, X, y, feature_idx, threshold): left_mask = X[:, feature_idx] <= threshold right_mask = X[:, feature_idx] > threshold left_entropy = self.calc_entropy(y[left_mask]) right_entropy = self.calc_entropy(y[right_mask]) left_weight = len(y[left_mask]) / len(y) right_weight = len(y[right_mask]) / len(y) information_gain = self.calc_entropy(y) - (left_weight * left_entropy + right_weight * right_entropy) return information_gain def find_best_split(self, X, y): best_feature_idx = None best_threshold = None best_information_gain = -np.inf for feature_idx in range(X.shape): thresholds = np.unique(X[:, feature_idx]) for threshold in thresholds: information_gain = self.calc_information_gain(X, y, feature_idx, threshold) if information_gain > best_information_gain: best_information_gain = information_gain best_feature_idx = feature_idx best_threshold = threshold return best_feature_idx, best_threshold def build_tree(self, X, y): if len(np.unique(y)) == 1: return np.unique(y) best_feature_idx, best_threshold = self.find_best_split(X, y) if best_feature_idx is None or best_threshold is None: return np.argmax(np.bincount(y)) left_mask = X[:, best_feature_idx] <= best_threshold right_mask = X[:, best_feature_idx] > best_threshold left_subtree = self.build_tree(X[left_mask], y[left_mask]) right_subtree = self.build_tree(X[right_mask], y[right_mask]) self.tree = { 'feature_idx': best_feature_idx, 'threshold': best_threshold, 'left': left_subtree, 'right': right_subtree } def fit(self, X, y): self.build_tree(X, y) def predict(self, X): predictions = [] for sample in X: node = self.tree while isinstance(node, dict): if sample[node['feature_idx']] <= node['threshold']: node = node['left'] else: node = node['right'] predictions.append(node) return predictions ``` 这段代码实现了一个简单信息增益决策树,包括计算熵、计算信息增益、寻找最佳分割点、构建决策树、拟合数据和预测等功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值