提升方法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
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# AI面会问哪些问题? #
24847次浏览 491人参与
# 中国电信笔试 #
31080次浏览 283人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
14147次浏览 209人参与
# 你的实习产出是真实的还是包装的? #
18817次浏览 330人参与
# 如果秋招能重来,我会____ #
96691次浏览 500人参与
# 春招至今,你的战绩如何? #
59982次浏览 543人参与
# 厦门银行科技岗值不值得投 #
7491次浏览 186人参与
# i人适合做什么工作 #
36914次浏览 124人参与
# 我是面试官,请用一句话让我破防 #
79511次浏览 219人参与
# 哪些公司真双非友好? #
69200次浏览 287人参与
# 金三银四,你的春招进行到哪个阶段了? #
21567次浏览 277人参与
# 找AI工作可以去哪些公司? #
7673次浏览 186人参与
# 从事AI岗需要掌握哪些技术栈? #
7676次浏览 251人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
339915次浏览 2165人参与
# 面试尴尬现场 #
220755次浏览 861人参与
# 五一之后,实习真的很难找吗? #
102797次浏览 584人参与
# 你做过最难的笔试是哪家公司 #
30108次浏览 193人参与
# 你小时候最想从事什么职业 #
159840次浏览 2072人参与
# 应届生第一份工资要多少合适 #
20483次浏览 84人参与
# 阿里笔试 #
176460次浏览 1302人参与
# 一张图晒出你司的标语 #
3821次浏览 72人参与
# 面试被问期望薪资时该如何回答 #
382459次浏览 2163人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务