text_plain_output_1.png image_output_1.png
from sklearn.metrics import mean_squared_error

from sklearn.model_selection import train_test_split, cross_val_score, KFold

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

import xgboost as xgb

import math
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
parameters = {'axes.grid': True}
plt.rcParams.update(parameters)
import optuna
from optuna.samplers import TPESampler
import xgboost as xgb
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error

df_train = pd.read_csv('../input/tabular-playground-series-aug-2021/train.csv')
df_test = pd.read_csv('../input/tabular-playground-series-aug-2021/test.csv')

df_train.drop('id', axis=1, inplace=True)

df_train.isnull().sum().max() == 0

df_test.drop('id', axis=1, inplace=True)

df_test.isnull().sum().max() == 0

# Plotting some graphs of random features in train set 

fig = plt.figure(figsize = (15, 10))
for j in [j for j in range(1, 16)]:
    i = np.random.randint(0, df_train.columns.size - 1)
    plt.subplot(3, 5, j)
    sns.kdeplot(x = df_train[df_train.columns[i]])
    plt.title(df_train.columns[i])
fig.tight_layout()
print('15 graphs of random features in train set')
plt.show()

df_train_no_target = df_train.drop('loss', axis=1)

scaler = StandardScaler()
df_train_no_target_scal = pd.DataFrame(scaler.fit_transform(df_train_no_target), columns=df_train_no_target.columns)
df_test = pd.DataFrame(scaler.fit_transform(df_test), columns=df_test.columns)

X = df_train_no_target_scal
y = df_train['loss']

model = xgb.XGBRegressor(objective='reg:tweedie', tree_method='gpu_hist', predictor='gpu_predictor', sampling_method='gradient_based', max_bin=512, silent=False, random_state=17)
model.fit(X_train, y_train)
preds_test = model.predict(X_test)
preds_train = model.predict(X_train)

def check_model(model=model, n_splits=10):
    scores = []
    cv = KFold(n_splits, shuffle=True)
    for train_idx, test_idx in cv.split(X):
        X_train, y_train = (X.iloc[train_idx], y.iloc[train_idx])
        X_test, y_test = (X.iloc[test_idx], y.iloc[test_idx])
        model.fit(X_train, y_train)
        preds = model.predict(X_test)
        score = np.sqrt(mean_squared_error(y_test, preds))
        scores.append(score)

check_model(model)