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

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

X_train = df_train.drop('target', axis=1)
y_train = df_train.target
X_test = df_test.copy()

cat_col = X_train.select_dtypes(include='object').columns
num_col = X_train.select_dtypes(include='float64').columns
encoder = OrdinalEncoder()
X_train[cat_col] = encoder.fit_transform(X_train[cat_col])
X_test[cat_col] = encoder.transform(X_test[cat_col])

n_splits = 5
kf = KFold(n_splits, shuffle=True, random_state=0)
pred_test = 0
for fold, (train_indx, valid_indx) in enumerate(kf.split(X_train)):
    X_train_fold = X_train.iloc[train_indx]
    y_train_fold = y_train.iloc[train_indx]
    X_valid_fold = X_train.iloc[valid_indx]
    y_valid_fold = y_train.iloc[valid_indx]
    model = XGBRegressor(tree_method='gpu_hist')
    model.fit(X_train_fold, y_train_fold, verbose=False)
    pred_valid_fold = model.predict(X_valid_fold)
    RMSE_fold = mean_squared_error(pred_valid_fold, y_valid_fold, squared=False)
    pred_test_fold = model.predict(X_test)
    pred_test += pred_test_fold / n_splits

pd.Series(data=model.feature_importances_, index=X_train.columns).sort_values(ascending=False).plot.bar()