提升方法AdaBoost算法,python完整代码实现!!!!!!,代码详细,好理解。

提升方法AdaBoost算法


前言

提升方法基于这样一种思想:对于一个复杂任务来说,将多个专 家的判断进行适当的综合所得出的判断,要比其中任何一个专家单独 的判断好。实际上,就是“三个臭皮匠顶个诸葛亮”的道理。

一、代码实现

iris数据集共有四个特征,该代码分别采取iris的0,1 和 2,3两个特征,分别用sklearn中的AdaBoostClassifier进行分类,并打分,并可视化。详细代码如下。

from matplotlib import pyplot as plt
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_iris
from sklearn.ensemble import AdaBoostClassifier
import numpy as np
iris = load_iris() #iris数据集

#创建AdaBoostClassifier模型
clf = AdaBoostClassifier(n_estimators=10) #迭代100次
clf_1 = AdaBoostClassifier(n_estimators=10) #迭代100次

#取iris第2,3个特征,便于可视化
X = iris.data[:,0:2]
X_1 = iris.data[:,2:4]
Y = iris.target

#模型输入参数
model =  clf.fit(X , Y)
model_1 =  clf_1.fit(X_1 , Y)

#对模型打分
scores = cross_val_score(clf, X, Y) #分类器的精确度
scores_1 = cross_val_score(clf_1, X_1, Y) #分类器的精确度

# 画图,可视化
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

x_min_1, x_max_1 = X_1[:, 0].min() - 1, X_1[:, 0].max() + 1
y_min_1, y_max_1 = X_1[:, 1].min() - 1, X_1[:, 1].max() + 1

xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
                     np.arange(y_min, y_max, 0.02))

xx_1, yy_1 = np.meshgrid(np.arange(x_min_1, x_max_1, 0.02),
                     np.arange(y_min_1, y_max_1, 0.02))


Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z_1 = model_1.predict(np.c_[xx_1.ravel(), yy_1.ravel()])

Z = Z.reshape(xx.shape)
Z_1 = Z_1.reshape(xx_1.shape)
plt.subplot(2,1,1)
plt.subplots_adjust(wspace = 0.4, hspace = 0.4)
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired ,alpha = 0.8)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.xlabel('SepalLength')
plt.ylabel('Sepalwidth')
# plt.legend(['Setosa','Versicolour','Virginica'],loc='upper right',fontsize=7)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title('Adaboostolassifier_test:')


plt.subplot(2,1,2)
cs_1 = plt.contourf(xx_1, yy_1, Z_1, cmap=plt.cm.Paired ,alpha = 0.8)
plt.scatter(X_1[:, 0], X_1[:, 1], c=Y, cmap=plt.cm.Paired)
plt.xlabel('PetalLength')
plt.ylabel('PetalWidth')
# plt.legend(['Setosa','Versicolour','Virginica'],loc='upper right',fontsize=7)
plt.xlim(xx_1.min(), xx_1.max())
plt.ylim(yy_1.min(), yy_1.max())
plt.xticks(())
plt.yticks(())
plt.title('Adaboostolassifier_test:')
plt.show()
print('iris_attribute_{0}, iris_attribute_{1}     scores:{2}'.format(0,1,scores.mean()))
print('iris_attribute_{0}, iris_attribute_{1}     scores:{2}'.format(2,3,scores_1.mean()))

二、运行结果

在这里插入图片描述

 iris_attribute_0, iris_attribute_1     scores:0.6733333333333333
iris_attribute_2, iris_attribute_3     scores:0.9533333333333334
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值