application_vnd.jupyter.stderr_output_1.png
from sklearn.metrics import mean_squared_error

from sklearn.preprocessing import OneHotEncoder

from xgboost import XGBRegressor

import optuna

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('/kaggle/input/30-days-of-ml/train.csv', index_col='id')
data_submission = pd.read_csv('/kaggle/input/30-days-of-ml/test.csv', index_col='id')

object_cols = [col for col in data.columns if data[col].dtype == 'object']
num_cols = [col for col in data.columns if data[col].dtype != 'object' and col != 'target']

from sklearn.preprocessing import OneHotEncoder
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(X_train[object_cols]))
OH_cols_valid = pd.DataFrame(OH_encoder.transform(X_valid[object_cols]))
OH_cols_train.index = X_train.index
OH_cols_valid.index = X_valid.index
num_X_train = X_train.drop(object_cols, axis=1)
num_X_valid = X_valid.drop(object_cols, axis=1)
OH_X_train = pd.concat([num_X_train, OH_cols_train], axis=1)
OH_X_valid = pd.concat([num_X_valid, OH_cols_valid], axis=1)

from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error
def optimize_stuff(trial):
    learning_rate = trial.suggest_float('learning_rate', 0.01, 0.25, log=True)
    reg_lambda = trial.suggest_loguniform('reg_lambda', 1e-08, 100.0)
    reg_alpha = trial.suggest_loguniform('reg_alpha', 1e-08, 100.0)
    subsample = trial.suggest_float('subsample', 0.1, 1.0)
    colsample_bytree = trial.suggest_float('colsample_bytree', 0.1, 1.0)
    max_depth = trial.suggest_int('max_depth', 1, 7)
    my_model = XGBRegressor(random_state=1, tree_method='gpu_hist', gpu_id=0, predictor='gpu_predictor', n_estimators=7000, learning_rate=learning_rate, reg_lambda=reg_lambda, reg_alpha=reg_alpha, subsample=subsample, colsample_bytree=colsample_bytree, max_depth=max_depth)
    my_model.fit(OH_X_train, y_train, early_stopping_rounds=300, eval_set=[(OH_X_valid, y_valid)], verbose=False)
    preds_valid = my_model.predict(OH_X_valid)
    rmse = mean_squared_error(y_valid, preds_valid, squared=False)
    return rmse

import optuna
study = optuna.create_study(direction='minimize')
study.optimize(optimize_stuff, n_trials=1000)
best_params = study.best_params