Kaggle competition: House Prices - Advanced Regression Techniques
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
trainData = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 testData = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 trainData.info()
print('_' * 40)
 testData.info()
trainData.describe()
IMAGE_SIZE = [512, 512]
EPOCHS = 30
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
SKIP_VALIDATION = False
TTA_NUM = 5
SEED = 42
random.seed(SEED)
 np.random.seed(SEED)
 tf.random.set_seed(SEED )<define_variables>
def BuildSummary(dataset, column):
meanData = dataset[[column, 'SalePrice']].groupby([column],as_index = False ).mean()
meanData.columns = [column, 'Average Sale Price($)']
countData = dataset[[column, 'SalePrice']].groupby([column],as_index = False ).count()
countData.columns = [column, 'Sale Price Count']
countData = countData.drop([column], axis = 1)
 summary = pd.concat([meanData, countData], axis = 1 ).sort_values(by = 'Average Sale Price($)', ascending = False)
 return summary
GCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')
 GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]] 
<define_variables>
listOfSummaryDfs = []
LotFrontage = BuildSummary(trainData, 'LotFrontage')
 LotFrontage.name = 'LotFrontage'
listOfSummaryDfs.append(LotFrontage)
 LotArea = BuildSummary(trainData, 'LotArea')
 LotArea.name = 'LotArea'
listOfSummaryDfs.append(LotArea)
 YearBuilt = BuildSummary(trainData, 'YearBuilt')
 YearBuilt.name = 'YearBuilt'
listOfSummaryDfs.append(YearBuilt)
 YearRemodAdd = BuildSummary(trainData, 'YearRemodAdd')
 YearRemodAdd.name = 'YearRemodAdd'
listOfSummaryDfs.append(YearRemodAdd)
 BsmtFinSF1 = BuildSummary(trainData, 'BsmtFinSF1')
 BsmtFinSF1.name = 'BsmtFinSF1'
listOfSummaryDfs.append(BsmtFinSF1)
 TotalBsmtSF = BuildSummary(trainData, 'TotalBsmtSF')
 TotalBsmtSF.name = 'TotalBsmtSF'
listOfSummaryDfs.append(TotalBsmtSF)
 FirstFlrSF = BuildSummary(trainData, '1stFlrSF')
 FirstFlrSF.name = 'FirstFlrSF'
listOfSummaryDfs.append(FirstFlrSF)
 GrLivArea = BuildSummary(trainData, 'GrLivArea')
 GrLivArea.name = 'GrLivArea'
listOfSummaryDfs.append(GrLivArea)
 TotRmsAbvGrd = BuildSummary(trainData, 'TotRmsAbvGrd')
 TotRmsAbvGrd.name = 'TotRmsAbvGrd'
listOfSummaryDfs.append(TotRmsAbvGrd)
 Fireplaces = BuildSummary(trainData, 'Fireplaces')
 Fireplaces.name = 'Fireplaces'
listOfSummaryDfs.append(Fireplaces)
 GarageYrBlt = BuildSummary(trainData, 'GarageYrBlt')
 GarageYrBlt.name = 'GarageYrBlt'
listOfSummaryDfs.append(GarageYrBlt)
 GarageCars = BuildSummary(trainData, 'GarageCars')
 GarageCars.name = 'GarageCars'
listOfSummaryDfs.append(GarageCars)
 GarageArea = BuildSummary(trainData, 'GarageArea')
 GarageArea.name = 'GarageArea'
listOfSummaryDfs.append(GarageArea)
 WoodDeckSF = BuildSummary(trainData, 'WoodDeckSF')
 WoodDeckSF.name = 'WoodDeckSF'
listOfSummaryDfs.append(WoodDeckSF)
 OpenPorchSF = BuildSummary(trainData, 'OpenPorchSF')
 OpenPorchSF.name = 'OpenPorchSF'
listOfSummaryDfs.append(OpenPorchSF)
 EnclosedPorch = BuildSummary(trainData, 'EnclosedPorch')
 EnclosedPorch.name = 'EnclosedPorch'
listOfSummaryDfs.append(EnclosedPorch)
 PoolArea = BuildSummary(trainData, 'PoolArea')
 PoolArea.name = 'PoolArea'
