决策树桩(Decision Stump)

1. 何为决策树桩?

 

单层决策树(decision stump),也称决策树桩,它是一种简单的决策树,通过给定的阈值进行分类。

从实际意义上来看,决策树桩根据一个属性的单个判断(但是实际上待判断的物体具有多个属性)就确定最终的分类结果。这种特性比较适合做集成学习中的弱学习器,因为其至少比随机的效果好一些,又计算较为容易。

2. 关键问题

根本目的:通过选择一个合适的决策树桩(弱学习器),使得物体类别识别准确率尽可能高。

怎么选择一个合适的决策树桩?

  1. 从所有属性中,选择那个属性作为决策树桩(弱学习器)
  2. 该决策树桩的阈值设定为何值(上图中是1.75)?
  3. 是小于阈值识别为1(yes),还是大于阈值识别为1(yes)。一般情况下设定为小于。

3. MATLAB代码实现

 

主函数test主要是数据的加载和决策树桩的调用, 并呈现最佳的决策树桩以及判断错误的概率。

首先看如何在整个数据集上获得最佳的决策树桩,使得判断错误的概

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是基于弱分类器为决策树的AdaBoost算法学习一个强分类器的Python代码实现,注释详细,希望能够帮到你: ```python import numpy as np class DecisionStump(): def __init__(self): self.polarity = 1 self.feature_index = None self.threshold = None self.alpha = None class AdaBoost(): def __init__(self, n_clf=5): self.n_clf = n_clf def fit(self, X, y): n_samples, n_features = X.shape # 初始化权重 w = np.full(n_samples, (1 / n_samples)) self.clfs = [] for _ in range(self.n_clf): clf = DecisionStump() min_error = float('inf') # 遍历每个特征,找到最佳阈值 for feature_i in range(n_features): feature_values = np.expand_dims(X[:, feature_i], axis=1) unique_values = np.unique(feature_values) for threshold in unique_values: p = 1 prediction = np.ones(np.shape(y)) prediction[X[:, feature_i] < threshold] = -1 error = sum(w[y != prediction]) if error > 0.5: error = 1 - error p = -1 if error < min_error: clf.polarity = p clf.threshold = threshold clf.feature_index = feature_i min_error = error # 计算分类器权重 clf.alpha = 0.5 * np.log((1.0 - min_error) / (min_error + 1e-10)) # 更新样本权重 predictions = np.ones(np.shape(y)) negative_idx = (clf.polarity * X[:, clf.feature_index] < clf.polarity * clf.threshold) predictions[negative_idx] = -1 w *= np.exp(-clf.alpha * y * predictions) w /= np.sum(w) # 保存分类器 self.clfs.append(clf) def predict(self, X): n_samples = np.shape(X)[0] y_pred = np.zeros((n_samples, 1)) for clf in self.clfs: predictions = np.ones(np.shape(y_pred)) negative_idx = (clf.polarity * X[:, clf.feature_index] < clf.polarity * clf.threshold) predictions[negative_idx] = -1 y_pred += clf.alpha * predictions y_pred = np.sign(y_pred).flatten() return y_pred.astype(int) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

身影王座

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

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

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

打赏作者

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

抵扣说明:

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

余额充值