Kaggle competition: House Prices - Advanced Regression Techniques
def create_model() :
pretrained_model = tf.keras.applications.Xception(input_shape=(IMAGE_SIZE[0],IMAGE_SIZE[1],3),weights='imagenet',include_top=False)
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D(name="Layer1"),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model.compile(
 optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss = 'sparse_categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
 return model<find_best_params>
%matplotlib inline
with strategy.scope() :
Xception_model = create_model()
Xception_model.summary()<train_model>
train_data = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test_data = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )
%%time
Checkpoint=tf.keras.callbacks.ModelCheckpoint(f"xception_model.h5", monitor='val_accuracy', verbose=1, save_best_only=True,
save_weights_only=True,mode='max')
 Xception_history1 = Xception_model.fit(
 get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback, Checkpoint])
 histories.append(Xception_history1 )<choose_model_class>
train_data['train'] = 1
test_data['train'] = 0
with strategy.scope() :
enet = efn.EfficientNetB7(
 input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3),
weights='imagenet',
include_top=False 
)
 enet.trainable = True
model1 = tf.keras.Sequential([
enet,
tf.keras.layers.GlobalMaxPooling2D(name="Layer1"),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model1.compile(
 optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss = 'sparse_categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
 model1.summary()
models.append(model1 )<train_model>
full_data = pd.concat([train_data, test_data], sort = False, ignore_index = True )
%%time
Checkpoint=tf.keras.callbacks.ModelCheckpoint(f"Enet_model.h5", monitor='val_accuracy', verbose=1, save_best_only=True,
save_weights_only=True,mode='max')
 train_history1 = model1.fit(
 get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback, Checkpoint], 
)
 histories.append(train_history1)
<predict_on_test>
full_data['SalePrice'].replace(np.nan, 1, inplace = True )
if not SKIP_VALIDATION:
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
m1 = model1.predict(images_ds)
 m2 = Xception_model.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
cm_probabilities = alpha*m1+(1-alpha)*m2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 scores.append(f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro'))
best_alpha = np.argmax(scores)/100
else:
best_alpha = 0.51
print('Best alpha: ' + str(best_alpha))<compute_test_metric>
total = full_data.isnull().sum().sort_values(ascending = False)
 percent =(full_data.isnull().sum() /full_data.isnull().count() ).sort_values(ascending = False)
 missing_data = pd.concat([total, percent], axis = 1, keys = ['Total', 'Percent'])
 missing_data.head(20)
if not SKIP_VALIDATION:
cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))
 score = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 precision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 recall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))<predict_on_test>
full_data.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence'], axis = 1, inplace = True)
 full_data['FireplaceQu'].fillna('No', inplace = True)
 full_data["LotFrontage"] = full_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))
for col in('GarageFinish', 'GarageQual', 'GarageCond', 'GarageType'):
full_data[col].fillna('No', inplace = True)
 full_data['GarageYrBlt'].fillna(0, inplace = True)
 for col in('BsmtCond', 'BsmtExposure', 'BsmtQual', 'BsmtFinType1', 'BsmtFinType2'):
full_data[col].fillna('No', inplace = True)
 full_data['MasVnrType'].fillna('No', inplace = True)
 full_data['MasVnrArea'].fillna(0, inplace = True)
 full_data['MSZoning'].fillna('RL', inplace = True)
 full_data['BsmtFullBath'].fillna(0, inplace = True)
 full_data['BsmtHalfBath'].fillna(0, inplace = True)
 full_data.drop(['Utilities'], axis = 1, inplace = True)
 full_data['Functional'].fillna('Typ', inplace = True)
 full_data['GarageArea'].fillna(0, inplace = True)
 full_data['TotalBsmtSF'].fillna(0, inplace = True)
 full_data['Electrical'].fillna('SBrkr', inplace = True)
 full_data['Electrical'].replace(['Mix', 'FuseP','FuseF'], ['SBrkr', 'SBrkr', 'SBrkr'], inplace = True)
 full_data.drop(['Exterior1st', 'Exterior2nd'], axis = 1, inplace = True)
 full_data['KitchenQual'].fillna('TA', inplace = True)
 full_data['GarageCars'].fillna(0, inplace = True)
 full_data.drop(['BsmtUnfSF'], axis = 1, inplace = True)
 full_data.drop(['BsmtFinSF1', 'BsmtFinSF2'], axis = 1, inplace = True)
 full_data.drop(['Neighborhood', 'Street', 'Condition2', 'RoofMatl', 'Heating'], axis = 1, inplace = True)
 full_data['SaleType'].fillna('WD', inplace = True )
def predict_tta(model, n_iter):
probs = []
for i in range(n_iter):
test_ds = get_test_dataset(ordered=True)
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probs.append(model.predict(test_images_ds,verbose=0))
return probs 
<predict_on_test>
full_data['GrLivArea'] = np.log(full_data['GrLivArea'])
 full_data['LotArea'] = np.log1p(full_data['LotArea'])
 full_data['LowQualFinSF'] = np.log1p(full_data['LowQualFinSF'])
 full_data['BsmtHalfBath'] = np.log1p(full_data['BsmtHalfBath'])
 full_data['KitchenAbvGr'] = np.log1p(full_data['KitchenAbvGr'])
 full_data['GarageYrBlt'] = np.log1p(full_data['GarageYrBlt'])
 full_data['EnclosedPorch'] = np.log1p(full_data['EnclosedPorch'])
 full_data['3SsnPorch'] = np.log1p(full_data['3SsnPorch'])
 full_data['ScreenPorch'] = np.log1p(full_data['ScreenPorch'])
 full_data['PoolArea'] = np.log1p(full_data['PoolArea'])
 full_data['MiscVal'] = np.log1p(full_data['MiscVal'] )
