application_vnd.jupyter.stderr_output_9.png application_vnd.jupyter.stderr_output_7.png text_plain_output_4.png text_plain_output_10.png text_plain_output_6.png application_vnd.jupyter.stderr_output_3.png application_vnd.jupyter.stderr_output_5.png text_plain_output_8.png text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from sklearn.ensemble import RandomForestRegressor

from sklearn.metrics import mean_squared_error

from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder

from xgboost import XGBRegressor

import pandas as pd

import time

train = pd.read_csv('../input/30days-folds/train_folds.csv', index_col=0)
test = pd.read_csv('../input/30-days-of-ml/test.csv', index_col=0)

sum(train.isnull().sum())

y = train['target']
features = train.drop(['target'], axis=1)

final_predictions = []
ordinal_encoder = OrdinalEncoder()
model = RandomForestRegressor(random_state=1)
for fold in range(5):
    X_test = test.copy()
    X_train = train[train.kfold != fold].reset_index(drop=True)
    X_valid = train[train.kfold == fold].reset_index(drop=True)
    y_train = X_train['target']
    y_valid = X_valid['target']
    X_train.drop(['target', 'kfold'], axis=1, inplace=True)
    X_valid.drop(['target', 'kfold'], axis=1, inplace=True)
    object_cols = [col for col in X_train.columns if X_train[col].dtype == 'object']
    X_train[object_cols] = ordinal_encoder.fit_transform(X_train[object_cols])
    X_valid[object_cols] = ordinal_encoder.transform(X_valid[object_cols])
    X_test[object_cols] = ordinal_encoder.transform(X_test[object_cols])
    model = XGBRegressor(random_state=fold, n_jobs=4)
    start_time = time.time()
    model.fit(X_train, y_train)
    end_time = time.time()
    preds_valid = model.predict(X_valid)
    preds_test = model.predict(X_test)
    final_predictions.append(preds_test)
    print(fold, round(mean_squared_error(y_valid, preds_valid, squared=False), 4), end_time - start_time, sep=' - ')