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

相关推荐

HR_丸山彩同学:你的项目描述里,系统设计讲了很多:MemCube是什么、三级存储架构怎么设计、四种遗忘策略分别是什么。这些面试的时候讲没问题,但简历上不需要这么细。 简历要突出的是影响力,不是实现细节。面试官看简历的时候想知道的是「这个项目有多大价值」,不是「这个项目具体怎么实现的」。实现细节是面试时候聊的 怎么改:技术细节可以精简为一句「采用三级存储架构+四种遗忘策略」,把省出来的篇幅用来写影响力。比如:项目有没有开源?有没有写成技术博客?有没有被别人使用过? 校园经历没有任何信息量,任何人都可以写这句话,写了等于没写。更关键的是,你投的是技术岗,校园活动经历本来就不是加分项。如果非要写,必须写出具体的数字和成果。如果你没有这些数字,那就老老实实删掉 「端到端耗时缩减30-40%」要给出确切数字和绝对值。从1000ms降到600ms是降了40%,从100ms降到60ms也是降了40%,但这两个含义完全不一样。其他也是,涉及到数据,准备好证据,口径统一,面试会问 「熟练」「熟悉」「了解」混在一起用,读起来很乱。而且「了解前端需求」最好改成「具备前后端协作经验」
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务