我理解的Kaggle比賽中提升成績主要有3個地方html
以前每次打比賽都只作了前兩部分,最後的模型融合就是簡單的加權平均,對於進階的Stacking方法一直沒嘗試,這幾天摸索了一下仍是把Stacking方法給弄懂了。(本文重點講解Stacking,Bagging和Boosting有不少權威的好教程,因此不詳細介紹)最先的Stacking思想早些年就有論文發表,可是應用Stacking方法到比賽中的相關文章仍是少之甚少,這有兩篇https://mlwave.com/kaggle-ensembling-guide/、HUMAN ENSEMBLE LEARNING講的很棒,可是以前由於理解不到位,有幾處卡住了。在@Wille 的文章如何在 Kaggle 首戰中進入前 10%中Stacking只是做爲一部分提到。所以決定本身寫一篇關於模型融合的文章。本文不涉及到各個算法原理層次的深度,目的在於從宏觀上幫助理解這幾個模型融合方法。python
Bagging算法不用咱們本身實現,隨機森林就是基於Bagging算法的一個典型例子,採用的基分類器是決策樹。R和python都集成好了,直接調用。git
1. 基模型M1,對訓練集train訓練,而後用於預測train和test的標籤列,分別是P1,T1
對於M2和M3,重複相同的工做,這樣也獲得P2,T2,P3,T3。github
2. 分別把P1,P2,P3以及T1,T2,T3合併,獲得一個新的訓練集和測試集train2,test2.web
Stacking本質上就是這麼直接的思路,可是這樣確定是不行的,問題在於P1的獲得是有問題的,用整個訓練集訓練的模型反過來去預測訓練集的標籤,毫無疑問過擬合是很是很是嚴重的,所以如今的問題變成了如何在解決過擬合的前提下獲得P一、P二、P3,這就變成了熟悉的節奏——K折交叉驗證。咱們以2折交叉驗證獲得P1爲例,假設訓練集爲4行3列面試
將其劃分爲2部分算法
用traina訓練模型M1,而後在trainb上進行預測獲得preb3和pred4
在trainb上訓練模型M1,而後在traina上進行預測獲得pred1和pred2
而後把兩個預測集進行拼接
對於測試集T1的獲得,有兩種方法。注意到剛剛是2折交叉驗證,M1至關於訓練了2次,因此一種方法是每一次訓練M1,能夠直接對整個test進行預測,這樣2折交叉驗證後測試集至關於預測了2次,而後對這兩列求平均獲得T1。
或者直接對測試集只用M1預測一次直接獲得T1。
P一、T1獲得以後,P二、T二、P三、T3也就是一樣的方法。理解了2折交叉驗證,對於K折的狀況也就理解也就很是順利了。因此最終的代碼是兩層循環,第一層循環控制基模型的數目,每個基模型要這樣去獲得P1,T1,第二層循環控制的是交叉驗證的次數K,對每個基模型,會訓練K次最後拼接獲得P1,取平均獲得T1。這下再把@Wille博文中的那張圖片放出來就很容易看懂了。app
def get_oof(clf, x_train, y_train, x_test):
oof_train = np.zeros((ntrain,))
oof_test = np.zeros((ntest,))
oof_test_skf = np.empty((NFOLDS, ntest)) #NFOLDS行,ntest列的二維array
for i, (train_index, test_index) in enumerate(kf): #循環NFOLDS次
x_tr = x_train[train_index]
y_tr = y_train[train_index]
x_te = x_train[test_index]
clf.fit(x_tr, y_tr)
oof_train[test_index] = clf.predict(x_te)
oof_test_skf[i, :] = clf.predict(x_test) #固定行填充,循環一次,填充一行
oof_test[:] = oof_test_skf.mean(axis=0) #axis=0,按列求平均,最後保留一行
return oof_train.reshape(-1, 1), oof_test.reshape(-1, 1) #轉置,從一行變爲一列
algorithmList <- c('lda', 'rpart', 'glm', 'knn', 'svmRadial')
stackControl <- trainControl(method="repeatedcv", number=10, repeats=3, savePredictions=TRUE, classProbs=TRUE)
stack.glm <- caretStack(models, method="glm", metric="Accuracy", trControl=stackControl)
nfolds <- 5
glm1 <- h2o.glm(x = x, y = y, family = family,
training_frame = train,
nfolds = nfolds,
fold_assignment = "Modulo",
keep_cross_validation_predictions = TRUE)
gbm1 <- h2o.gbm(x = x, y = y, distribution = "bernoulli",
training_frame = train,
seed = 1,
nfolds = nfolds,
fold_assignment = "Modulo",
keep_cross_validation_predictions = TRUE)
rf1 <- h2o.randomForest(x = x, y = y, # distribution not used for RF
training_frame = train,
seed = 1,
nfolds = nfolds,
fold_assignment = "Modulo",
keep_cross_validation_predictions = TRUE)
dl1 <- h2o.deeplearning(x = x, y = y, distribution = "bernoulli",
training_frame = train,
nfolds = nfolds,
fold_assignment = "Modulo",
keep_cross_validation_predictions = TRUE)
models <- list(glm1, gbm1, rf1, dl1)
metalearner <- "h2o.glm.wrapper"
stack <- h2o.stack(models = models,
response_frame = train[,y],
metalearner = metalearner,
seed = 1,
keep_levelone_data = TRUE)
# Compute test set performance:
perf <- h2o.ensemble_performance(stack, newdata = test)