text_html_output_1.png text_plain_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.impute import SimpleImputer

from sklearn.metrics import mean_squared_error

from sklearn.pipeline import Pipeline

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('../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]
num_cols = [col for col in features.columns if features[col].dtype in ('int64', 'float64')]
X = features.copy()
X_test = test[features.columns].copy()

numerical_transformer = SimpleImputer(strategy='constant')
categorical_transformer = Pipeline(steps=[('imp', SimpleImputer(strategy='most_frequent')), ('OHen', OneHotEncoder(handle_unknown='ignore', sparse=False))])
processor = ColumnTransformer(transformers=[('num', numerical_transformer, num_cols), ('cat', categorical_transformer, object_cols)])
my_pipeline = Pipeline(steps=[('processor', processor)])
X_pre = pd.DataFrame(my_pipeline.fit_transform(X))
X_pre.index = X.index
X_test_pre = pd.DataFrame(my_pipeline.transform(X_test))
X_test_pre.index = X_test.index

model = XGBRegressor(n_estimators=5000, n_jobs=4, learning_rate=0.005, max_depth=5, colsample_bytree=0.5, tree_method='hist', random_state=0)
model.fit(X_train, y_train, early_stopping_rounds=5, eval_set=[(X_valid, y_valid)], eval_metric='rmse', verbose=False)
preds_valid = model.predict(X_valid)

predictions = model.predict(X_test_pre)
print(predictions)
output = pd.DataFrame({'Id': X_test_pre.index, 'target': predictions})
output.to_csv('submission.csv', index=False)