text_plain_output_1.png
from sklearn.preprocessing import OneHotEncoder

from xgboost import XGBRegressor

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

train = pd.read_csv('/kaggle/input/home-data-for-ml-course/train.csv')
test = pd.read_csv('/kaggle/input/home-data-for-ml-course/train.csv')

numerical_cols = [col for col in X_train_full.columns if X_train_full[col].dtype == 'int64' or X_train_full[col].dtype == 'float64']

categorical_cols_prev = [col for col in X_train_full.columns if X_train_full[col].dtype == 'object' and X_train_full[col].nunique() > 4]
categorical_cols = []
for col in categorical_cols_prev:
    if set(list(test[col].unique())).issubset(set(list(X_train_full[col].unique()))):
        categorical_cols.append(col)

X_train = X_train_full[numerical_cols + categorical_cols]
X_valid = X_valid_full[numerical_cols + categorical_cols]
X_test = test[numerical_cols + categorical_cols]

from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_X_train = pd.DataFrame(encoder.fit_transform(X_train[categorical_cols]))
OH_X_valid = pd.DataFrame(encoder.transform(X_valid[categorical_cols]))
OH_X_test = pd.DataFrame(encoder.transform(X_test[categorical_cols]))
OH_X_train.index = X_train.index
OH_X_valid.index = X_valid.index
OH_X_test.index = X_test.index
X_train_wna = X_train.drop(categorical_cols, axis=1)
X_valid_wna = X_valid.drop(categorical_cols, axis=1)
X_test_wna = X_test.drop(categorical_cols, axis=1)
X_train = pd.concat((X_train_wna, OH_X_train), axis=1)
X_valid = pd.concat((X_valid_wna, OH_X_valid), axis=1)
X_test = pd.concat((X_test_wna, OH_X_test), axis=1)

from xgboost import XGBRegressor
model = XGBRegressor(learning_rate=0.05, n_estimators=1000)
model.fit(X_train, y_train, early_stopping_rounds=5, eval_set=[(X_valid, y_valid)], eval_metric='mae')