AdaBoostClassifier实战

AdaBoostClassifier实战

部份内容摘自:http://blog.csdn.net/sun_shengyun/article/details/54289955   html

 这里咱们用一个具体的例子来说解AdaBoostClassifier的使用。算法

    1. #gnu  
    2. >>> from sklearn.model_selection import cross_val_score  
    3. >>> from sklearn.datasets import load_iris  
    4. >>> from sklearn.ensemble import AdaBoostClassifier  
    5.   
    6. >>> iris = load_iris() #仍是那个数据集  
    7. >>> clf = AdaBoostClassifier(n_estimators=100) #迭代100次  
    8. >>> scores = cross_val_score(clf, iris.data, iris.target) #分类器的精确度  
    9. >>> scores.mean()                               
    10. 0.9...   #得分比较理想  

Methodsdom

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))
复制代码

    咱们经过可视化看看咱们的分类数据,它有两个特征,两个输出类别,用颜色区别。ui

plt.scatter(X[:, 0], X[:, 1], marker= 'o', c=y)

    输出为下图:this

    能够看到数据有些混杂,咱们如今用基于决策树的Adaboost来作分类拟合。spa

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,在实际运用中你可能须要经过交叉验证调参而选择最好的参数。拟合完了后,咱们用网格图来看看它拟合的区域。.net

复制代码
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()
复制代码

    输出的图以下:code

    从图中能够看出,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才能达到相似的拟合效果。
相关文章
相关标签/搜索