listOfSummaryDfs.append(PoolArea)
 MoSold = BuildSummary(trainData, 'MoSold')
 MoSold.name = 'MoSold'
listOfSummaryDfs.append(MoSold)
 YrSold = BuildSummary(trainData, 'YrSold')
 YrSold.name = 'YrSold'
listOfSummaryDfs.append(YrSold )
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec' )<categorify>
print(trainData.isnull().sum())
 print('_' * 40)
 print(testData.isnull().sum() )
np.set_printoptions(threshold=15, linewidth=80)
 def batch_to_numpy_images_and_labels(data):
images, labels = data
numpy_images = images.numpy()
numpy_labels = labels.numpy()
if numpy_labels.dtype == object:
numpy_labels = [None for _ in enumerate(numpy_images)]
return numpy_images, numpy_labels
def title_from_label_and_target(label, correct_label):
if correct_label is None:
return CLASSES[label], True
correct =(label == correct_label)
 return "{} [{}{}{}]".format(CLASSES[label], 'OK' if correct else 'NO', u"\u2192" if not correct else '',
CLASSES[correct_label] if not correct else ''), correct 
<define_variables>
trainData['label'] = 'train'
testData['label'] = 'test'
combined = pd.concat([trainData, testData])
 s =(combined.dtypes != 'object')
 combinedColumnsToFillMedian = list(s[s].index)
 t =(combined.dtypes == 'object')
 combinedColumnsToFillNoData = list(t[t].index)
 numericalImputer = SimpleImputer(strategy = 'median')
 numiercalCombined = pd.DataFrame(numericalImputer.fit_transform(combined[combinedColumnsToFillMedian]))
numiercalCombined.columns = combined[combinedColumnsToFillMedian].columns
categoricalImputer = SimpleImputer(strategy = 'constant', fill_value = 'Nodata/Na')
 categoricalCombined = pd.DataFrame(categoricalImputer.fit_transform(combined[combinedColumnsToFillNoData]))
categoricalCombined.columns = combined[combinedColumnsToFillNoData].columns
combined = pd.concat([numiercalCombined, categoricalCombined], axis = 1)
def display_batch_of_images(databatch, predictions=None):

 images, labels = batch_to_numpy_images_and_labels(databatch)
 if labels is None:
labels = [None for _ in enumerate(images)]
rows = int(math.sqrt(len(images)))
 cols = len(images)//rows
FIGSIZE = 13.0
SPACING = 0.1
subplot=(rows,cols,1)
 if rows < cols:
plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
else:
plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))
for i,(image, label)in enumerate(zip(images[:rows*cols], labels[:rows*cols])) :
title = '' if label is None else CLASSES[label]
correct = True
if predictions is not None:
title, correct = title_from_label_and_target(predictions[i], label)
 dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3
subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)
 plt.tight_layout()
if label is None and predictions is None:
plt.subplots_adjust(wspace=0, hspace=0)
 else:
plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
 plt.show()<categorify>
combined = pd.get_dummies(combined, columns = combinedColumnsToFillNoData, drop_first = True)
 trainData = combined[combined['label_train'] == 1]
testData = combined[combined['label_train'] == 0]
trainData = trainData.drop('label_train', axis = 1)
 testData = testData.drop(['label_train', 'SalePrice'], axis = 1)
 print(trainData.isnull().sum())
 print('_' * 40)
 print(testData.isnull().sum() )
def random_blockout(img, sl=0.1, sh=0.2, rl=0.4):
p=random.random()
if p>=0.25:
w, h, c = IMAGE_SIZE[0], IMAGE_SIZE[1], 3
origin_area = tf.cast(h*w, tf.float32)
 e_size_l = tf.cast(tf.round(tf.sqrt(origin_area * sl * rl)) , tf.int32)
 e_size_h = tf.cast(tf.round(tf.sqrt(origin_area * sh / rl)) , tf.int32)
 e_height_h = tf.minimum(e_size_h, h)
 e_width_h = tf.minimum(e_size_h, w)
 erase_height = tf.random.uniform(shape=[], minval=e_size_l, maxval=e_height_h, dtype=tf.int32)
 erase_width = tf.random.uniform(shape=[], minval=e_size_l, maxval=e_width_h, dtype=tf.int32)
 erase_area = tf.zeros(shape=[erase_height, erase_width, c])
 erase_area = tf.cast(erase_area, tf.uint8)
 pad_h = h - erase_height
