text_plain_output_1.png
from sklearn.metrics import mean_squared_error

from sklearn.model_selection import KFold

from sklearn.preprocessing import OrdinalEncoder

from xgboost import XGBRegressor

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df_train = pd.read_csv('../input/30-days-of-ml/train.csv')
df_test = pd.read_csv('../input/30-days-of-ml/test.csv')

kf = KFold(shuffle=True, random_state=5)

cat_cols = [c for c in df_train.columns if 'cat' in c]

ordinal_encoder = OrdinalEncoder()
ordinal_encoder.fit(df_train.loc[:, cat_cols])
def transform_categorical(df, oe=ordinal_encoder):
    cat_cols = [c for c in df.columns if 'cat' in c]
    df[cat_cols] = oe.transform(df.loc[:, cat_cols])
    return df

df_train = transform_categorical(df_train)
df_test = transform_categorical(df_test)

preds = []
for fold, (train_indices, validation_indices) in enumerate(kf.split(df_train)):
    xtrain = df_train.iloc[train_indices]
    xvalid = df_train.iloc[validation_indices]
    ytrain = xtrain.target
    yvalid = xvalid.target
    xtrain = xtrain.drop(columns=['target'])
    xvalid = xvalid.drop(columns=['target'])
    model = XGBRegressor(random_state=fold, verbosity=1)
    model.fit(xtrain, ytrain)
    preds_valid = model.predict(xvalid)
    preds_test = model.predict(df_test)
    preds.append(preds_test)
    print(fold, mean_squared_error(yvalid, preds_valid, squared=False))