Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<import_modules>
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OrdinalEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import KFold
from xgboost import XGBRegressor
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import StandardScaler
from skopt import BayesSearchCV
from skopt.space import Real, Categorical, Integer
import time
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
import math
from datetime import datetime<set_options>
X = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
 X_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')
 X
%matplotlib inline
sns.set({'figure.figsize':(16,8), 'axes.titlesize':15, 'axes.labelsize':10} )<define_variables>
y = X.SalePrice
X.drop(columns=['SalePrice'], axis=1, inplace=True )
FILEPATH = os.path.join(".. ", "input")
 TRAINPATH = os.path.join(FILEPATH, "train.csv")
 TESTPATH = os.path.join(FILEPATH, "test.csv" )<load_from_csv>
X.isnull().sum() , X.nunique() , X.dtypes
df = pd.read_csv(TRAINPATH, index_col=0)
 df.head()<load_from_csv>
cat_cols = [cname for cname in X.columns if
X[cname].dtype == "object"]
num_cols = [cname for cname in X.columns if
X[cname].dtype in ['int64', 'float64']]
df_test = pd.read_csv(TESTPATH)
 df_test.head()<filter>
num_transf = Pipeline(steps=[ 
('imputer', SimpleImputer()), 
('sca', StandardScaler())
 ])
 cat_transf = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', num_transf, num_cols), 
('cat', cat_transf, cat_cols)
 ])
 model = XGBRegressor(tree_method='gpu_hist', use_label_encoder=False, eval_metric='rmse', random_state=7)
 pipe = Pipeline(steps=[('preprocessor', preprocessor), 
('model', model)
 ] )
df = df[df.passenger_count != 0]<count_duplicates>
params = {'model__n_estimators':np.arange(100, 800, 10),'model__learning_rate':np.arange(0.001, 0.2, 0.001),
'model__max_depth':np.arange(3, 12, 1)}
kfold = KFold(n_splits=7, shuffle=True, random_state=7)
 scan = RandomizedSearchCV(pipe, params, n_iter=100, scoring='neg_root_mean_squared_error', cv=kfold, random_state=7)
 result = scan.fit(X, y)
 print("Best: %f using %s" %(-1*result.best_score_, result.best_params_))
df.duplicated().sum()<count_duplicates>
best_parameters = dict(result.best_params_.copy())
 for k in best_parameters.keys() :
best_parameters[k.replace("model__","")] = best_parameters.pop(k)
 best_parameters['reg_alpha'] = best_parameters.pop('model__reg_alpha')
 best_parameters['n_estimators'] = best_parameters.pop('model__n_estimators')
 best_parameters
<count_missing_values><EOS> 
model_scan = XGBRegressor(**best_parameters, eval_metric='rmse', use_label_encoder=False, tree_method='gpu_hist')
 pipe_scan = Pipeline(steps=[('preprocessing', preprocessor),('model', model_scan)])
 pipe_scan.fit(X, y)
 pred_scan = pipe_scan.predict(X_test)
 output = pd.DataFrame({'Id': X_test.index,
'SalePrice': pred_scan})
 output.to_csv('submission.csv', index=False)
 output