text_plain_output_5.png application_vnd.jupyter.stderr_output_2.png text_plain_output_9.png application_vnd.jupyter.stderr_output_4.png application_vnd.jupyter.stderr_output_6.png application_vnd.jupyter.stderr_output_8.png application_vnd.jupyter.stderr_output_10.png text_plain_output_3.png text_plain_output_7.png text_plain_output_1.png text_plain_output_11.png
from sklearn.metrics import mean_squared_error

from sklearn.preprocessing import OrdinalEncoder

from xgboost import XGBRegressor

import pandas as pd

train = pd.read_csv('../input/train10fold/train-folds (1).csv')
test = pd.read_csv('../input/30-days-of-ml/test.csv')
submission_data = pd.read_csv('../input/30-days-of-ml/sample_submission.csv')

features = [col for col in train.columns if col not in ('id', 'target', 'kfold')]
object_cols = [col for col in features if 'cat' in col]
ordinal_encoder = OrdinalEncoder()
xtest = test[features]
xtest = xtest.copy()
xtest[object_cols] = ordinal_encoder.fit_transform(xtest[object_cols])

final_preds = []
for fold in range(5):
    xtrain = train[train.kfold != fold].reset_index(drop=True)
    xvalid = train[train.kfold == fold].reset_index(drop=True)
    ytrain = xtrain.target
    yvalid = xvalid.target
    xtrain = xtrain[features]
    xvalid = xvalid[features]
    xtrain[object_cols] = ordinal_encoder.fit_transform(xtrain[object_cols])
    xvalid[object_cols] = ordinal_encoder.fit_transform(xvalid[object_cols])
    best_params = {'learning_rate': 0.07853392035787837, 'colsample_bytree': 0.170759104940733, 'max_depth': 3, 'reg_lambda': 1.7549293092194938e-05, 'reg_alpha': 14.68267919457715, 'subsample': 0.8031450486786944, 'alpha': 30}
    model = XGBRegressor(objective='reg:squarederror', n_estimators=5000, random_state=0, **best_params)
    model.fit(xtrain, ytrain, early_stopping_rounds=300, eval_set=[(xvalid, yvalid)], verbose=1000)
    preds_valid = model.predict(xvalid)
    test_preds = model.predict(xtest)
    final_preds.append(test_preds)
    print(fold, mean_squared_error(yvalid, preds_valid, squared=False))