pad_top = tf.random.uniform(shape=[], minval=0, maxval=pad_h, dtype=tf.int32)
 pad_bottom = pad_h - pad_top
pad_w = w - erase_width
pad_left = tf.random.uniform(shape=[], minval=0, maxval=pad_w, dtype=tf.int32)
 pad_right = pad_w - pad_left
erase_mask = tf.pad([erase_area], [[0,0],[pad_top, pad_bottom], [pad_left, pad_right], [0,0]], constant_values=1)
 erase_mask = tf.squeeze(erase_mask, axis=0)
 erased_img = tf.multiply(tf.cast(img,tf.float32), tf.cast(erase_mask, tf.float32))
return tf.cast(erased_img, img.dtype)
 else:
return tf.cast(img, img.dtype )<categorify>
train, test = train_test_split(trainData, test_size =.2)
 X_train = train.drop("SalePrice", axis = 1)
 y_train = train["SalePrice"]
X_test = test.drop("SalePrice", axis = 1)
 y_test = test["SalePrice"]
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, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum<create_dataframe>
lrRegressor = LinearRegression().fit(X_train, y_train)
 lrPred = lrRegressor.predict(X_test)
 ridgeRegressor = Ridge().fit(X_train, y_train)
 ridgePred = ridgeRegressor.predict(X_test)
 treeRegressor = DecisionTreeRegressor().fit(X_train, y_train)
 treePred = treeRegressor.predict(X_test)
 neighborRegressor = KNeighborsRegressor().fit(X_train, y_train)
 knPred = neighborRegressor.predict(X_test)
 baggingRegressor = BaggingRegressor().fit(X_train, y_train)
 baggingPred = baggingRegressor.predict(X_test)
 adaRegressor = AdaBoostRegressor().fit(X_train, y_train)
 adaPred = adaRegressor.predict(X_test)
 gradientRegressor = GradientBoostingRegressor().fit(X_train, y_train)
 gradientPred = gradientRegressor.predict(X_test)
 xgbRegressor = xgb.XGBRegressor().fit(X_train, y_train)
 xgbPred = xgbRegressor.predict(X_test )
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset 
<define_variables>
def PrintPerformance(regressor, model):
print(model)
 print('Best Score: ' + str(regressor.best_score_))
