更新、更全的《机器学习》的更新网站,更有python、go、数据结构与算法、爬虫、人工智能教学等着你:https://www.cnblogs.com/nickchen121/p/11686958.html

AdaBoost算法代码(鸢尾花分类)

一、导入模块

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.font_manager import FontProperties
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')

二、导入数据

X = iris_data.data[:, [2, 3]]
y = iris_data.target
label_list = ['山鸢尾', '杂色鸢尾', '维吉尼亚鸢尾']

三、构造决策边界

def plot_decision_regions(X, y, classifier=None):
marker_list = ['o', 'x', 's']
color_list = ['r', 'b', 'g']
cmap = ListedColormap(color_list[:len(np.unique(y))]) x1_min, x1_max = X[:, 0].min()-1, X[:, 0].max()+1
x2_min, x2_max = X[:, 1].min()-1, X[:, 1].max()+1
t1 = np.linspace(x1_min, x1_max, 666)
t2 = np.linspace(x2_min, x2_max, 666) x1, x2 = np.meshgrid(t1, t2)
y_hat = classifier.predict(np.array([x1.ravel(), x2.ravel()]).T)
y_hat = y_hat.reshape(x1.shape)
plt.contourf(x1, x2, y_hat, alpha=0.2, cmap=cmap)
plt.xlim(x1_min, x1_max)
plt.ylim(x2_min, x2_max) for ind, clas in enumerate(np.unique(y)):
plt.scatter(X[y == clas, 0], X[y == clas, 1], alpha=0.8, s=50,
c=color_list[ind], marker=marker_list[ind], label=label_list[clas])

四、训练模型

4.1 训练模型(n_e=10, l_r=0.8)

adbt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),
algorithm="SAMME", n_estimators=10, learning_rate=0.8)
adbt.fit(X, y)
AdaBoostClassifier(algorithm='SAMME',
base_estimator=DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=2,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=5, min_samples_split=20,
min_weight_fraction_leaf=0.0, presort=False, random_state=None,
splitter='best'),
learning_rate=0.8, n_estimators=10, random_state=None)

4.2 可视化

plot_decision_regions(X, y, classifier=adbt)
plt.xlabel('花瓣长度(cm)', fontproperties=font)
plt.ylabel('花瓣宽度(cm)', fontproperties=font)
plt.title('AdaBoost算法代码(鸢尾花分类, n_e=10, l_r=0.8)',
fontproperties=font, fontsize=20)
plt.legend(prop=font)
plt.show()

print("Score:{}".format(adbt.score(X, y)))
Score:0.9866666666666667

4.3 训练模型(n_estimators=300, learning_rate=0.8)

adbt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),
algorithm="SAMME", n_estimators=300, learning_rate=0.8)
adbt.fit(X, y)
print("Score:{}".format(adbt.score(X, y)))
Score:0.9933333333333333

由于样本太少,可能效果不明显,但是对比上一个模型可以发现,相同步长的情况下,如果弱学习个数越多,拟合效果越好,但如果过多则可能过拟合。

4.4 训练模型(n_estimators=300, learning_rate=0.5)

adbt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),
algorithm="SAMME", n_estimators=300, learning_rate=0.001)
adbt.fit(X, y)
print("Score:{}".format(adbt.score(X, y)))
Score:0.9533333333333334

相同迭代次数的情况下,对比上一个模型可以发现,如果步长越大,则模型效果越好。

4.5 训练模型(n_estimators=600, learning_rate=0.7)

adbt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),
algorithm="SAMME", n_estimators=600, learning_rate=0.8)
adbt.fit(X, y)
print("Score:{}".format(adbt.score(X, y)))
Score:0.9933333333333333

对比第二个模型,可以发现即使增加迭代次数,算法准确率也没有提高,所以n_estimators=300的时候其实算法就已经收敛了。

