text_html_output_1.png
from sklearn.metrics import mean_squared_error

from xgboost import XGBRegressor

import pandas as pd

df_train = pd.read_csv('../input/tabulardata-kfolds-created/train_folds.csv')
df_test = pd.read_csv('../input/tabular-playground-series-aug-2021/test.csv')
sample_submission = pd.read_csv('../input/tabular-playground-series-aug-2021/sample_submission.csv')

useful_features = [c for c in df_train.columns if c not in ('id', 'loss', 'kfold')]
df_train[useful_features]

from xgboost import XGBRegressor
import numpy as np
final_preds = []
for fold in range(5):
    xtrain = df_train[df_train.kfold != fold]
    xvalid = df_train[df_train.kfold == fold]
    ytrain = xtrain['loss']
    xtrain = xtrain[useful_features]
    yvalid = xvalid['loss']
    xvalid = xvalid[useful_features]
    model = XGBRegressor(n_estimators=500, random_state=fold)
    model.fit(xtrain, ytrain, early_stopping_rounds=5, eval_set=[(xvalid, yvalid)], verbose=False)
    preds_valid = model.predict(xvalid)
    test = df_test[useful_features]
    test_preds = model.predict(test)
    final_preds.append(test_preds)
    print(mean_squared_error(yvalid, preds_valid, squared=False))