print('Best Parameters: ', str(regressor.best_params_), '
')
 lrParamGrid = {'fit_intercept' : [True, False],
'normalize' : [True, False]}
lrGrid = GridSearchCV(lrRegressor, param_grid = lrParamGrid)
 bestLr = lrGrid.fit(X_train, y_train)
 PrintPerformance(bestLr, 'Linear Regression')
 ridgeParamGrid = {'alpha' : [.1,.5, 1, 5, 10, 50, 100],
'fit_intercept' : [True, False],
'normalize' : [True, False],
'solver' : ['auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga']}
ridgeGrid = RandomizedSearchCV(ridgeRegressor, param_distributions = ridgeParamGrid)
 bestRidge = ridgeGrid.fit(X_train, y_train)
 PrintPerformance(bestRidge, 'Ridge Regression')
 treeParamGrid = {'criterion' : ['mse', 'friedman_mse', 'mae'],
'min_samples_leaf' : [1, 2, 3, 5, 8, 10],
'splitter' : ['best', 'random']}
treeGrid = GridSearchCV(treeRegressor, param_grid = treeParamGrid)
 bestTree = treeGrid.fit(X_train, y_train)
 PrintPerformance(bestTree, 'Tree Regression')
 knnParamGrid = {'n_neighbors' : [1, 2, 3, 4, 5],
'algorithm' : ['auto', 'ball_tree', 'kd_tree', 'brute'],
'weights': ['uniform', 'distance']}
knnGrid = RandomizedSearchCV(neighborRegressor, param_distributions = knnParamGrid)
 bestKnn = knnGrid.fit(X_train, y_train)
 PrintPerformance(bestKnn, 'Knn Regression')
 baggingParamGrid = {'n_estimators' : [200],
'max_features' : [1, 2, 3, 5, 8, 10],
'bootstrap': [True, False],
'bootstrap_features': [True, False]}
baggingGrid = RandomizedSearchCV(baggingRegressor, param_distributions = baggingParamGrid)
 bestBagging = baggingGrid.fit(X_train, y_train)
 PrintPerformance(bestBagging, 'Bagging Regression')
 adaBoostParamGrid = {'n_estimators' : [200],
'loss' : ['linear', 'square', 'exponential']}
adaGrid = GridSearchCV(adaRegressor, param_grid = adaBoostParamGrid)
 bestAda = adaGrid.fit(X_train, y_train)
 PrintPerformance(bestAda, 'Ada Regression')
 gradientParamGrid = {'loss' : ['ls', 'lad', 'huber', 'quantile'],
'n_estimators' : [200],
'subsample': [.5,.8, 1],
'criterion' : ['mse', 'friedman_mse', 'mae']}
gradientGrid = RandomizedSearchCV(gradientRegressor, param_distributions = gradientParamGrid)
 bestGradient = gradientGrid.fit(X_train, y_train)
 PrintPerformance(bestGradient, 'Gradient Regression')
 xgbParamGrid = {'learning_rate': [.05,.1,.2,.3,.4,.5],
'gamma' : [0, 1, 5, 10, 50, 100],
'max_depth' : [3, 4, 5, 6, 7, 8],
'subsample' : [.5,.8, 1]}
xgbGrid = RandomizedSearchCV(xgbRegressor, param_distributions = xgbParamGrid)
 bestXgb = xgbGrid.fit(X_train, y_train)
 PrintPerformance(bestXgb, 'XGBoost Regression' )
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset<create_dataframe>
ridgeParamGrid2 = {'alpha' : [1, 2, 3],
'solver': ['svd'],
'normalize' : [True],
'fit_intercept' : [False]}
ridgeGrid2 = GridSearchCV(ridgeRegressor, param_grid = ridgeParamGrid2)
 bestRidge = ridgeGrid2.fit(X_train, y_train)
 PrintPerformance(bestRidge, 'Ridge Regression')
 xgbParamGrid2 = {'learning_rate': [.08,.1,.15],
'gamma' : [10, 20, 30],
'subsample' : [.9,.95, 1],
'max_depth': [5]}
xgbGrid2 = GridSearchCV(xgbRegressor, param_grid = xgbParamGrid2)
 bestXgb = xgbGrid2.fit(X_train, y_train)
 PrintPerformance(bestXgb, 'XGBoost Regression' )
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset<create_dataframe>
ridgePred = bestRidge.predict(testData)
 gradientPred = bestGradient.predict(testData)
 xgbPred = bestXgb.predict(testData)
 adaPred = bestAda.predict(testData)
 treePred = bestTree.predict(testData)
 submissionRidge = pd.DataFrame({'Id': testData.Id.astype(int), 'SalePrice': ridgePred.astype(int)})
 submissionGradient = pd.DataFrame({'Id': testData.Id.astype(int), 'SalePrice': gradientPred.astype(int)})
 submissionXgb = pd.DataFrame({'Id': testData.Id.astype(int), 'SalePrice': xgbPred.astype(int)})
 submissionAda = pd.DataFrame({'Id': testData.Id.astype(int), 'SalePrice': adaPred.astype(int)})
 submissionTree = pd.DataFrame({'Id': testData.Id.astype(int), 'SalePrice': treePred.astype(int)})
 submissionRidge.to_csv('submissionRidge.csv', index = False)
 submissionGradient.to_csv('submissionGradient.csv', index = False)
 submissionXgb.to_csv('submissionXgb.csv', index = False)
 submissionAda.to_csv('submissionAda.csv', index = False)
 submissionTree.to_csv('submissionTree.csv', index = False)
 print('Files Saved' )