text_plain_output_1.png
from catboost import CatBoostRegressor

from sklearn.metrics import mean_squared_error

from sklearn.preprocessing import OrdinalEncoder

from xgboost import XGBRegressor

import pandas as pd

df = pd.read_csv('../input/train-folds/train_folds.csv')
test_df = pd.read_csv('../input/30-days-of-ml/test.csv')

test_df.head().T

from xgboost import XGBRegressor
from catboost import CatBoostRegressor
useful_features = [c for c in df.columns if c not in ('id', 'target', 'kfold')]
object_cols = [col for col in useful_features if 'cat' in col]
test_df = test_df[useful_features]
final_predictions = []
for fold in range(5):
    xtrain = df[df.kfold != fold].reset_index(drop=True)
    xvalid = df[df.kfold == fold].reset_index(drop=True)
    xtest = test_df.copy()
    ytrain = xtrain.target
    yvalid = xvalid.target
    xtrain = xtrain[useful_features]
    xvalid = xvalid[useful_features]
    ordinal_encoder = OrdinalEncoder()
    xtrain[object_cols] = ordinal_encoder.fit_transform(xtrain[object_cols])
    xvalid[object_cols] = ordinal_encoder.transform(xvalid[object_cols])
    xtest[object_cols] = ordinal_encoder.transform(xtest[object_cols])
    model = XGBRegressor(random_state=fold, n_jobs=4)
    model.fit(xtrain, ytrain)
    preds_valid = model.predict(xvalid)
    test_preds = model.predict(xtest)
    catboost_model = CatBoostRegressor(random_state=fold, verbose=100)
    catboost_model.fit(xtrain, ytrain)
    cat_preds_valid = catboost_model.predict(xvalid)
    cat_preds_test = model.predict(xtest)
    final_predictions.append(test_preds)
    final_predictions.append(cat_preds_test)
    print('XGB:', fold, mean_squared_error(yvalid, preds_valid, squared=False))
    print('Catboost:', fold, mean_squared_error(yvalid, cat_preds_valid, squared=False))