04-04 AdaBoost算法代码(鸢尾花分类)的更多相关文章

  1. [Python]基于K-Nearest Neighbors[K-NN]算法的鸢尾花分类问题解决方案

    看了原理,总觉得需要用具体问题实现一下机器学习算法的模型,才算学习深刻.而写此博文的目的是,网上关于K-NN解决此问题的博文很多,但大都是调用Python高级库实现,尤其不利于初级学习者本人对模型的理 ...

  2. Spark ML下实现的多分类adaboost+naivebayes算法在文本分类上的应用

    1. Naive Bayes算法 朴素贝叶斯算法算是生成模型中一个最经典的分类算法之一了,常用的有Bernoulli和Multinomial两种.在文本分类上经常会用到这两种方法.在词袋模型中,对于一 ...

  3. AdaBoost 算法-分析波士顿房价数据集

    公号:码农充电站pro 主页:https://codeshellme.github.io 在机器学习算法中,有一种算法叫做集成算法,AdaBoost 算法是集成算法的一种.我们先来看下什么是集成算法. ...

  4. SIGAI机器学习第二十集 AdaBoost算法1

    讲授Boosting算法的原理,AdaBoost算法的基本概念,训练算法,与随机森林的比较,训练误差分析,广义加法模型,指数损失函数,训练算法的推导,弱分类器的选择,样本权重削减,实际应用 AdaBo ...

  5. 集成学习值Adaboost算法原理和代码小结(转载)

    在集成学习原理小结中,我们讲到了集成学习按照个体学习器之间是否存在依赖关系可以分为两类: 第一个是个体学习器之间存在强依赖关系: 另一类是个体学习器之间不存在强依赖关系. 前者的代表算法就是提升(bo ...

  6. 【图像处理】Haar Adaboost 检测自定义目标(视频车辆检测算法代码)

    阅读须知 本博客涉及到的资源: 正样本:http://download.csdn.net/detail/zhuangxiaobin/7326197 负样本:http://download.csdn.n ...

  7. Adaboost算法及其代码实现

    . . Adaboost算法及其代码实现 算法概述 AdaBoost(adaptive boosting),即自适应提升算法. Boosting 是一类算法的总称,这类算法的特点是通过训练若干弱分类器 ...

  8. 【AdaBoost算法】积分图代码实现

    一.积分图介绍 定义:图像左上方的像素点值的和: 在Adaboost算法中可用于加速计算Haar或MB-LBP特征值,如下图: 二.代码实现 #include <opencv/highgui.h ...

  9. 02-19 k近邻算法(鸢尾花分类)

    [TOC] 更新.更全的<机器学习>的更新网站,更有python.go.数据结构与算法.爬虫.人工智能教学等着你:https://www.cnblogs.com/nickchen121/ ...

随机推荐

  1. 网页去重之Simhash算法

    Simhash算法是Google应用在网页去重中的一个常用算法,在开始讲解Simhash之前,先了解——什么是网页去重?为什么要进行网页去重?如何进行网页去重,其基本框架是什么?   网页去重,顾名思 ...

  2. css3-旋转的太极图

    123 body { background-color: #aaa; } .div { width: 400px; height: 400px; border-radius: 50%; border: ...

  3. Maven学习归纳(三)——依赖添加依赖排除与项目整合

    一.Maven的坐标 1. 坐标的定义 数学意义上的坐标可以是平面上的(x,y)也可以是空间上的(x,y,z),都可以确定一个质点的位置和方向. Maven中有很多构件,为了能够自动化解析任何一个构件 ...

  4. centos7安装mongodb以及使用

    https://blog.csdn.net/sun007700/article/details/100671570

  5. PLC与上位机的socket通讯——ABB机器人程序(三)

    源程序:https://github.com/935094505/ABB-socket-communication 程序范例 觉得有帮助,别忘了打赏下

  6. [DE] How to learn Big Data

    打开一瞧:50G的文件! emptystacks jobstacks jobtickets stackrequests worker 大数据加数据分析,需要以python+scikit,sql作为基础 ...

  7. 谁是狸猫谁是太子?--戏说java构造器

    故事背景 <狸猫换太子>在我国民间文学中很出名,故事剧情大致如下:北宋第三位皇帝宋真宗赵恒年长无子,他的两个妃子刘妃与李妃同时怀了身孕.真宗召见二人,各赐信物,并声明哪个生了儿子就立谁为皇 ...

  8. ZooKeeper的ACL实现源码阅读

    什么是ACL(Access Control List) zookeeper在分布式系统中承担中间件的作用,它管理的每一个节点上可能都存储这重要的信息,因为应用可以读取到任意节点,这就可能造成安全问题, ...

  9. Splitting into digits CodeForce#1104A

    题目链接:Splitting into digits 题目原文 Vasya has his favourite number 

  10. Kubernetes的Secret对象的使用

    Secret可以想要访问的加密数据,存放到Etcd中,Pod可以通过的Volume的方式,访问到Secret保存的信息 ,当数据修改的时候,Pod挂载的Secret文件也会被修改 一.创建Secret ...