1. 什么是 stacking
stacking 就是當(dāng)用初始訓(xùn)練數(shù)據(jù)學(xué)習(xí)出若干個(gè)基學(xué)習(xí)器后,將這幾個(gè)學(xué)習(xí)器的預(yù)測(cè)結(jié)果作為新的訓(xùn)練集忘瓦,來(lái)學(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)過(guò) 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è)逃延,來(lái)得到第二層的測(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 來(lái)預(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.reibang.com/p/28f02bb59fe5
也許可以找到你想要的:
[入門問題][TensorFlow][深度學(xué)習(xí)][強(qiáng)化學(xué)習(xí)][神經(jīng)網(wǎng)絡(luò)][機(jī)器學(xué)習(xí)][自然語(yǔ)言處理][聊天機(jī)器人]