adaboost实验


import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import AdaBoostRegressor
from sklearn.tree import DecisionTreeRegressor
np.random.seed(0)
# 训练集X为300个0到10之间的随机数
X = np.linspace(0, 10, 1000).reshape(-1,1)
# 定义训练集X的目标变量
y = np.sin(1*X).ravel() + np.sin(2*X).ravel() + np.sin(3* X).ravel()+np.cos(3*X).ravel() +np.random.normal(0, 0.3, X.shape[0])
adbr_1 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), n_estimators=1, random_state=123)
adbr_2 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), n_estimators=10, random_state=123)
adbr_3 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), n_estimators=100, random_state=123)
regr_1 = DecisionTreeRegressor(max_depth=4)

# 拟合上述几个模型
adbr_1.fit(X, y)
adbr_2.fit(X, y)
adbr_3.fit(X, y)
regr_1.fit(X, y)

# 读取各个模型的最大迭代次数
adbr_1_n_estimators = adbr_1.get_params(True)['n_estimators']
adbr_2_n_estimators = adbr_2.get_params(True)['n_estimators']
adbr_3_n_estimators = adbr_3.get_params(True)['n_estimators']

# 预测
y_1 = adbr_1.predict(X)
y_2 = adbr_2.predict(X)
y_3 = adbr_3.predict(X)
y_4 = regr_1.predict(X)

# 画出各个模型的回归拟合效果
plt.figure(figsize=(10, 6))
# 画出训练数据集(用黑色表示)
plt.scatter(X, y, c="k", s=10, label="Training Samples")
# 画出adbr_1模型(最大迭代次数为1)的拟合效果(用红色表示)
plt.plot(X, y_1, c="r", label="n_estimators=%d" % adbr_1_n_estimators, linewidth=2)
# 画出adbr_2模型(最大迭代次数为10)的拟合效果(用绿色表示)
plt.plot(X, y_2, c="y", label="n_estimators=%d" % adbr_2_n_estimators, linewidth=2)
# 画出adbr_3模型(最大迭代次数为100)的拟合效果(用蓝色表示)
plt.plot(X, y_3, c="b", label="n_estimators=%d" % adbr_3_n_estimators, linewidth=2)

plt.plot(X, y_4, c='g', label="DecisionTree")

plt.xlabel("data")
plt.ylabel("target")
plt.title("AdaBoost_Regressor Comparison with different n_estimators when max_depth=3")
plt.legend()
plt.show()
# 拟合不同基学习器深度的回归模型
adbr_4 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=8), n_estimators=100, random_state=123)
adbr_5 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=9), n_estimators=100, random_state=123)
adbr_6 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=10), n_estimators=100, random_state=123)

# 拟合上述3个模型
adbr_4.fit(X, y)
adbr_5.fit(X, y)
adbr_6.fit(X, y)

# 预测
y_4 = adbr_4.predict(X)
y_5 = adbr_5.predict(X)
y_6 = adbr_6.predict(X)

# 画出各个模型的回归拟合效果
plt.figure(figsize=(10, 6))
# 画出训练数据集(用黑色表示)
plt.scatter(X, y, c="k", s=10, label="Training Samples")
# 画出adbr_4模型(基学习器深度为3)的拟合效果(用红色表示)
plt.plot(X, y_4, c="r", label="max_depth=4" , linewidth=2)
# 画出adbr_5模型(基学习器深度为4)的拟合效果(用绿色表示)
plt.plot(X, y_5, c="g", label="max_depth=5" , linewidth=2)
# 画出adbr_6模型(基学习器深度为5)的拟合效果(用蓝色表示)
plt.plot(X, y_6, c="b", label="max_depth=6" , linewidth=2)

plt.xlabel("data")
plt.ylabel("target")
plt.title("AdaBoost_Regressor Comparison with different max_depth when n_estimators=100")
plt.legend()
plt.show()

对于adaboost算法的实现,我们需要做以下的操作:
1.先导入各种sklearn中各种各样的包:数据集的包(numpy);画图的包(matplotlib.(pyplot));adboost中的包(从sklearn.ensemble中导入adaboostRegressor);需要用到决策树的包(sklearn.tree中导入decisionTreeRegressor)
sklearn里使用AdaBoostRegressor类对AdaBoost回归算法进行实现
2.np.linspace()函数的使用:def linspace(开始数, 结束数, 数据个数, endpoint=True, retstep=False, dtype=None)。从中我们得出训练集的随机数。
3.numpy中的ravel函数的作用是让多维数组变成一维数组。此次实验中将y转化为一维的。
4.dbr_1 = AdaBoostRegressor(): 固定基学习器最大深度 定义不同迭代次数的AdaBoost回归器模型 最大深度为4,调节迭代次数分别为1、10和100
5.regr_1 = DecisionTreeRegressor(max_depth=4):max_depth表示树的最大深度。达到这个数值时,决策树就会停止运算。
6.model.fit( )函数:训练模型
7.get_params()函数:get方法通过params参数实现。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值