text_html_output_1.png
from sklearn.ensemble import RandomForestRegressor

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import KFold

from sklearn.preprocessing import OrdinalEncoder

from xgboost import XGBRegressor

import numpy as np

import pandas as pd

import warnings

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

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

object_cols = [col for col in features.columns if 'cat' in col]
X = features.copy()
X_test = test.copy()
ordinal_encoder = OrdinalEncoder()
X[object_cols] = ordinal_encoder.fit_transform(X[object_cols])
X_test[object_cols] = ordinal_encoder.transform(X_test[object_cols])

X.isna().sum()

model = RandomForestRegressor(random_state=1)

model.fit(X_train, y_train)
preds_valid = model.predict(X_valid)

trees = model.get_booster().get_dump()

importance_df = pd.DataFrame({'feature': X.columns, 'importance': model.feature_importances_}).sort_values('importance', ascending=False)

from sklearn.model_selection import KFold
def rmse(a, b):
    return mean_squared_error(a, b, squared=False)
def train_and_evaluate(X_train, y_train, X_valid, y_valid, **params):
    model = XGBRegressor(random_state=42, n_jobs=-1, **params)
    model.fit(X_train, y_train)
    train_rmse = rmse(model.predict(X_train), y_train)
    val_rmse = rmse(model.predict(X_valid), y_valid)
    return (model, train_rmse, val_rmse)

kfold = KFold(n_splits=5)
models = []
for train_idxs, val_idxs in kfold.split(X):
    X_train, y_train = (X.iloc[train_idxs], y.iloc[train_idxs])
    X_valid, y_valid = (X.iloc[val_idxs], y.iloc[val_idxs])
    model, train_rmse, val_rmse = train_and_evaluate(X_train, y_train, X_valid, y_valid, max_depth=4, n_estimators=20)
    models.append(model)
import warnings
warnings.filterwarnings('ignore')

def predict_avg(models, inputs):
    return np.mean([model.predict(inputs) for model in models], axis=0)
preds = predict_avg(models, X)
preds