AdaBoostClassifier实战
2024-10-01 06:02:45
AdaBoostClassifier实战
部分内容摘自:http://blog.csdn.net/sun_shengyun/article/details/54289955
这里我们用一个具体的例子来讲解AdaBoostClassifier的使用。
- #gnu
- >>> from sklearn.model_selection import cross_val_score
- >>> from sklearn.datasets import load_iris
- >>> from sklearn.ensemble import AdaBoostClassifier
- >>> iris = load_iris() #还是那个数据集
- >>> clf = AdaBoostClassifier(n_estimators=100) #迭代100次
- >>> scores = cross_val_score(clf, iris.data, iris.target) #分类器的精确度
- >>> scores.mean()
- 0.9... #得分比较理想
- #
Methods
decision_function (X) |
Compute the decision function of X . |
fit (X, y[, sample_weight]) |
Build a boosted classifier from the training set (X, y). |
get_params ([deep]) |
Get parameters for this estimator. |
predict (X) |
Predict classes for X. |
predict_log_proba (X) |
Predict class log-probabilities for X. |
predict_proba (X) |
Predict class probabilities for X. |
score (X, y[, sample_weight]) |
Returns the mean accuracy on the given test data and labels. |
set_params (**params) |
Set the parameters of this estimator. |
staged_decision_function (X) |
Compute decision function of X for each boosting iteration. |
staged_predict (X) |
Return staged predictions for X. |
staged_predict_proba (X) |
Predict class probabilities for X. |
staged_score (X, y[, sample_weight]) |
Return staged scores for X, y. |
首先我们载入需要的类库:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_gaussian_quantiles
接着我们生成一些随机数据来做二元分类,如果对如何产生随机数据不熟悉,在另一篇文章机器学习算法的随机数据生成中有比较详细的介绍。
# 生成2维正态分布,生成的数据按分位数分为两类,500个样本,2个样本特征,协方差系数为2
X1, y1 = make_gaussian_quantiles(cov=2.0,n_samples=500, n_features=2,n_classes=2, random_state=1)
# 生成2维正态分布,生成的数据按分位数分为两类,400个样本,2个样本特征均值都为3,协方差系数为2
X2, y2 = make_gaussian_quantiles(mean=(3, 3), cov=1.5,n_samples=400, n_features=2, n_classes=2, random_state=1)
#讲两组数据合成一组数据
X = np.concatenate((X1, X2))
y = np.concatenate((y1, - y2 + 1))
我们通过可视化看看我们的分类数据,它有两个特征,两个输出类别,用颜色区别。
plt.scatter(X[:, 0], X[:, 1], marker='o', c=y)
输出为下图:
可以看到数据有些混杂,我们现在用基于决策树的Adaboost来做分类拟合。
bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5), algorithm="SAMME", n_estimators=200, learning_rate=0.8) bdt.fit(X, y)
这里我们选择了SAMME算法,最多200个弱分类器,步长0.8,在实际运用中你可能需要通过交叉验证调参而选择最好的参数。拟合完了后,我们用网格图来看看它拟合的区域。
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
np.arange(y_min, y_max, 0.02)) Z = bdt.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
plt.scatter(X[:, 0], X[:, 1], marker='o', c=y)
plt.show()
输出的图如下:
从图中可以看出,Adaboost的拟合效果还是不错的,现在我们看看拟合分数:
print "Score:", bdt.score(X,y)
输出为:
也就是说拟合训练集数据的分数还不错。当然分数高并不一定好,因为可能过拟合。
现在我们将最大弱分离器个数从200增加到300。再来看看拟合分数。
bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),
algorithm="SAMME",
n_estimators=300, learning_rate=0.8)
bdt.fit(X, y)
print "Score:", bdt.score(X,y)
此时的输出为:
这印证了我们前面讲的,弱分离器个数越多,则拟合程度越好,当然也越容易过拟合。
现在我们降低步长,将步长从上面的0.8减少到0.5,再来看看拟合分数。
bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),
algorithm="SAMME",
n_estimators=300, learning_rate=0.5)
bdt.fit(X, y)
print "Score:", bdt.score(X,y)
此时的输出为:
可见在同样的弱分类器的个数情况下,如果减少步长,拟合效果会下降。
最后我们看看当弱分类器个数为700,步长为0.7时候的情况:
bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),
algorithm="SAMME",
n_estimators=600, learning_rate=0.7)
bdt.fit(X, y)
print "Score:", bdt.score(X,y)
此时的输出为:
此时的拟合分数和我们最初的300弱分类器,0.8步长的拟合程度相当。也就是说,在我们这个例子中,如果步长从0.8降到0.7,则弱分类器个数要从300增加到700才能达到类似的拟合效果。
最新文章
- .NET Core之Entity Framework Core 你如何创建 DbContext
- xamarin 一般错误解决办法
- Idea安装及简单配置
- Failed opening .rdb for saving: Permission denied
- Android中如何收听特定应用安装成功的广播
- 高性能网站架构设计之缓存篇(3)- Redis 的配置
- [Java面试二]Java基础知识精华部分.
- [转载]BT656/BT601/BT1120协议
- [Javascript] Use Number() to convert to Number if possilbe
- angular+bootstrap分页指令案例
- K3整理
- java表达式陷阱
- mybatis-generato的功能扩展
- sass和compass实战 读书笔记(一)
- 使用静态基类方案让 ASP.NET Core 实现遵循 HATEOAS Restful Web API
- webpack严格模式!!!忽略
- Network - 互联网协议简介
- Docker 为 ASP.NET Core WebApi 应用程序生成 Docker 映像,创建容器并运行
- js弹出层的插件
- codevs 1462 素数和