提升学习简介(AdBoost算法,前向分步算法,梯度提升树)

Boosting

AdBoost算法

在这里插入图片描述

前向分步算法

在这里插入图片描述

提升树(Boosting tree)

在这里插入图片描述

提升树之梯度提升(Gradient Boosting)

在这里插入图片描述

代码实现

数据

#return X,y
def create_data():
    iris = load_iris()
    #创建表
    df=pd.DataFrame(iris.data,columns=iris.feature_names)
    df['label']=iris.target
    df.columns=['sepal length','sepal width','petal length','petal width','label']
    #将表的前一百个数据提取前两个特征X和标签y
    data=np.array(df.iloc[:100,[0,1,-1]])
    #可以改进
    for i in range(len(data)):
        if data[i,-1]==0:
            data[i,-1]=-1
    return data[:,:2],data[:,-1]
X,y=create_data()
#划分数据集,测试集占比20%
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)

#可视化
plt.scatter(X[:50,0],X[:50,1],label='-1')
plt.scatter(X[50:,0],X[50:,1],label='+1')
plt.legend()

在这里插入图片描述

AdaBoost的sklearn实例

from sklearn.ensemble import AdaBoostClassifier
#100个弱学习器,步长为0.5
clf=AdaBoostClassifier(n_estimators=100,learning_rate=0.5)
clf.fit(X_train,y_train)
#打印决策界限
def plot_decision_boundary(x,y,clf):
     ## 1 生成网格数据
    x_min, y_min = x.min(axis = 0) - 1
    x_max, y_max = x.max(axis = 0) + 1
    # 利用一组网格数据求出方程的值,然后把边界画出来。
    x_values, y_values = np.meshgrid(np.arange(x_min, x_max, 0.01),
    np.arange(y_min, y_max, 0.01))
    # 计算出分类器对所有数据点的分类结果 生成网格采样
    mesh_output = clf.predict(np.c_[x_values.ravel(), y_values.ravel()])
    # 数组维度变形  
    mesh_output = mesh_output.reshape(x_values.shape)
    fig, ax = plt.subplots(figsize=(16,10), dpi= 80)
    # 会根据 mesh_output结果自动从 cmap 中选择颜色
    plt.pcolormesh(x_values, y_values, mesh_output, cmap = 'rainbow')
    plt.scatter(x[:, 0], x[:, 1], c = y, s=100, edgecolors ='steelblue' , linewidth = 1, cmap = plt.cm.Spectral)
    plt.xlim(x_values.min(), x_values.max())
    plt.ylim(y_values.min(), y_values.max()
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值