内容提要:
* 习题 8.1 详解
* 习题 8.2 详解
点击蓝字 |关注我们
习题 8.1 详解
「习题 8.1」 某公司招聘职员考查身体,业务能力,发展潜力这 3 项。身体分为合格 1,不合格 0 两级,业务能力和发展潜力分为上 1,中 2,下 3 三级。分类为合格 1,不合格 两类。已知 10 个人的数据,如表 8.5 所示。假设弱分类器为决策树桩。试用 AdaBoost 算法学习一个强分类器。
AdaBoost 算法
输入:训练数据集 ,其中 ;弱学习算法;
输出:最终分类器 。
(1)初始化训练数据的权值分布
(2)对
(a)使用具有权值分布 的训练数据集学习,得到基本分类器
(b)计算 在训练数据集上的分类误差率
(c)计算 的系数
这里的对数是自然对数。
(d)更新训练数据集的权值分布
这里, 是规范化因子
它使 成为一个概率分布。
(3)构建基本分类器的线性组合
得到最终分类器
Python 代码
按照 AdaBoost 算法写出 Python 代码,并且输出每一轮的弱分类器、误差率、分类器系数、权重分布、预测结果和准确率,清晰地观察每一轮的预测结果以及模型的性能变化,设置准确率 100% 作为停止条件,代码如下。
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# 特征:身体(0或1),业务能力(1,2,3),发展潜力(1,2,3)
X = np.array([[0, 1, 3],
[0, 3, 1],
[1, 2, 2],
[1, 1, 3],
[1, 2, 3],
[0, 1, 2],
[1, 1, 2],
[1, 1, 1],
[1, 3, 1],
[0, 2, 1]
])
# 标签:合格(1),不合格(-1)
y = np.array([-1, -1, -1, -1, -1, -1, 1, 1, -1, -1])
N = len(y) # 样本数量
# 初始化样本权重
w = np.ones(N) / N
M = 50# 最大迭代轮数
accuracy_threshold = 1.0# 设定准确率阈值
# 存储每轮的分类器、系数
classifiers = []
classifier_weights = []
for m in range(M):
# 训练一个决策树桩(最大深度=1)
stump = DecisionTreeClassifier(max_depth=1)
stump.fit(X, y, sample_weight=w)
y_pred = stump.predict(X)
# 计算分类误差率
err_m = np.sum(w * (y_pred != y)) / np.sum(w)
# 计算弱分类器权重
alpha_m = 0.5 * np.log((1 - err_m) / max(err_m, 1e-10))
# 更新样本权重
w *= np.exp(-alpha_m * y * y_pred)
w /= np.sum(w) # 归一化
# 存储分类器
classifiers.append(stump)
classifier_weights.append(alpha_m)
# 计算当前强分类器的预测结果
f_x = np.sum([alpha * clf.predict(X) for alpha, clf in zip(classifier_weights, classifiers)], axis=0)
G_x = np.sign(f_x)
accuracy = accuracy_score(y, G_x)
# 输出每一轮的信息
print(f"Round {m + 1}:")
print(f"Weak Classifier feature: {stump.tree_.feature[0]}")
print(f"Weak Classifier split_point: {stump.tree_.threshold[0]:.4f}")
print(f"Error: {err_m:.4f}")
print(f"Alpha: {alpha_m:.4f}")
print(f"Updated Weights: {w.round(4)}")
print(f"Current Predictions: {G_x}")
print(f"Current Accuracy: {accuracy:.4f}\n")
# 如果达到设定的准确率阈值,则提前终止训练
if accuracy >= accuracy_threshold:
print(f"Stopping early at round {m + 1} because accuracy ({accuracy_threshold * 100:.2f}%) "
f"has reached the threshold ({accuracy_threshold * 100:.2f}%).")
break
# 最终强分类器的数学表达式
final_expr = "G(x) = sign(" + " + ".join([f"{alpha:.4f} * G_{i+1}(x)"for i, alpha in enumerate(classifier_weights)]) + ")"
print("final model:")
print(final_expr)
运行代码,得到如下结果。
Round 1:
Weak Classifier feature: 1
Weak Classifier split_point: 1.5000
Error: 0.2000
Alpha: 0.6931
Updated Weights: [0.06250.06250.06250.06250.06250.06250.25 0.25 0.06250.0625]
Current Predictions: [-1.-1.-1.-1.-1.-1.-1.-1.-1.-1.]
Current Accuracy: 0.8000
Round 2:
Weak Classifier feature: 1
Weak Classifier split_point: 1.5000
Error: 0.1875
Alpha: 0.7332
Updated Weights: [0.16670.03850.03850.16670.03850.16670.15380.15380.03850.0385]
Current Predictions: [ 1.-1.-1.1.-1.1.1.1.-1.-1.]
Current Accuracy: 0.7000
Round 3:
Weak Classifier feature: 0
Weak Classifier split_point: 0.5000
Error: 0.2821
Alpha: 0.4672
Updated Weights: [0.11610.02680.06820.29550.06820.11610.10710.10710.06820.0268]
Current Predictions: [-1.-1.-1.1.-1.-1.1.1.-1.-1.]
Current Accuracy: 0.9000
Round 4:
Weak Classifier feature: 2
Weak Classifier split_point: 2.5000
Error: 0.2143
Alpha: 0.6496
Updated Weights: [0.07390.0170.04340.1880.04340.07390.25 0.25 0.04340.017 ]
Current Predictions: [-1.-1.-1.-1.-1.-1.-1.-1.-1.-1.]
Current Accuracy: 0.8000
Round 5:
Weak Classifier feature: 2
Weak Classifier split_point: 2.5000
Error: 0.1947
Alpha: 0.7098
Updated Weights: [0.04590.04380.11140.11670.02690.18970.15520.15520.11140.0438]
Current Predictions: [-1.-1.-1.-1.-1.-1.1.1.-1.-1.]
Current Accuracy: 1.0000
Stopping early at round 5 because accuracy (100.00%) has reached the threshold (100.00%).
final model:
G(x) = sign(0.6931 * G_1(x) + 0.7332 * G_2(x) + 0.4672 * G_3(x) + 0.6496 * G_4(x) + 0.7098 * G_5(x))
以第一轮为例,进行结果说明。
-
Weak Classifier feature: 1 代表第一轮得到的基本分类器 是以第二个特征“业务能力”作为最优特征进行分类; -
Weak Classifier split_point: 1.5000 代表 中的以 1.5 作为最优切分点; -
Error: 0.2000 代表第一轮中的误差率; -
Alpha: 0.6931 代表第一轮中的分类器系数; -
Updated Weights: [0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.25 0.25 0.0625 0.0625] 代表第一轮的权值分布; -
Current Predictions: [-1. -1. -1. -1. -1. -1. -1. -1. -1. -1.] 代表第一轮后的预测结果; -
Current Accuracy: 0.8000 代表第一轮预测的准确率。
Stopping early at round 5 because accuracy (100.00%) has reached the threshold (100.00%).
表示一共进了五轮提升迭代。
final model:
G(x) = sign(0.6931 * G_1(x) + 0.7332 * G_2(x) + 0.4672 * G_3(x) + 0.6496 * G_4(x) + 0.7098 * G_5(x))
表示最终的模型为
习题 8.2 详解
「习题 8.2」 比较支持向量机,AdaBoost,逻辑斯谛回归模型的学习策略与算法。
学习策略
首先对比 3 种模型的学习策略。
优化算法
然后对比 3 种模型的优化算法。
适用场景及其优缺点
最后,列出 3 种模型的适用场景及其优缺点。
欢迎大家关注简博士的B站和公众号,在公众号后台发送“入群”,就可以与小伙伴们一起讨论问题哦。

