adaboost算法java代码_04-04 AdaBoost算法代码(鸢尾花分类)

本文通过Python的sklearn库实现Adaboost算法,并以鸢尾花数据集为例进行分类。展示了不同参数如n_estimators(弱学习器数量)和learning_rate(学习率)对模型性能的影响,强调了合理选择参数对避免过拟合的重要性。
摘要由CSDN通过智能技术生成

[TOC] 更新、更全的《机器学习》的更新网站,更有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()

![png](http://www.chenyoude.com/ml/04-04 AdaBoost算法代码(鸢尾花分类)_11_0.png?x-oss-process=style/watermark)

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的时候其实算法就已经收敛了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值