【Kaggle】Intermediate Machine Learning(管道+交叉验证)
文章目錄
- 4. Pipelines 管道
- 5. Cross-Validation 交叉驗證
上一篇:【Kaggle】Intermediate Machine Learning(缺失值+文字特征處理)
下一篇:【Kaggle】Intermediate Machine Learning(XGBoost + Data Leakage)
4. Pipelines 管道
該模塊可以把數(shù)據(jù)前處理+建模整合起來
好處:
步驟1: 定義前處理步驟
- 對缺失的數(shù)字?jǐn)?shù)據(jù),進(jìn)行插值
- 對文字特征進(jìn)行one-hot編碼
步驟2: 定義模型
from sklearn.ensemble import RandomForestRegressormodel = RandomForestRegressor(n_estimators=100, random_state=0)步驟3: 創(chuàng)建和評估管道
我們使用Pipeline類來定義將預(yù)處理和建模步驟捆綁在一起的管道。
管道會在生成預(yù)測之前自動對數(shù)據(jù)進(jìn)行預(yù)處理(如果沒有管道,我們必須在進(jìn)行預(yù)測之前先對數(shù)據(jù)進(jìn)行預(yù)處理)。
# Bundle preprocessing and modeling code in a pipeline # 將 前處理管道 + 模型管道,再次疊加形成新管道 my_pipeline = Pipeline(steps=[('preprocessor', preprocessor),('model', model)])# Preprocessing of training data, fit model my_pipeline.fit(X_train, y_train)# Preprocessing of validation data, get predictions preds = my_pipeline.predict(X_valid) # 用定義好的pipeline 對test進(jìn)行預(yù)測,提交,代碼很簡潔,不易出錯 preds_test = my_pipeline.predict(X_test) # Save test predictions to file output = pd.DataFrame({'Id': X_test.index,'SalePrice': preds_test}) output.to_csv('submission.csv', index=False)You advanced 5,020 places on the leaderboard!
Your submission scored 16459.13640, which is an improvement of your previous score of 16619.07644. Great job!
誤差有點提升,哈哈,加油!🚀
5. Cross-Validation 交叉驗證
交叉驗證可以更好的驗證模型,把數(shù)據(jù)分成幾份(Folds),依次選取一份作為驗證集,其余的用來訓(xùn)練,顯然交叉驗證會花費(fèi)更多的時間
如何選擇是否使用:
-
對于較小的數(shù)據(jù)集,不需要太多的計算負(fù)擔(dān),則應(yīng)運(yùn)行交叉驗證
-
對于較大的數(shù)據(jù)集,單個驗證集就足夠了,因為數(shù)據(jù)足夠多了,交叉驗證花費(fèi)的時間成本變大
-
沒有簡單的準(zhǔn)則,如果模型花費(fèi)幾分鐘或更短的時間來運(yùn)行,那就使用交叉驗證吧
-
可以運(yùn)行交叉驗證,看看每個實驗的分?jǐn)?shù)是否接近。如果每個實驗產(chǎn)生相同的結(jié)果,則單個驗證集可能就足夠了
還可以通過 sklearn.model_selection.GridSearchCV 網(wǎng)格式搜索最佳的參數(shù)
上一篇:【Kaggle】Intermediate Machine Learning(缺失值+文字特征處理)
下一篇:【Kaggle】Intermediate Machine Learning(XGBoost + Data Leakage)
總結(jié)
以上是生活随笔為你收集整理的【Kaggle】Intermediate Machine Learning(管道+交叉验证)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 371. 两整数之和(
- 下一篇: LeetCode 84. 柱状图中最大的