部份內容摘自:http://blog.csdn.net/sun_shengyun/article/details/54289955 html
這裏咱們用一個具體的例子來說解AdaBoostClassifier的使用。算法
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
輸出爲下圖:this
能夠看到數據有些混雜,咱們如今用基於決策樹的Adaboost來作分類擬合。spa
這裏咱們選擇了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的擬合效果仍是不錯的,如今咱們看看擬合分數:
輸出爲:
也就是說擬合訓練集數據的分數還不錯。固然分數高並不必定好,由於可能過擬合。
如今咱們將最大弱分離器個數從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才能達到相似的擬合效果。