test_ds = get_test_dataset(ordered=True)
 print('Calculating predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probs1 = np.mean(predict_tta(model1, TTA_NUM), axis=0)
 probs2 = np.mean(predict_tta(Xception_model, TTA_NUM), axis=0)
 probabilities = best_alpha*probs1 +(1-best_alpha)*probs2
predictions = np.argmax(probabilities, axis=-1)
 print('Generating submission file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')
<import_modules>
cat_to_num = {
'HouseStyle' : {'1Story' : 1, '1.5Unf' : 2, '1.5Fin' : 3, '2Story' : 4, '2.5Unf' : 5,
'2.5Fin' : 6, 'SFoyer' : 7, 'SLvl' : 8},
'ExterQual' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2},
'ExterCond' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2},
'BsmtQual' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2, 'No' : 0},
'BsmtCond' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2, 'No' : 0},
'BsmtExposure' : {'Gd' : 10, 'Av' : 7, 'Mn' : 4, 'No' : 0},
'BsmtFinType1' : {'GLQ' : 10, 'ALQ' : 8, 'BLQ' : 6, 'Rec' : 5, 'LwQ' : 4, 'Unf' : 2, 'No' : 0},
'BsmtFinType2' : {'GLQ' : 10, 'ALQ' : 8, 'BLQ' : 6, 'Rec' : 5, 'LwQ' : 4, 'Unf' : 2, 'No' : 0},
'HeatingQC' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2},
'KitchenQual' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2},
'FireplaceQu' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2, 'No' : 0},
'GarageQual' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2, 'No' : 0},
'GarageCond' : {'Ex' : 10, 'Gd' : 8, 'TA' : 6, 'Fa' : 4, 'Po' : 2, 'No' : 0}
}
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from kaggle_datasets import KaggleDatasets<install_modules>
full_data.replace(cat_to_num, inplace = True )
!pip install -q efficientnet 
<define_variables>
threshold = 50
cat_columns = list(full_data.select_dtypes(include = ['object', 'category'] ).columns)
 for col in cat_columns:
vc = full_data[col].value_counts()
val_remove = vc[vc <= threshold].index.values
full_data[col].loc[full_data[col].isin(val_remove)] = 'Other'
GCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')
 print(GCS_DS_PATH )<define_variables>
full_data = pd.get_dummies(full_data )
IMAGE_SIZE = 512
EPOCHS = 35
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
NUM_TRAINING_IMAGES = 12753
NUM_TEST_IMAGES = 7382
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE<create_dataframe>
full_data.drop(['Id'], axis = 1, inplace = True)
 df_train = full_data[full_data['train'] == 1]
df_train = df_train.drop(['train'],axis=1)
 df_train.drop(['SalePrice'], axis = 1, inplace = True)
 df_test = full_data[full_data['train'] == 0]
df_test = df_test.drop(['train'],axis=1)
 df_test.drop(['SalePrice'], axis = 1, inplace = True )
train_data = tf.data.TFRecordDataset(
 tf.io.gfile.glob(GCS_DS_PATH + '/tfrecords-jpeg-' + str(IMAGE_SIZE)+ 'x' + str(IMAGE_SIZE)+ '/train/*.tfrec'),
num_parallel_reads = tf.data.experimental.AUTOTUNE
)<set_options>
Y = full_data['SalePrice'][full_data['train'] == 1]
ignore_order = tf.data.Options()
ignore_order.experimental_deterministic = False
train_data = train_data.with_options(ignore_order )<categorify>
X_train, X_val, Y_train, Y_val = train_test_split(df_train, Y, test_size = 0.15, random_state = 4 )
def read_labeled_tfrecord(example):
tfrec_format = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, tfrec_format)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [IMAGE_SIZE, IMAGE_SIZE, 3])
 return image<categorify>
xgb_reg = xgb.XGBRegressor(booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.6, gamma=0,
importance_type='gain', learning_rate=0.01, max_delta_step=0,
max_depth=4, min_child_weight=1.5, n_estimators=2400,
n_jobs=1, nthread=None, objective='reg:linear',
reg_alpha=0.6, reg_lambda=0.6, scale_pos_weight=1,
silent=None, subsample=0.8, verbosity=1 )
train_data = train_data.map(read_labeled_tfrecord )<categorify>
xgb_reg.fit(X_train, Y_train, verbose = 1, early_stopping_rounds = 50, eval_metric = 'rmse' , eval_set = [(X_val, Y_val)] )
train_data = train_data.map(augment, num_parallel_calls=tf.data.experimental.AUTOTUNE )<define_variables>
Y_hat1 = xgb_reg.predict(df_test )
<define_variables><EOS> 
submission = pd.DataFrame({
"Id": test_data["Id"],
"SalePrice": Y_hat1
})
 submission.to_csv('submission.csv', index=False )