詳解 Stacking 的 python 實(shí)現(xiàn)

1. 什么是 stacking

stacking 就是當(dāng)用初始訓(xùn)練數(shù)據(jù)學(xué)習(xí)出若干個(gè)基學(xué)習(xí)器后,將這幾個(gè)學(xué)習(xí)器的預(yù)測(cè)結(jié)果作為新的訓(xùn)練集,來學(xué)習(xí)一個(gè)新的學(xué)習(xí)器。


2. 代碼:

例如我們用 RandomForestClassifier, ExtraTreesClassifier, GradientBoostingClassifier 作為第一層學(xué)習(xí)器:

    # Our level 0 classifiers
    clfs = [
        RandomForestClassifier(n_estimators = n_trees, criterion = 'gini'),
        ExtraTreesClassifier(n_estimators = n_trees * 2, criterion = 'gini'),
        GradientBoostingClassifier(n_estimators = n_trees),
    ]

接著要訓(xùn)練第一層學(xué)習(xí)器,并得到第二層學(xué)習(xí)器所需要的數(shù)據(jù),這里會(huì)用到 k 折交叉驗(yàn)證。

1. 先用初始訓(xùn)練集訓(xùn)練 clf,并得到第二層的訓(xùn)練數(shù)據(jù) blend_train

第 j 個(gè)學(xué)習(xí)器,共經(jīng)過 nfolds 次交叉驗(yàn)證,每一次會(huì)得到當(dāng)前驗(yàn)證集角標(biāo)上的預(yù)測(cè)值,nfolds 之后得到和初始訓(xùn)練集一樣大小的集合:

blend_train[cv_index, j] = clf.predict(X_cv)

2. 再用 clf 對(duì) test 集進(jìn)行預(yù)測(cè),來得到第二層的測(cè)試數(shù)據(jù) blend_test

即每個(gè)第一層學(xué)習(xí)器在每次 fold 時(shí),用學(xué)習(xí)器對(duì)初識(shí)測(cè)試集進(jìn)行預(yù)測(cè),n 次之后,對(duì)所有結(jié)果取平均值,得到第 j 個(gè)學(xué)習(xí)器在測(cè)試集上的預(yù)測(cè)結(jié)果:

blend_test_j[:, i] = clf.predict(X_test)
blend_test[:, j] = blend_test_j.mean(1)

這樣第一層的每個(gè)學(xué)習(xí)器,都會(huì)得到一列訓(xùn)練數(shù)據(jù)和一列測(cè)試數(shù)據(jù)為第二層的學(xué)習(xí)器所用。

    # For each classifier, we train the number of fold times (=len(skf))
    for j, clf in enumerate(clfs):
        print 'Training classifier [%s]' % (j)
        blend_test_j = np.zeros((X_test.shape[0], len(skf))) # Number of testing data x Number of folds , we will take the mean of the predictions later
        for i, (train_index, cv_index) in enumerate(skf):
            print 'Fold [%s]' % (i)
            
            # This is the training and validation set
            X_train = X_dev[train_index]
            Y_train = Y_dev[train_index]
            X_cv = X_dev[cv_index]
            Y_cv = Y_dev[cv_index]
            
            clf.fit(X_train, Y_train)
            
            # This output will be the basis for our blended classifier to train against,
            # which is also the output of our classifiers
            blend_train[cv_index, j] = clf.predict(X_cv)
            blend_test_j[:, i] = clf.predict(X_test)
        # Take the mean of the predictions of the cross validation set
        blend_test[:, j] = blend_test_j.mean(1)

3. 接著用 blend_train, Y_dev 去訓(xùn)練第二層的學(xué)習(xí)器 LogisticRegression

    # Start blending!
    bclf = LogisticRegression()
    bclf.fit(blend_train, Y_dev)

blend_train = np.zeros((X_dev.shape[0], len(clfs))),這個(gè)集合是有幾個(gè)學(xué)習(xí)器就有幾列:

4. 再用 bclf 來預(yù)測(cè)測(cè)試集 blend_test,并得到 score:

    # Predict now
    Y_test_predict = bclf.predict(blend_test)
    score = metrics.accuracy_score(Y_test, Y_test_predict)
    print 'Accuracy = %s' % (score)

blend_test = np.zeros((X_test.shape[0], len(clfs))),也是有幾個(gè)學(xué)習(xí)器就有幾列:

整體流程簡(jiǎn)圖如下:


學(xué)習(xí)資料:
https://zhuanlan.zhihu.com/p/25836678
http://www.chioka.in/stacking-blending-and-stacked-generalization/


推薦閱讀 歷史技術(shù)博文鏈接匯總
http://www.lxweimin.com/p/28f02bb59fe5
也許可以找到你想要的:
[入門問題][TensorFlow][深度學(xué)習(xí)][強(qiáng)化學(xué)習(xí)][神經(jīng)網(wǎng)絡(luò)][機(jī)器學(xué)習(xí)][自然語言處理][聊天機(jī)器人]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容