12Use Scikit Learn's cross_val_predict to do a Out-of-Fold Cross validation as opposed
3to averaging out the scores on each fold.4This **usually** tends to be more stable/reliable compared to within fold average.5This script works forall Scikit Learn models as well as the Scikit Learn APIs of
6XGBoost, LightGBM and Keras.78910"""
11import numpy as np
12import pandas as pd
13from xgboost import XGBRegressor
14from sklearn.metrics import mean_squared_error
15from sklearn.model_selection import cross_val_predict
16# Read Data17print("Reading Dataset...")18train = pd.read_csv("../input/train.csv")19target = np.array(train["target"])20target_log = np.log1p(target)# Log transform target as the evaluation metric uses it21xtrain = np.array(train.iloc[:,2:])22print("Shape of training data: {}".format(np.shape(xtrain)))23# Define Model 24xgb_model = XGBRegressor(max_depth=6, learning_rate=0.1, n_estimators=70,25 min_child_weight=100, subsample=1.0,26 colsample_bytree=0.8, colsample_bylevel=0.8,27 random_state=42, n_jobs=4)28# Make OOF predictions using 5 folds29print("Cross Validating...")30oof_preds_log = cross_val_predict(xgb_model, xtrain, target_log, cv=5,31 n_jobs=1, method="predict")3233# Calculate RMSLE (RMSE of Log(1+y))34cv_rmsle = np.sqrt(mean_squared_error(target_log, oof_preds_log))35print("\nOOF RMSLE Score: {:.4f}".format(cv_rmsle))36373839貼り付け元 <https://www.kaggle.com/adarshchavakula/out-of-fold-oof-model-cross-validation>40コード
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。