Kaggle competition: House Prices - Advanced Regression Techniques
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1 )<count_values>
warnings.filterwarnings("ignore" )
train_data['WheelType'].value_counts()<count_values>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train_size = int(len(train)*0.8)
 train = train.sort_values(['YrSold', 'MoSold'], axis=0 ).reset_index(drop=True)
 train_X = train.drop(['SalePrice', 'Id'], axis=1 ).loc[:train_size-1, :]
valid_X = train.drop(['SalePrice', 'Id'], axis=1 ).loc[train_size:, :]
train_y = train['SalePrice'][:train_size]
valid_y = train['SalePrice'][train_size:]
test_X = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')
 test_X = test_X.sort_values(['YrSold', 'MoSold'], axis=0 )
test_data['WheelType'].value_counts()<data_type_conversions>
class CustomImputer:
def __init__(self):
pass
def fit(self, data):
cat_cols = data.columns[data.dtypes == 'object']
self.impute_cols = cat_cols[data[cat_cols].isna().sum() > 0]
def transform(self, data):
for column in self.impute_cols:
if data[column].isin(['None', 'No', 'Othr'] ).sum() > 0:
replace_value = data.loc[data[column].isin(['None', 'No', 'Othr']), column].unique() [0]
data[column] = data[column].fillna(replace_value)
 else:
data[column] = data[column].fillna('None')
 return data
def fit_transform(self, data):
self.fit(data)
 return self.transform(data)
 def get_params(self):
pass
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )<count_values>
na_cols = train_X.columns[(train_X.isna().sum() > 0 ).values]
print(f'Columns with NA: {na_cols}')
 train_X.loc[:, na_cols] = CustomImputer().fit_transform(train_X.loc[:, na_cols])
 test_X.loc[:, na_cols] = CustomImputer().fit_transform(test_X.loc[:, na_cols])
 valid_X.loc[:, na_cols] = CustomImputer().fit_transform(valid_X.loc[:, na_cols])
 train_X.loc[:, na_cols[train_X.loc[:, na_cols].dtypes == 'float64']] = train_X.loc[:, na_cols[train_X.loc[:, na_cols].dtypes == 'float64']].fillna(0 )
train_data['WheelType'].value_counts()<count_values>
train_X.columns[train_X.isna().sum() > 0]
train_data['Nationality'].value_counts()<count_values>
train_X = train_X.assign(Sold_Lst_Mnth=lambda df: get_sold_last_mnth(df))
valid_X = valid_X.assign(Sold_Lst_Mnth=lambda df: get_sold_last_mnth(df))
test_X = test_X.assign(Sold_Lst_Mnth=lambda df: get_sold_last_mnth(df))
train_X = train_X.assign(FireplacesPerRm=lambda df: get_fireplaces_per_room(df))
valid_X = valid_X.assign(FireplacesPerRm=lambda df: get_fireplaces_per_room(df))
test_X = test_X.assign(FireplacesPerRm=lambda df: get_fireplaces_per_room(df))
train_X = train_X.assign(QualPerRm=lambda df: get_quality_per_room(df))
valid_X = valid_X.assign(QualPerRm=lambda df: get_quality_per_room(df))
test_X = test_X.assign(QualPerRm=lambda df: get_quality_per_room(df))
test_data['Nationality'].value_counts()<data_type_conversions>
ta_leakage_cols = ['YrSold', 'MoSold']
train_X = train_X.drop(ta_leakage_cols, axis=1)
 valid_X = valid_X.drop(ta_leakage_cols, axis=1)
 test_X =test_X.drop(ta_leakage_cols, axis=1 )
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True )<count_values>
cat_features = train_X.columns[train_X.dtypes == 'object']
num_features = train_X.columns[(train_X.dtypes == 'int64')|(train_X.dtypes == 'float64')]
train_data['Size'].value_counts()<count_values>
from sklearn import pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from category_encoders import CatBoostEncoder, OrdinalEncoder
test_data['Size'].value_counts()<data_type_conversions>
ordinal_features = [
'LandSlope',
'Condition1',
'Condition2',
'ExterQual',
'ExterCond',
'BsmtQual',
'BsmtCond',
'BsmtFinType1',
'BsmtFinType2',
'HeatingQC',
'KitchenQual',
'FireplaceQu',
'GarageQual',
'GarageCond',
'PoolQC'
]
nominal_features = cat_features.drop(ordinal_features)
 nominal_transformer = pipeline.Pipeline(steps=[ 
('cat_boost', CatBoostEncoder()), 
('scaler', StandardScaler())
 ])
 num_transformer = pipeline.Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant', fill_value=0, missing_values=np.nan)) , 
('scaler', StandardScaler())
 ])
 num_ord_transformer = ColumnTransformer(transformers=[ 
('ordinal_transformer', OrdinalEncoder() , ordinal_features), 
('num_transformer', num_transformer, num_features)
 ])
 train_X_nominal = nominal_transformer.fit_transform(train_X[nominal_features], train_y)
 valid_X_nominal = nominal_transformer.transform(valid_X[nominal_features])
 test_X_nominal = nominal_transformer.transform(test_X[nominal_features])
 train_X_num_ord = num_ord_transformer.fit_transform(train_X)
 valid_X_num_ord = num_ord_transformer.transform(valid_X)
 test_X_num_ord = num_ord_transformer.transform(test_X)
 train_X_transformed = np.concatenate(( train_X_nominal, train_X_num_ord), axis=1)
 valid_X_transformed = np.concatenate(( valid_X_nominal, valid_X_num_ord), axis=1)
 test_X_transformed = np.concatenate(( test_X_nominal, test_X_num_ord), axis=1 )
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True )<count_values>
from sklearn.model_selection import GridSearchCV, TimeSeriesSplit
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error
train_data['TopThreeAmericanName'].value_counts()<count_values>
params = {
'learning_rate': np.arange(0.01, 0.11, 0.01)
 }
optimizer = GridSearchCV(XGBRegressor(n_estimators=500, tree_method='gpu_hist', max_depth=4, min_child_weight=3, gamma=0, colsample_bytree=0.4, subsample=0.7, learning_rate=0.05), params, cv=TimeSeriesSplit(n_splits=3), n_jobs=-1)
 optimizer.fit(train_X_transformed, train_y)
 valid_score = mean_absolute_error(valid_y, optimizer.predict(valid_X_transformed))
test_data['TopThreeAmericanName'].value_counts()<data_type_conversions>
optimizer.best_params_
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )<count_values>
print(f'Mean validation score is {valid_score}' )
train_data['PRIMEUNIT'].value_counts()<count_values>
xgb_params = {
'colsample_bytree': 0.4,
'n_estimators': 1000,
'min_child_weight': 3,
'max_depth': 6,
'subsample': 0.4,
'learning_rate': 0.01,
'gamma': 0,
'reg_lambda': 0.02
}
model = XGBRegressor(tree_method='gpu_hist', **xgb_params ).fit(train_X_transformed, train_y )
test_data['PRIMEUNIT'].value_counts()<data_type_conversions>
train_score = mean_absolute_error(train_y, model.predict(train_X_transformed))
print(f'Mean train score is {train_score}' )
train_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True )<categorify>
valid_score = mean_absolute_error(valid_y, model.predict(valid_X_transformed))
print(f'Mean validation score is {valid_score}' )
<data_type_conversions><EOS> 
preds_test = model.predict(test_X_transformed)
 output = pd.DataFrame({'Id': test_X.index,
'SalePrice': preds_test})
 output.to_csv('submission.csv', index=False )