Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<define_variables>
df= pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv' )
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()
cm_probabilities =(models[0].predict(images_ds)+ models[1].predict(images_ds)) / 2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions )<compute_test_metric>
total = df.isnull().sum().sort_values(ascending=False)
 percent =(df.isnull().sum() /df.isnull().count() ).sort_values(ascending=False)
 missing = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing.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))<compute_test_metric>
df.drop(['Alley'],axis=1,inplace=True)
 df.drop(['PoolQC'],axis=1,inplace=True)
 df.drop(['MiscFeature'],axis=1,inplace=True)
 df.drop(['Fence'],axis=1,inplace=True)
 df.drop(['FireplaceQu'],axis=1,inplace=True)
 df['LotFrontage']=df['LotFrontage'].fillna(df['LotFrontage'].mean())
 df['GarageCond']=df['GarageCond'].fillna(df['GarageCond'].mode() [0])
 df['GarageType']=df['GarageType'].fillna(df['GarageType'].mode() [0])
 df['GarageYrBlt']=df['GarageYrBlt'].fillna(df['GarageYrBlt'].mean())
 df['GarageFinish']=df['GarageFinish'].fillna(df['GarageFinish'].mode() [0])
 df['GarageQual']=df['GarageQual'].fillna(df['GarageQual'].mode() [0])
 df['BsmtExposure']=df['BsmtExposure'].fillna(df['BsmtExposure'].mode() [0])
 df['BsmtFinType2']=df['BsmtFinType2'].fillna(df['BsmtFinType2'].mode() [0])
 df['BsmtFinType1']=df['BsmtFinType1'].fillna(df['BsmtFinType1'].mode() [0])
 df['BsmtCond']=df['BsmtCond'].fillna(df['BsmtCond'].mode() [0])
 df['BsmtQual']=df['BsmtQual'].fillna(df['BsmtQual'].mode() [0])
 df['MasVnrArea']=df['MasVnrArea'].fillna(df['MasVnrArea'].mean())
 df['MasVnrType']=df['MasVnrType'].fillna(df['MasVnrType'].mode() [0])
 df['Electrical']=df['Electrical'].fillna(df['Electrical'].mode() [0] )
if not SKIP_VALIDATION:
cmat_norm =(cmat.T / cmat.sum(axis=1)).T
display_confusion_matrix(cmat_norm, score, precision, recall )<save_to_csv>
lencoders = {}
for col in df.select_dtypes(include=['object'] ).columns:
lencoders[col] = LabelEncoder()
df[col] = lencoders[col].fit_transform(df[col] )
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities =(models[0].predict(test_images_ds)+ models[1].predict(test_images_ds)) / 2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv 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='' )<install_modules>
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_1 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='log2', max_depth = 4))
clf_2 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='auto', max_depth = 4))
clf_1.fit(x_t, y_t)
 clf_2.fit(x_t, y_t)
 sel_feat_1 = x_t.columns[(clf_1.get_support())]
sel_feat_2 = x_t.columns[(clf_2.get_support())]
print(sel_feat_1)
 print(sel_feat_2 )
!pip install -q efficientnet<import_modules>
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_3 = SelectFromModel(DecisionTreeClassifier(max_features='log2'))
clf_4 = SelectFromModel(DecisionTreeClassifier(max_features='auto'))
clf_3.fit(x_t, y_t)
 clf_4.fit(x_t, y_t)
 sel_feat_3 = x_t.columns[(clf_3.get_support())]
sel_feat_4 = x_t.columns[(clf_4.get_support())]
print(sel_feat_3)
 print(sel_feat_4 )
print("Tensorflow version " + tf.__version__)
<set_options>
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_5 = SelectFromModel(ExtraTreesClassifier(max_features='log2'))
clf_6 = SelectFromModel(ExtraTreesClassifier(max_features='auto'))
clf_5.fit(x_t, y_t)
 clf_6.fit(x_t, y_t)
 sel_feat_5 = x_t.columns[(clf_5.get_support())]
sel_feat_6 = x_t.columns[(clf_5.get_support())]
print(sel_feat_5)
 print(sel_feat_6 )
AUTO = tf.data.experimental.AUTOTUNE
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 GCS_DS_PATH = KaggleDatasets().get_gcs_path()
img_size=512
IMAGE_SIZE = [512, 512]
EPOCHS = 20
BATCH_SIZE = 16 * strategy.num_replicas_in_sync 
<init_hyperparams>
clf_1 = RandomForestClassifier()
x_train_1 = df[['LotFrontage', 'LotArea', 'Neighborhood', 'Condition1','Condition2',
'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd', 'RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrArea', 'ExterQual', 'BsmtFinSF1','BsmtFinSF2',
'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional',
'GarageYrBlt', 'GarageCars', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal']]
y_train = df['SalePrice']
viz_1 = FeatureImportances(clf_1)
 viz_1.fit(x_train_1, y_train)
 viz_1 = FeatureImportances(clf_1)
 viz_1.fit(x_train_1, y_train)
 viz_1.poof()
clf_2 = DecisionTreeClassifier()
x_train_2 = df[['LotFrontage', 'LotArea', 'LotShape', 'LotConfig', 'Neighborhood',
'HouseStyle', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd',
'Exterior1st', 'Exterior2nd', 'MasVnrArea', 'BsmtExposure',
'BsmtFinType1', 'BsmtFinSF1', 'BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC',
'1stFlrSF', '2ndFlrSF', 'GrLivArea', 'BedroomAbvGr','TotRmsAbvGrd',
'Fireplaces','GarageType','GarageYrBlt', 'GarageFinish', 'GarageArea', 'WoodDeckSF',
'OpenPorchSF', 'MoSold', 'YrSold']]
viz_2 = FeatureImportances(clf_2)
 viz_2.fit(x_train_2, y_train)
 viz_2 = FeatureImportances(clf_2)
 viz_2.fit(x_train_2, y_train)
 viz_2.poof()
clf_3 = ExtraTreesClassifier()
x_train_3 = df[['MSSubClass', 'LotFrontage', 'LotArea', 'LotShape', 'LotConfig',
'Neighborhood', 'OverallQual', 'OverallCond', 'YearBuilt',
'YearRemodAdd', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
'MasVnrArea', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1', 'BsmtUnfSF',
'TotalBsmtSF', 'HeatingQC', '1stFlrSF', '2ndFlrSF', 'GrLivArea',
'BsmtFullBath', 'BedroomAbvGr', 'TotRmsAbvGrd', 'Fireplaces',
'GarageYrBlt', 'GarageFinish', 'GarageArea', 'WoodDeckSF',
'OpenPorchSF', 'MoSold', 'YrSold']]
viz_3 = FeatureImportances(clf_3)
 viz_3.fit(x_train_3, y_train)
 viz_3 = FeatureImportances(clf_3)
 viz_3.fit(x_train_3, y_train)
 viz_3.poof()
LR_START = 0.00001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 0
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr =(LR_MAX - LR_START)/ LR_RAMPUP_EPOCHS * epoch + LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))<define_variables>
df_train = df[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch','MoSold', 'YrSold','SalePrice']]
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]]
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')
 SKIP_VALIDATION = True
if SKIP_VALIDATION:
TRAINING_FILENAMES = TRAINING_FILENAMES + VALIDATION_FILENAMES
VALIDATION_MISMATCHES_IDS = [] 
<define_variables>
df_train['LotArea']=np.log(df_train['LotArea'] )
CLASSES = ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'wild geranium', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle',
'snapdragon', "colt's foot", 'king protea', 'spear thistle', 'yellow iris', 'globe-flower', 'purple coneflower', 'peruvian lily', 'balloon flower', 'giant white arum lily',
'fire lily', 'pincushion flower', 'fritillary', 'red ginger', 'grape hyacinth', 'corn poppy', 'prince of wales feathers', 'stemless gentian', 'artichoke', 'sweet william',
'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly', 'ruby-lipped cattleya', 'cape flower', 'great masterwort', 'siam tulip', 'lenten rose',
'barberton daisy', 'daffodil', 'sword lily', 'poinsettia', 'bolero deep blue', 'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',
'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus', 'bishop of llandaff', 'gaura', 'geranium', 'orange dahlia', 'pink-yellow dahlia',
'cautleya spicata', 'japanese anemone', 'black-eyed susan', 'silverbush', 'californian poppy', 'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',
'gazania', 'azalea', 'water lily', 'rose', 'thorn apple', 'morning glory', 'passion flower', 'lotus', 'toad lily', 'anthurium',
'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose', 'tree mallow', 'magnolia', 'cyclamen ', 'watercress', 'canna lily',
'hippeastrum ', 'bee balm', 'pink quill', 'foxglove', 'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower',
'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose']<categorify>
x_train = df_train[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']]
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
def display_one_flower(image, title, subplot, red=False, titlesize=16):
plt.subplot(*subplot)
 plt.axis('off')
 plt.imshow(image)
 if len(title)> 0:
plt.title(title, fontsize=int(titlesize)if not red else int(titlesize/1.2), color='red' if red else 'black', fontdict={'verticalalignment':'center'}, pad=int(titlesize/1.5))
return(subplot[0], subplot[1], subplot[2]+1)
 def display_batch_of_images(databatch, predictions=None, figsize = 13.0):

 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 = figsize
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()
def display_confusion_matrix(cmat, score, precision, recall):
plt.figure(figsize=(15,15))
ax = plt.gca()
ax.matshow(cmat, cmap='Reds')
 ax.set_xticks(range(len(CLASSES)))
 ax.set_xticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_xticklabels() , rotation=45, ha="left", rotation_mode="anchor")
 ax.set_yticks(range(len(CLASSES)))
 ax.set_yticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_yticklabels() , rotation=45, ha="right", rotation_mode="anchor")
 titlestring = ""
if score is not None:
titlestring += 'f1 = {:.3f} '.format(score)
 if precision is not None:
titlestring += '
precision = {:.3f} '.format(precision)
 if recall is not None:
titlestring += '
recall = {:.3f} '.format(recall)
 if len(titlestring)> 0:
ax.text(101, 1, titlestring, fontdict={'fontsize': 18, 'horizontalalignment':'right', 'verticalalignment':'top', 'color':'
plt.show()
def display_training_curves(training, validation, title, subplot):
if subplot%10==1:
plt.subplots(figsize=(10,10), facecolor='
plt.tight_layout()
ax = plt.subplot(subplot)
 ax.set_facecolor('
ax.plot(training)
 ax.plot(validation)
 ax.set_title('model '+ title)
 ax.set_ylabel(title)
 ax.set_xlabel('epoch')
 ax.legend(['train', 'valid.'])
<normalization>
y_train = df_train['SalePrice']
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))
def transform(image,label):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
rot = 15.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = 16.* tf.random.normal([1],dtype='float32')
 w_shift = 16.* tf.random.normal([1],dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3]),label<categorify>
valgrid = {'n_estimators':hp.quniform('n_estimators', 1500, 2500, 25),
'gamma':hp.uniform('gamma', 0.01, 0.05),
'base_score':hp.uniform('base_score',0.6,0.9),
'learning_rate':hp.uniform('learning_rate', 0.00001, 0.03),
'max_depth':hp.quniform('max_depth', 3,8,1),
'subsample':hp.uniform('subsample', 0.50, 0.95),
'colsample_bytree':hp.uniform('colsample_bytree', 0.50, 0.95),
'colsample_bylevel':hp.uniform('colsample_bylevel', 0.50, 0.95),
'colsample_bynode':hp.uniform('colsample_bynode', 0.50, 0.95),
'reg_lambda':hp.uniform('reg_lambda', 1, 20)
 }
def objective(params):
params = {'n_estimators': int(params['n_estimators']),
'gamma': params['gamma'],
'base_score': params['base_score'],
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'subsample': params['subsample'],
'colsample_bytree': params['colsample_bytree'],
'colsample_bylevel': params['colsample_bylevel'],
'colsample_bynode': params['colsample_bynode'],
'reg_lambda': params['reg_lambda']}
xb_a= xgb.XGBRegressor(**params)
 score = cross_val_score(xb_a, x_train, y_train, scoring='neg_mean_squared_error', cv=5, n_jobs=-1 ).mean()
return -score
bestP = fmin(fn= objective, space= valgrid, max_evals=20, rstate=np.random.RandomState(42), algo=tpe.suggest )
def random_blockout(img, sl=0.1, sh=0.2, rl=0.4):
h, w, c = img_size, img_size, 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 )<categorify>
df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 total = df_test[['MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']].isnull().sum().sort_values(ascending=False)
 percent =(df_test.isnull().sum() /df_test.isnull().count() ).sort_values(ascending=False)
 missing_test = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_test.head(20 )
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
def read_labeled_id_tfrecord(example):
LABELED_ID_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, LABELED_ID_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 idnum = example['id']
return image, label, idnum
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_id_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def load_dataset_with_id(filenames, 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_id_tfrecord, num_parallel_calls=AUTO)
 return dataset
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 image= random_blockout(image)
 return image, label
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.filter(lambda image, label, idnum: tf.reduce_sum(tf.cast(idnum == VALIDATION_MISMATCHES_IDS, tf.int32)) ==0)
 dataset = dataset.map(lambda image, label, idnum: [image, label])
 dataset = dataset.map(transform, num_parallel_calls=AUTO)
 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
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)
 dataset = dataset.filter(lambda image, label, idnum: tf.reduce_sum(tf.cast(idnum == VALIDATION_MISMATCHES_IDS, tf.int32)) ==0)
 dataset = dataset.map(lambda image, label, idnum: [image, label])
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset_with_id(ordered=False):
dataset = load_dataset_with_id(VALIDATION_FILENAMES, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
dataset = load_dataset(filenames,labeled = False)
 dataset = dataset.map(lambda image, idnum: idnum)
 dataset = dataset.filter(lambda idnum: tf.reduce_sum(tf.cast(idnum == VALIDATION_MISMATCHES_IDS, tf.int32)) ==0)
 uids = next(iter(dataset.batch(21000)) ).numpy().astype('U')
 return len(np.unique(uids))
NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES =(1 - SKIP_VALIDATION)* count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<create_dataframe>
df_test['LotFrontage']=df_test['LotFrontage'].fillna(df_test['LotFrontage'].mean())
 df_test['GarageFinish']=df_test['GarageFinish'].fillna(df_test['GarageFinish'].mode() [0])
 df_test['GarageYrBlt']=df_test['GarageYrBlt'].fillna(df_test['GarageYrBlt'].mean())
 df_test['GarageType']=df_test['GarageType'].fillna(df_test['GarageType'].mode() [0])
 df_test['BsmtExposure']=df_test['BsmtExposure'].fillna(df_test['BsmtExposure'].mode() [0])
 df_test['BsmtFinType1']=df_test['BsmtFinType1'].fillna(df_test['BsmtFinType1'].mode() [0])
 df_test['MasVnrType']=df_test['MasVnrType'].fillna(df_test['MasVnrType'].mode() [0])
 df_test['MasVnrArea']=df_test['MasVnrArea'].fillna(df_test['MasVnrArea'].mean())
 df_test['BsmtFullBath']=df_test['BsmtFullBath'].fillna(df_test['BsmtFullBath'].mode() [0])
 df_test['Functional']=df_test['Functional'].fillna(df_test['Functional'].mode() [0])
 df_test['KitchenQual']=df_test['KitchenQual'].fillna(df_test['KitchenQual'].mode() [0])
 df_test['GarageArea']=df_test['GarageArea'].fillna(df_test['GarageArea'].mean())
 df_test['BsmtUnfSF']=df_test['BsmtUnfSF'].fillna(df_test['BsmtUnfSF'].mean())
 df_test['Exterior2nd']=df_test['Exterior2nd'].fillna(df_test['Exterior2nd'].mode() [0])
 df_test['Exterior1st']=df_test['Exterior1st'].fillna(df_test['Exterior1st'].mode() [0])
 df_test['BsmtFinSF1']=df_test['BsmtFinSF1'].fillna(df_test['BsmtFinSF1'].mean())
 df_test['BsmtFinSF2']=df_test['BsmtFinSF2'].fillna(df_test['BsmtFinSF2'].mean())
 df_test['TotalBsmtSF']=df_test['TotalBsmtSF'].fillna(df_test['TotalBsmtSF'].mean())
 df_test['GarageCars']=df_test['GarageCars'].fillna(df_test['GarageCars'].mean() )
if len(VALIDATION_MISMATCHES_IDS)>0:
dataset = load_dataset(TRAINING_FILENAMES,labeled = True)
 dataset = dataset.filter(lambda image, label, idnum: tf.reduce_sum(tf.cast(idnum == VALIDATION_MISMATCHES_IDS, tf.int32)) >0)
 dataset = dataset.map(lambda image, label, idnum: [image, label])
 imgs = next(iter(dataset.batch(len(VALIDATION_MISMATCHES_IDS))))
display_batch_of_images(imgs)
<choose_model_class>
df_test['LotArea']=np.log(df_test['LotArea'] )
with strategy.scope() :
enet = efn.EfficientNetB7(
 input_shape=(512, 512, 3),
weights='noisy-student',
include_top=False 
)
 model = tf.keras.Sequential([
enet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model.compile(
 optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 model.summary() 
<train_model>
df_test_work = df_test[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']]
lencoders_test = {}
for colt in df_test_work.select_dtypes(include=['object'] ).columns:
lencoders_test[colt] = LabelEncoder()
df_test_work[colt] = lencoders_test[colt].fit_transform(df_test_work[colt] )
history = model.fit(
 get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback],
validation_data=None if SKIP_VALIDATION else get_validation_dataset() 
)
<choose_model_class>
reg_final = xgboost.XGBRegressor(base_score=bestP['base_score'],
colsample_bylevel=bestP['colsample_bylevel'], colsample_bynode=bestP['colsample_bynode'],
colsample_bytree=bestP['colsample_bytree'],
gamma=bestP['gamma'], learning_rate=bestP['learning_rate'],
max_depth=int(bestP['max_depth']), n_estimators=int(bestP['n_estimators']),
random_state=42, reg_lambda=bestP['reg_lambda'], subsample=bestP['subsample'])
 reg_final.fit(x_train,y_train)
 y_pred = reg_final.predict(df_test_work)
 prediction = pd_out.DataFrame(y_pred)
 output = pd_out.concat([df_test_work['Id'],prediction], axis=1)
 output.columns=['Id','SalePrice']
output.to_csv('HousePrice_submission.csv', index=False)
 print("Submission successful" )
with strategy.scope() :
rnet = DenseNet201(
 input_shape=(512, 512, 3),
weights='imagenet',
include_top=False 
)
 model2 = tf.keras.Sequential([
rnet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model2.compile(
 optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 model2.summary()<train_model>
df= pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv' )
history2 = model2.fit(
 get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback],
validation_data=None if SKIP_VALIDATION else get_validation_dataset() 
)
<predict_on_test>
total = df.isnull().sum().sort_values(ascending=False)
 percent =(df.isnull().sum() /df.isnull().count() ).sort_values(ascending=False)
 missing = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing.head(20 )
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()
m = model.predict(images_ds)
 m2 = model2.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
cm_probabilities = alpha*m+(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'))
print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions)
 plt.plot(scores)
 best_alpha = np.argmax(scores)/100
cm_probabilities = best_alpha*m+(1-best_alpha)*m2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 else:
best_alpha = 0.51<save_to_csv>
df.drop(['Alley'],axis=1,inplace=True)
 df.drop(['PoolQC'],axis=1,inplace=True)
 df.drop(['MiscFeature'],axis=1,inplace=True)
 df.drop(['Fence'],axis=1,inplace=True)
 df.drop(['FireplaceQu'],axis=1,inplace=True)
 df['LotFrontage']=df['LotFrontage'].fillna(df['LotFrontage'].mean())
 df['GarageCond']=df['GarageCond'].fillna(df['GarageCond'].mode() [0])
 df['GarageType']=df['GarageType'].fillna(df['GarageType'].mode() [0])
 df['GarageYrBlt']=df['GarageYrBlt'].fillna(df['GarageYrBlt'].mean())
 df['GarageFinish']=df['GarageFinish'].fillna(df['GarageFinish'].mode() [0])
 df['GarageQual']=df['GarageQual'].fillna(df['GarageQual'].mode() [0])
 df['BsmtExposure']=df['BsmtExposure'].fillna(df['BsmtExposure'].mode() [0])
 df['BsmtFinType2']=df['BsmtFinType2'].fillna(df['BsmtFinType2'].mode() [0])
 df['BsmtFinType1']=df['BsmtFinType1'].fillna(df['BsmtFinType1'].mode() [0])
 df['BsmtCond']=df['BsmtCond'].fillna(df['BsmtCond'].mode() [0])
 df['BsmtQual']=df['BsmtQual'].fillna(df['BsmtQual'].mode() [0])
 df['MasVnrArea']=df['MasVnrArea'].fillna(df['MasVnrArea'].mean())
 df['MasVnrType']=df['MasVnrType'].fillna(df['MasVnrType'].mode() [0])
 df['Electrical']=df['Electrical'].fillna(df['Electrical'].mode() [0] )
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = best_alpha*model.predict(test_images_ds)+(1-best_alpha)*model2.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv 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='')
 np.savez_compressed('probs.npz', a=probabilities)
<load_from_csv>
lencoders = {}
for col in df.select_dtypes(include=['object'] ).columns:
lencoders[col] = LabelEncoder()
df[col] = lencoders[col].fit_transform(df[col] )
%matplotlib inline
NUMBER_DAYS = 100
NUMBER_FAMILIES = 5000
MAX_BEST_CHOICE = 5
data = pd.read_csv('/kaggle/input/santa-workshop-tour-2019/family_data.csv')
 submission = pd.read_csv('/kaggle/input/santas-workshop-tour-2019-optimal-solution/preference_cost_43622.csv')
 assigned_days = submission['assigned_day'].values
columns = data.columns[1:11]
DESIRED = data[columns].values
COST_PER_FAMILY = [0,50,50,100,200,200,300,300,400,500]
COST_PER_FAMILY_MEMBER = [0, 0, 9, 9, 9, 18, 18, 36, 36,235]
N_PEOPLE = data['n_people'].values
def get_daily_occupancy(assigned_days):
daily_occupancy = np.zeros(100, int)
 for fid, assigned_day in enumerate(assigned_days):
daily_occupancy[assigned_day-1] += N_PEOPLE[fid]
return daily_occupancy
def days_plot(assigned_days):
daily_occupancy = get_daily_occupancy(assigned_days)
 best_choices = get_daily_occupancy(DESIRED[:,0])
 plt.rcParams['figure.figsize'] = [20, 5]
plt.xticks(np.arange(1, 101, step=1), rotation=90)
 plt.axhline(y=125, color='gray', linestyle=':')
 plt.axhline(y=300, color='gray', linestyle=':')
 mondays125 = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 == 1 and daily_occupancy[day] == 125])
 other_mondays = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 == 1 and daily_occupancy[day] != 125])
 weekends = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 in [2,3,4] or day == 0])
 not_weekends = np.array([(day+1, daily_occupancy[day])for day in range(1, 100)if day % 7 in [0,5,6]])
 plt.bar(*weekends.transpose() , color = 'y', label = 'Weekends')
 plt.bar(*not_weekends.transpose() , color = 'b', label = 'Thu-Wed-Tue')
 plt.bar(*other_mondays.transpose() , color = 'm', label = 'Mondays > 125')
 plt.bar(*mondays125.transpose() , color = 'g', label = 'Mondays = 125')
 plt.plot(range(1,101), best_choices, color = 'k', label = 'Best choices')
 plt.ylim(0, 500)
 plt.xlim(0, 101)
 plt.xlabel('Days before Christmas', fontsize=14)
 plt.ylabel('Occupancy', fontsize=14)
 plt.legend()
plt.show()
def days_plot2(assigned_days):
daily_occupancy = get_daily_occupancy(assigned_days)
 best_choices = get_daily_occupancy(DESIRED[:,0])
 plt.rcParams['figure.figsize'] = [20, 5]
plt.xticks(np.arange(1, 101, step=1), rotation=90)
 plt.axhline(y=125, color='gray', linestyle=':')
 plt.axhline(y=300, color='gray', linestyle=':')
 mondays = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 == 1])
 weekends = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 in [2,3,4] or day == 0])
 not_weekends = np.array([(day+1, daily_occupancy[day])for day in range(1, 100)if day % 7 in [0,5,6]])
 plt.bar(*weekends.transpose() , color = 'y', label = 'Weekends')
 plt.bar(*not_weekends.transpose() , color = 'b', label = 'Thu-Wed-Tue')
 plt.bar(*mondays.transpose() , color = 'm', label = 'Mondays')
 plt.plot(range(1,101), best_choices, color = 'k', label = 'Best choices')
 plt.ylim(0, 500)
 plt.xlim(0, 101)
 plt.xlabel('Days before Christmas', fontsize=14)
 plt.ylabel('Occupancy', fontsize=14)
 plt.legend()
plt.show()
def cost_function(prediction):
N_DAYS = 100
MAX_OCCUPANCY = 300
MIN_OCCUPANCY = 125
penalty = 0
days = list(range(N_DAYS,0,-1))
tmp = pd.read_csv('/kaggle/input/santa-workshop-tour-2019/family_data.csv', index_col='family_id')
 family_size_dict = tmp[['n_people']].to_dict() ['n_people']
cols = [f'choice_{i}' for i in range(10)]
choice_dict = tmp[cols].to_dict()
daily_occupancy = {k:0 for k in days}
for f, d in enumerate(prediction):
n = family_size_dict[f]
choice_0 = choice_dict['choice_0'][f]
choice_1 = choice_dict['choice_1'][f]
choice_2 = choice_dict['choice_2'][f]
choice_3 = choice_dict['choice_3'][f]
choice_4 = choice_dict['choice_4'][f]
choice_5 = choice_dict['choice_5'][f]
choice_6 = choice_dict['choice_6'][f]
choice_7 = choice_dict['choice_7'][f]
choice_8 = choice_dict['choice_8'][f]
choice_9 = choice_dict['choice_9'][f]
daily_occupancy[d] += n
if d == choice_0:
penalty += 0
elif d == choice_1:
penalty += 50
elif d == choice_2:
penalty += 50 + 9 * n
elif d == choice_3:
penalty += 100 + 9 * n
elif d == choice_4:
penalty += 200 + 9 * n
elif d == choice_5:
penalty += 200 + 18 * n
elif d == choice_6:
penalty += 300 + 18 * n
elif d == choice_7:
penalty += 300 + 36 * n
elif d == choice_8:
penalty += 400 + 36 * n
elif d == choice_9:
penalty += 500 + 36 * n + 199 * n
else:
penalty += 500 + 36 * n + 398 * n
for _, v in daily_occupancy.items() :
if(v < MIN_OCCUPANCY):
penalty += 100000000
accounting_cost =(daily_occupancy[days[0]]-125.0)/ 400.0 * daily_occupancy[days[0]]**(0.5)
 accounting_costs = [max(0, accounting_cost)]
diffs = [0]
yesterday_count = daily_occupancy[days[0]]
for day in days[1:]:
today_count = daily_occupancy[day]
diff = abs(today_count - yesterday_count)
 accounting_costs.append(max(0,(today_count-125.0)/ 400.0 * today_count**(0.5 + diff / 50.0)))
 yesterday_count = today_count
return penalty, sum(accounting_costs), penalty + sum(accounting_costs)
 def print_cost_function(prediction):
cfa = cost_function(prediction)
 print("Preference cost: ",cfa[0])
 print("Accounting penalty: ",cfa[1])
 print("Score: ",cfa[2])
 days_plot(assigned_days )<compute_test_metric>
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_1 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='log2', max_depth = 4))
clf_2 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='auto', max_depth = 4))
clf_1.fit(x_t, y_t)
 clf_2.fit(x_t, y_t)
 sel_feat_1 = x_t.columns[(clf_1.get_support())]
sel_feat_2 = x_t.columns[(clf_2.get_support())]
print(sel_feat_1)
 print(sel_feat_2 )
print_cost_function(assigned_days )<load_from_csv>
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_3 = SelectFromModel(DecisionTreeClassifier(max_features='log2'))
clf_4 = SelectFromModel(DecisionTreeClassifier(max_features='auto'))
clf_3.fit(x_t, y_t)
 clf_4.fit(x_t, y_t)
 sel_feat_3 = x_t.columns[(clf_3.get_support())]
sel_feat_4 = x_t.columns[(clf_4.get_support())]
print(sel_feat_3)
 print(sel_feat_4 )
submission = pd.read_csv('/kaggle/input/santas-workshop-tour-2019-optimal-solution/accounting_penalty_313.27.csv')
 assigned_days = submission['assigned_day'].values
days_plot2(assigned_days )<compute_test_metric>
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_5 = SelectFromModel(ExtraTreesClassifier(max_features='log2'))
clf_6 = SelectFromModel(ExtraTreesClassifier(max_features='auto'))
clf_5.fit(x_t, y_t)
 clf_6.fit(x_t, y_t)
 sel_feat_5 = x_t.columns[(clf_5.get_support())]
sel_feat_6 = x_t.columns[(clf_5.get_support())]
print(sel_feat_5)
 print(sel_feat_6 )
print_cost_function(assigned_days )<load_from_csv>
clf_1 = RandomForestClassifier()
x_train_1 = df[['LotFrontage', 'LotArea', 'Neighborhood', 'Condition1','Condition2',
'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd', 'RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrArea', 'ExterQual', 'BsmtFinSF1','BsmtFinSF2',
'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional',
'GarageYrBlt', 'GarageCars', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal']]
y_train = df['SalePrice']
viz_1 = FeatureImportances(clf_1)
 viz_1.fit(x_train_1, y_train)
 viz_1 = FeatureImportances(clf_1)
 viz_1.fit(x_train_1, y_train)
 viz_1.poof()
clf_2 = DecisionTreeClassifier()
x_train_2 = df[['LotFrontage', 'LotArea', 'LotShape', 'LotConfig', 'Neighborhood',
'HouseStyle', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd',
'Exterior1st', 'Exterior2nd', 'MasVnrArea', 'BsmtExposure',
'BsmtFinType1', 'BsmtFinSF1', 'BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC',
'1stFlrSF', '2ndFlrSF', 'GrLivArea', 'BedroomAbvGr','TotRmsAbvGrd',
'Fireplaces','GarageType','GarageYrBlt', 'GarageFinish', 'GarageArea', 'WoodDeckSF',
'OpenPorchSF', 'MoSold', 'YrSold']]
viz_2 = FeatureImportances(clf_2)
 viz_2.fit(x_train_2, y_train)
 viz_2 = FeatureImportances(clf_2)
 viz_2.fit(x_train_2, y_train)
 viz_2.poof()
clf_3 = ExtraTreesClassifier()
x_train_3 = df[['MSSubClass', 'LotFrontage', 'LotArea', 'LotShape', 'LotConfig',
'Neighborhood', 'OverallQual', 'OverallCond', 'YearBuilt',
'YearRemodAdd', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
'MasVnrArea', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1', 'BsmtUnfSF',
'TotalBsmtSF', 'HeatingQC', '1stFlrSF', '2ndFlrSF', 'GrLivArea',
'BsmtFullBath', 'BedroomAbvGr', 'TotRmsAbvGrd', 'Fireplaces',
'GarageYrBlt', 'GarageFinish', 'GarageArea', 'WoodDeckSF',
'OpenPorchSF', 'MoSold', 'YrSold']]
viz_3 = FeatureImportances(clf_3)
 viz_3.fit(x_train_3, y_train)
 viz_3 = FeatureImportances(clf_3)
 viz_3.fit(x_train_3, y_train)
 viz_3.poof()
submission = pd.read_csv('/kaggle/input/santas-workshop-tour-2019-optimal-solution/submission_68888.04.csv')
 assigned_days = submission['assigned_day'].values 
<install_modules>
df_train = df[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch','MoSold', 'YrSold','SalePrice']]
!pip install pulp<define_variables>
df_train['LotArea']=np.log(df_train['LotArea'] )
%%time
daily_occupancy = get_daily_occupancy(assigned_days)
 lower_bounds = np.zeros(NUMBER_DAYS)
 upper_bounds = np.zeros(NUMBER_DAYS)
 delta = 0
for fi in range(NUMBER_DAYS):
lower_bounds[fi] = max(daily_occupancy[fi]-delta,125)
 upper_bounds[fi] = min(daily_occupancy[fi]+delta,300)
 prob = LpProblem("Setup occupation of days", LpMinimize)
 PCOSTM, B = {}, {}
for fid in range(NUMBER_FAMILIES):
for i in range(MAX_BEST_CHOICE):
B[fid, DESIRED[fid][i]-1] = LpVariable(f'b{fid, i}', 0, 1, LpInteger)
 if assigned_days[fid] == DESIRED[fid][i]:
B[fid, DESIRED[fid][i]-1].setInitialValue(1)
 else:
B[fid, DESIRED[fid][i]-1].setInitialValue(0)
 PCOSTM[fid, DESIRED[fid][i]-1] = COST_PER_FAMILY[i] + N_PEOPLE[fid] * COST_PER_FAMILY_MEMBER[i]
D = {}
for j in range(NUMBER_DAYS):
rj = range(int(lower_bounds[j]),int(upper_bounds[j])+1)
 for i in rj:
if j<99:
rj1 = range(int(lower_bounds[j+1]),int(upper_bounds[j+1])+1)
 for i1 in rj1:
D[j, i,i1] = LpVariable(f'D{j, i,i1}', 0, 1, LpInteger)
 if daily_occupancy[j] == i and daily_occupancy[j+1] == i1:
D[j, i,i1].setInitialValue(1)
 else:
D[j, i,i1].setInitialValue(0)
 else:
D[j,i,i] = LpVariable(f'D{j, i,i}', 0, 1, LpInteger)
 if daily_occupancy[j] == i:
D[j, i,i].setInitialValue(1)
 else:
D[j, i,i].setInitialValue(0)
 prob += lpSum(PCOSTM[i, j] * B[i, j] for i, j in B)+ lpSum(D[j,i,i1]*(int(i)-125.0)/400.0*int(i)**(0.5+abs(i-i1)/50.0)for j,i,i1 in D)
 I = {}
for j in range(NUMBER_DAYS):
I[j] = LpVariable(f'I{j}', int(lower_bounds[j]), int(upper_bounds[j]), LpInteger)
 I[j].setInitialValue(daily_occupancy[j])
 prob += lpSum([N_PEOPLE[i] * B[i, j] for i in range(NUMBER_FAMILIES)if(i,j)in B])== I[j]
for j in range(NUMBER_DAYS):
rj = range(int(lower_bounds[j]),int(upper_bounds[j])+1)
 if j<99:
rj1 = range(int(lower_bounds[j+1]),int(upper_bounds[j+1])+1)
 prob += lpSum([D[j, i,i1]*i for i in rj for i1 in rj1])== I[j]
prob += lpSum([D[j, i,i1]*i1 for i in rj for i1 in rj1])== I[j+1]
else:
prob += lpSum([D[j, i,i]*i for i in rj])== I[j]
for i in range(NUMBER_FAMILIES):
prob += lpSum(B[i, j] for j in range(NUMBER_DAYS)if(i,j)in B)== 1
prob.solve()
print("Status:", LpStatus[prob.status])
 print("Score = ", value(prob.objective))<compute_test_metric>
x_train = df_train[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']]
print_cost_function(assigned_days )<save_to_csv>
y_train = df_train['SalePrice']
submission['assigned_day'] = assigned_days
submission.to_csv('submission_68888.04.csv', index=False )<load_from_csv>
valgrid = {'n_estimators':hp.quniform('n_estimators', 1500, 2500, 25),
'gamma':hp.uniform('gamma', 0.01, 0.05),
'base_score':hp.uniform('base_score',0.6,0.9),
'learning_rate':hp.uniform('learning_rate', 0.00001, 0.03),
'max_depth':hp.quniform('max_depth', 3,8,1),
'subsample':hp.uniform('subsample', 0.50, 0.95),
'colsample_bytree':hp.uniform('colsample_bytree', 0.50, 0.95),
'colsample_bylevel':hp.uniform('colsample_bylevel', 0.50, 0.95),
'colsample_bynode':hp.uniform('colsample_bynode', 0.50, 0.95),
'reg_lambda':hp.uniform('reg_lambda', 1, 20)
 }
def objective(params):
params = {'n_estimators': int(params['n_estimators']),
'gamma': params['gamma'],
'base_score': params['base_score'],
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'subsample': params['subsample'],
'colsample_bytree': params['colsample_bytree'],
'colsample_bylevel': params['colsample_bylevel'],
'colsample_bynode': params['colsample_bynode'],
'reg_lambda': params['reg_lambda']}
xb_a= xgb.XGBRegressor(**params)
 score = cross_val_score(xb_a, x_train, y_train, scoring='neg_mean_squared_error', cv=5, n_jobs=-1 ).mean()
return -score
bestP = fmin(fn= objective, space= valgrid, max_evals=20, rstate=np.random.RandomState(42), algo=tpe.suggest )
%%time
NUMBER_DAYS = 100
NUMBER_FAMILIES = 5000
MAX_BEST_CHOICE = 5
data = pd.read_csv('/kaggle/input/santa-workshop-tour-2019/family_data.csv')
 submission = pd.read_csv('/kaggle/input/c-stochastic-product-search-65ns/submission.csv')
 assigned_days = submission['assigned_day'].values
columns = data.columns[1:11]
DESIRED = data[columns].values
COST_PER_FAMILY = [0,50,50,100,200,200,300,300,400,500]
COST_PER_FAMILY_MEMBER = [0, 0, 9, 9, 9, 18, 18, 36, 36,235]
N_PEOPLE = data['n_people'].values
def get_daily_occupancy(assigned_days):
daily_occupancy = np.zeros(100, int)
 for fid, assigned_day in enumerate(assigned_days):
daily_occupancy[assigned_day-1] += N_PEOPLE[fid]
return daily_occupancy
def cost_function(prediction):
N_DAYS = 100
MAX_OCCUPANCY = 300
MIN_OCCUPANCY = 125
penalty = 0
days = list(range(N_DAYS,0,-1))
tmp = pd.read_csv('/kaggle/input/santa-workshop-tour-2019/family_data.csv', index_col='family_id')
 family_size_dict = tmp[['n_people']].to_dict() ['n_people']
cols = [f'choice_{i}' for i in range(10)]
choice_dict = tmp[cols].to_dict()
daily_occupancy = {k:0 for k in days}
for f, d in enumerate(prediction):
n = family_size_dict[f]
choice_0 = choice_dict['choice_0'][f]
choice_1 = choice_dict['choice_1'][f]
choice_2 = choice_dict['choice_2'][f]
choice_3 = choice_dict['choice_3'][f]
choice_4 = choice_dict['choice_4'][f]
choice_5 = choice_dict['choice_5'][f]
choice_6 = choice_dict['choice_6'][f]
choice_7 = choice_dict['choice_7'][f]
choice_8 = choice_dict['choice_8'][f]
choice_9 = choice_dict['choice_9'][f]
daily_occupancy[d] += n
if d == choice_0:
penalty += 0
elif d == choice_1:
penalty += 50
elif d == choice_2:
penalty += 50 + 9 * n
elif d == choice_3:
penalty += 100 + 9 * n
elif d == choice_4:
penalty += 200 + 9 * n
elif d == choice_5:
penalty += 200 + 18 * n
elif d == choice_6:
penalty += 300 + 18 * n
elif d == choice_7:
penalty += 300 + 36 * n
elif d == choice_8:
penalty += 400 + 36 * n
elif d == choice_9:
penalty += 500 + 36 * n + 199 * n
else:
penalty += 500 + 36 * n + 398 * n
for _, v in daily_occupancy.items() :
if(v < MIN_OCCUPANCY):
penalty += 100000000
accounting_cost =(daily_occupancy[days[0]]-125.0)/ 400.0 * daily_occupancy[days[0]]**(0.5)
 accounting_costs = [max(0, accounting_cost)]
diffs = [0]
yesterday_count = daily_occupancy[days[0]]
for day in days[1:]:
today_count = daily_occupancy[day]
diff = abs(today_count - yesterday_count)
 accounting_costs.append(max(0,(today_count-125.0)/ 400.0 * today_count**(0.5 + diff / 50.0)))
 yesterday_count = today_count
return penalty, sum(accounting_costs), penalty + sum(accounting_costs)
 seed(2)
 for f in range(100):
ad = assigned_days.copy()
days_for_fix = np.array(sample(range(1,101),50))
daily_occupancy = get_daily_occupancy(ad)
 fids = np.where(np.isin(ad, days_for_fix)) [0]
solver = pywraplp.Solver('Setup occupation of days', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
 PCOSTM, B = {}, {}
for fid in fids:
for i in range(MAX_BEST_CHOICE):
B[fid, DESIRED[fid][i]-1] = solver.BoolVar(f'b{fid, i}')
 PCOSTM[fid, DESIRED[fid][i]-1] = COST_PER_FAMILY[i] + N_PEOPLE[fid] * COST_PER_FAMILY_MEMBER[i]
lower_bounds = np.zeros(100)
 upper_bounds = np.zeros(100)
 delta = 8
for fi in days_for_fix:
lower_bounds[fi-1] = max(daily_occupancy[fi-1]-delta,125)
 upper_bounds[fi-1] = min(daily_occupancy[fi-1]+delta,300)
 D = {}
for j in range(NUMBER_DAYS):
I = solver.IntVar(lower_bounds[j], upper_bounds[j], f'I{j}')
 solver.Add(solver.Sum([N_PEOPLE[i] * B[i, j] for i in range(NUMBER_FAMILIES)if(i,j)in B])== I)
 if upper_bounds[j]>124:
rj = range(int(lower_bounds[j]),int(upper_bounds[j])+1)
 for i in rj:
D[j, i] = solver.BoolVar(f'd{j, i}')
 solver.Add(solver.Sum([D[j, i]*i for i in rj])== I)
 for i in fids:
solver.Add(solver.Sum(B[i, j] for j in range(NUMBER_DAYS)if(i,j)in B)== 1)
 sM =solver.Sum(PCOSTM[i, j] * B[i, j] for i, j in B)
 for i in range(NUMBER_DAYS):
if np.isin(i,days_for_fix-1):
ri = range(int(lower_bounds[i]),int(upper_bounds[i])+1)
 if i<99:
sM += solver.Sum(D[i,j]*(j-125)/400*j**(0.5+abs(j-daily_occupancy[i+1])/50)for j in ri)
 if i>0:
sM += solver.Sum(D[i,j]*(daily_occupancy[i-1]-125)/400*daily_occupancy[i-1]**(0.5+abs(j-daily_occupancy[i-1])/50)for j in ri)
 solver.Minimize(sM)
 sol = solver.Solve()
status = ['OPTIMAL', 'FEASIBLE', 'INFEASIBLE', 'UNBOUNDED', 'ABNORMAL', 'MODEL_INVALID', 'NOT_SOLVED']
if status[sol] == 'OPTIMAL':
for i, j in B:
if B[i, j].solution_value() > 0.5:
ad[i] = j+1
if cost_function(ad)[2]<cost_function(assigned_days)[2]:
submission['assigned_day'] = ad
assigned_days = ad
print(cost_function(ad))
score = cost_function(assigned_days)[2]
submission.to_csv(f'submission_{score}.csv', index=False)
<load_from_csv>
df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 total = df_test[['MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']].isnull().sum().sort_values(ascending=False)
 percent =(df_test.isnull().sum() /df_test.isnull().count() ).sort_values(ascending=False)
 missing_test = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_test.head(20 )
%matplotlib inline
NUMBER_DAYS = 100
NUMBER_FAMILIES = 5000
data = pd.read_csv('/kaggle/input/santa-workshop-tour-2019/family_data.csv')
 submission = pd.read_csv('/kaggle/input/best-submission-31-12a/submission.csv')
 assigned_days = submission['assigned_day'].values
columns = data.columns[1:11]
DESIRED = data[columns].values
COST_PER_FAMILY = [0,50,50,100,200,200,300,300,400,500]
COST_PER_FAMILY_MEMBER = [0, 0, 9, 9, 9, 18, 18, 36, 36,235]
N_PEOPLE = data['n_people'].values
def get_daily_occupancy(assigned_days):
daily_occupancy = np.zeros(100, int)
 for fid, assigned_day in enumerate(assigned_days):
daily_occupancy[assigned_day-1] += N_PEOPLE[fid]
return daily_occupancy
def days_plot(assigned_days):
daily_occupancy = get_daily_occupancy(assigned_days)
 best_choices = get_daily_occupancy(DESIRED[:,0])
 plt.rcParams['figure.figsize'] = [20, 5]
plt.xticks(np.arange(1, 101, step=1), rotation=90)
 plt.axhline(y=125, color='gray', linestyle=':')
 plt.axhline(y=300, color='gray', linestyle=':')
 mondays125 = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 == 1 and daily_occupancy[day] == 125])
 other_mondays = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 == 1 and daily_occupancy[day] != 125])
 weekends = np.array([(day+1, daily_occupancy[day])for day in range(100)if day % 7 in [2,3,4] or day == 0])
 not_weekends = np.array([(day+1, daily_occupancy[day])for day in range(1, 100)if day % 7 in [0,5,6]])
 plt.bar(*weekends.transpose() , color = 'y', label = 'Weekends')
 plt.bar(*not_weekends.transpose() , color = 'b', label = 'Thu-Wed-Tue')
 plt.bar(*other_mondays.transpose() , color = 'm', label = 'Mondays > 125')
 plt.bar(*mondays125.transpose() , color = 'g', label = 'Mondays = 125')
 plt.plot(range(1,101), best_choices, color = 'k', label = 'Best choices')
 plt.ylim(0, 500)
 plt.xlim(0, 101)
 plt.xlabel('Days before Christmas', fontsize=14)
 plt.ylabel('Occupancy', fontsize=14)
 plt.legend()
plt.show()
def cost_function(prediction):
N_DAYS = 100
MAX_OCCUPANCY = 300
MIN_OCCUPANCY = 125
penalty = 0
days = list(range(N_DAYS,0,-1))
tmp = pd.read_csv('/kaggle/input/santa-workshop-tour-2019/family_data.csv', index_col='family_id')
 family_size_dict = tmp[['n_people']].to_dict() ['n_people']
cols = [f'choice_{i}' for i in range(10)]
choice_dict = tmp[cols].to_dict()
daily_occupancy = {k:0 for k in days}
for f, d in enumerate(prediction):
n = family_size_dict[f]
choice_0 = choice_dict['choice_0'][f]
choice_1 = choice_dict['choice_1'][f]
choice_2 = choice_dict['choice_2'][f]
choice_3 = choice_dict['choice_3'][f]
choice_4 = choice_dict['choice_4'][f]
choice_5 = choice_dict['choice_5'][f]
choice_6 = choice_dict['choice_6'][f]
choice_7 = choice_dict['choice_7'][f]
choice_8 = choice_dict['choice_8'][f]
choice_9 = choice_dict['choice_9'][f]
daily_occupancy[d] += n
if d == choice_0:
penalty += 0
elif d == choice_1:
penalty += 50
elif d == choice_2:
penalty += 50 + 9 * n
elif d == choice_3:
penalty += 100 + 9 * n
elif d == choice_4:
penalty += 200 + 9 * n
elif d == choice_5:
penalty += 200 + 18 * n
elif d == choice_6:
penalty += 300 + 18 * n
elif d == choice_7:
penalty += 300 + 36 * n
elif d == choice_8:
penalty += 400 + 36 * n
elif d == choice_9:
penalty += 500 + 36 * n + 199 * n
else:
penalty += 500 + 36 * n + 398 * n
for _, v in daily_occupancy.items() :
if(v < MIN_OCCUPANCY):
penalty += 100000000
accounting_cost =(daily_occupancy[days[0]]-125.0)/ 400.0 * daily_occupancy[days[0]]**(0.5)
 accounting_costs = [max(0, accounting_cost)]
diffs = [0]
yesterday_count = daily_occupancy[days[0]]
for day in days[1:]:
today_count = daily_occupancy[day]
diff = abs(today_count - yesterday_count)
 accounting_costs.append(max(0,(today_count-125.0)/ 400.0 * today_count**(0.5 + diff / 50.0)))
 yesterday_count = today_count
return penalty, sum(accounting_costs), penalty + sum(accounting_costs)
 days_plot(assigned_days)
 print("Score: ", cost_function(assigned_days))<define_variables>
df_test['LotFrontage']=df_test['LotFrontage'].fillna(df_test['LotFrontage'].mean())
 df_test['GarageFinish']=df_test['GarageFinish'].fillna(df_test['GarageFinish'].mode() [0])
 df_test['GarageYrBlt']=df_test['GarageYrBlt'].fillna(df_test['GarageYrBlt'].mean())
 df_test['GarageType']=df_test['GarageType'].fillna(df_test['GarageType'].mode() [0])
 df_test['BsmtExposure']=df_test['BsmtExposure'].fillna(df_test['BsmtExposure'].mode() [0])
 df_test['BsmtFinType1']=df_test['BsmtFinType1'].fillna(df_test['BsmtFinType1'].mode() [0])
 df_test['MasVnrType']=df_test['MasVnrType'].fillna(df_test['MasVnrType'].mode() [0])
 df_test['MasVnrArea']=df_test['MasVnrArea'].fillna(df_test['MasVnrArea'].mean())
 df_test['BsmtFullBath']=df_test['BsmtFullBath'].fillna(df_test['BsmtFullBath'].mode() [0])
 df_test['Functional']=df_test['Functional'].fillna(df_test['Functional'].mode() [0])
 df_test['KitchenQual']=df_test['KitchenQual'].fillna(df_test['KitchenQual'].mode() [0])
 df_test['GarageArea']=df_test['GarageArea'].fillna(df_test['GarageArea'].mean())
 df_test['BsmtUnfSF']=df_test['BsmtUnfSF'].fillna(df_test['BsmtUnfSF'].mean())
 df_test['Exterior2nd']=df_test['Exterior2nd'].fillna(df_test['Exterior2nd'].mode() [0])
 df_test['Exterior1st']=df_test['Exterior1st'].fillna(df_test['Exterior1st'].mode() [0])
 df_test['BsmtFinSF1']=df_test['BsmtFinSF1'].fillna(df_test['BsmtFinSF1'].mean())
 df_test['BsmtFinSF2']=df_test['BsmtFinSF2'].fillna(df_test['BsmtFinSF2'].mean())
 df_test['TotalBsmtSF']=df_test['TotalBsmtSF'].fillna(df_test['TotalBsmtSF'].mean())
 df_test['GarageCars']=df_test['GarageCars'].fillna(df_test['GarageCars'].mean() )
print("Day allocations:")
 print(np.arange(1, 100)[np.abs(np.diff(get_daily_occupancy(assigned_days)) * -1)> 25] )<feature_engineering>
df_test['LotArea']=np.log(df_test['LotArea'] )
days_for_fix = np.array([37])
 daily_occupancy = get_daily_occupancy(assigned_days)
 fids = np.where(np.isin(assigned_days, days_for_fix)) [0]
MAX_BEST_CHOICE = 3
solver = pywraplp.Solver('Setup occupation of days', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
 PCOSTM, B = {}, {}
for fid in fids:
for i in range(MAX_BEST_CHOICE):
B[fid, DESIRED[fid][i]-1] = solver.BoolVar(f'b{fid, i}')
 PCOSTM[fid, DESIRED[fid][i]-1] = COST_PER_FAMILY[i] + N_PEOPLE[fid] * COST_PER_FAMILY_MEMBER[i]
lower_bounds = np.zeros(100)
 upper_bounds = 300.- daily_occupancy
upper_bounds[np.arange(100)%7 == 1] = 0
lower_bounds[days_for_fix-1] = 125
upper_bounds[days_for_fix-1] = 125
for j in range(NUMBER_DAYS):
I = solver.IntVar(lower_bounds[j], upper_bounds[j], f'I{j}')
 solver.Add(solver.Sum([N_PEOPLE[i] * B[i, j] for i in range(NUMBER_FAMILIES)if(i,j)in B])== I)
 for i in fids:
solver.Add(solver.Sum(B[i, j] for j in range(NUMBER_DAYS)if(i,j)in B)== 1)
 solver.Minimize(solver.Sum(PCOSTM[i, j] * B[i, j] for i, j in B))
sol = solver.Solve()
status = ['OPTIMAL', 'FEASIBLE', 'INFEASIBLE', 'UNBOUNDED', 'ABNORMAL', 'MODEL_INVALID', 'NOT_SOLVED']
if status[sol] == 'OPTIMAL':
for i, j in B:
if B[i, j].solution_value() > 0.5:
assigned_days[i] = j+1
print('Solution: ', status[sol])
 print("Score: ", cost_function(assigned_days))
days_plot(assigned_days)
 submission['assigned_day'] = assigned_days
submission.to_csv('submission.csv', index=False )<load_from_csv>
df_test_work = df_test[['Id','MSSubClass','LotFrontage','LotArea','LotShape','LotConfig', 'Neighborhood','HouseStyle',
'Condition1','OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle',
'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'BsmtExposure','BsmtFinType1',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC','1stFlrSF', '2ndFlrSF', 'GrLivArea',
'FullBath', 'BsmtFullBath','BedroomAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Functional','Fireplaces',
'GarageType','GarageYrBlt', 'GarageCars', 'GarageFinish', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'ScreenPorch', 'MoSold', 'YrSold']]
lencoders_test = {}
for colt in df_test_work.select_dtypes(include=['object'] ).columns:
lencoders_test[colt] = LabelEncoder()
df_test_work[colt] = lencoders_test[colt].fit_transform(df_test_work[colt] )
def pytorch_optimim(MAX_CHOICE = 6):
root_path = Path(r'.. /input/santa-workshop-tour-2019')
 best_submit_path = Path(r'.. /input/mip-optimization-preference-cost/')
 fpath = root_path / 'family_data.csv'
data = pd.read_csv(fpath, index_col='family_id')
 data_choices = data.values
fpath = root_path / 'sample_submission.csv'
submission = pd.read_csv(fpath, index_col='family_id')
 dummies = []
for i in range(MAX_CHOICE):
tmp = pd.get_dummies(data[f'choice_{i}'] ).values*(data['n_people'].values.reshape(-1,1))
dummies.append((
np.concatenate([tmp, tmp[:, -1].reshape(-1,1)], axis=1)
 ).reshape(5000, 101, 1))
dummies = np.concatenate(dummies, axis=2)
 dummies = np.swapaxes(dummies, 1, 2)
 penalties = {n: [0, 50, 50 + 9 * n, 100 + 9 * n, 200 + 9 * n, 200 + 18 * n, 300 + 18 * n, 300 + 36 * n, 400 + 36 * n, 500 + 36 * n + 199 * n] for n in np.unique(data['n_people'])}
mat = []
for i in range(5000):
n = data.iloc[i]['n_people']
mat.append(penalties[n][:MAX_CHOICE])
 mat = np.array(mat)
 def create_init(initial_sub):
fam_choices = data
a = pd.merge(initial_sub, fam_choices, on='family_id')
 initial_choices = []
for i in range(MAX_CHOICE):
initial_choices.append(((a[f'choice_{i}'] == a['assigned_day'])).values.reshape(-1,1))
initial_choices = np.concatenate(initial_choices, axis=1)
 initial_choices = torch.tensor(
 initial_choices*10
, dtype=torch.float32)
 return initial_choices
initial_sub = pd.read_csv('./submission.csv')
 initial_choices = create_init(initial_sub)
 family_sizes = data.n_people.values.astype(np.int8)
 cost_dict = {0: [ 0, 0],
1: [ 50, 0],
2: [ 50, 9],
3: [100, 9],
4: [200, 9],
5: [200, 18],
6: [300, 18],
7: [300, 36],
8: [400, 36],
9: [500, 36 + 199],
10: [500, 36 + 398],
}
def cost(choice, members, cost_dict):
x = cost_dict[choice]
return x[0] + members * x[1]
all_costs = {k: pd.Series([cost(k, x, cost_dict)for x in range(2,9)], index=range(2,9)) for k in cost_dict.keys() }
df_all_costs = pd.DataFrame(all_costs)
 family_cost_matrix = np.zeros(( 100,len(family_sizes)))
 for i, el in enumerate(family_sizes):
family_cost_matrix[:, i] += all_costs[10][el]
for j, choice in enumerate(data.drop("n_people",axis=1 ).values[i,:]):
family_cost_matrix[choice-1, i] = all_costs[j][el]
def accounting(today, previous):
return(( today - 125)/ 400)* today ** (.5 +(abs(today - previous)/ 50))
acc_costs = np.zeros([176,176])
 for i, x in enumerate(range(125,300+1)) :
for j, y in enumerate(range(125,300+1)) :
acc_costs[i,j] = accounting(x,y)
 @njit(fastmath=True)
 def cost_function(prediction, family_size, family_cost_matrix, accounting_cost_matrix):
N_DAYS = 100
MAX_OCCUPANCY = 300
MIN_OCCUPANCY = 125
penalty = 0
accounting_cost = 0
max_occ = False
daily_occupancy = np.zeros(N_DAYS + 1, dtype=np.int16)
 for i,(pred, n)in enumerate(zip(prediction, family_size)) :
daily_occupancy[pred - 1] += n
penalty += family_cost_matrix[pred - 1, i]
daily_occupancy[-1] = daily_occupancy[-2]
for day in range(N_DAYS):
n_next = daily_occupancy[day + 1]
n = daily_occupancy[day]
max_occ += MIN_OCCUPANCY > n
max_occ += MAX_OCCUPANCY < n
accounting_cost += accounting_cost_matrix[n-MIN_OCCUPANCY, n_next-MIN_OCCUPANCY]
if max_occ:
return 1e11
return penalty+accounting_cost
print(cost_function(initial_sub['assigned_day'].values, family_sizes, family_cost_matrix, acc_costs))
class Model(nn.Module):
def __init__(self, mat, dummies):
super().__init__()
self.mat = torch.from_numpy(mat ).type(torch.int16)
 self.dummies = torch.from_numpy(dummies ).type(torch.float32)
 self.weight = torch.nn.Parameter(data=torch.Tensor(5000, MAX_CHOICE ).type(torch.float32)
 , requires_grad=True)
 self.weight.data.copy_(initial_choices)
 def forward(self):
prob = F.softmax(self.weight,dim=1)
 x =(prob * self.mat ).sum()
daily_occupancy = torch.zeros(101, dtype=torch.float32)
 for i in range(MAX_CHOICE):
daily_occupancy +=(prob[:, i]@self.dummies[:, i, :])
 diff = torch.abs(daily_occupancy[:-1] - daily_occupancy[1:])
 daily_occupancy = daily_occupancy[:-1]
y =(
 torch.relu(daily_occupancy-125.0)/ 400.0 * daily_occupancy**(0.5 + diff / 50.0)
 ).sum()
v =(( torch.relu(125-daily_occupancy)) **2+(torch.relu(daily_occupancy-300)) **2 ).sum()
entropy_loss = -1.0 *(prob * F.log_softmax(self.weight, dim=1)).sum()
return x, y, v*10000, entropy_loss
model = Model(mat, dummies)
 best_score = 10e10
best_pos = None
optimizer = torch.optim.Adam(model.parameters() , lr = 0.1)
 for epoch in tqdm.tqdm_notebook(range(100_001)) :
optimizer.zero_grad()
x, y, v, ent = model()
loss = x + y + v + 0*ent
loss.backward()
optimizer.step()
pos = model.weight.argmax(1 ).cpu().numpy()
pred = []
for i in range(5000):
pred.append(data_choices[i, pos[i]])
 pred = np.array(pred)
 score = cost_function(pred, family_sizes, family_cost_matrix, acc_costs)
 if(score < best_score):
best_score = score
best_pos = pred
print(best_score)
 submission['assigned_day'] = best_pos
submission.to_csv(f'submission.csv')
 if epoch % 1000 == 0:
x = np.round(x.item() ,1)
 y = np.round(y.item() ,1)
 print(f'{epoch}\t{x}\t{y} \t{np.round(score, 2)}')
 prev_best_score = best_score
coef = 1
count_failures = 0
for _ in range(10_000):
initial_sub = pd.read_csv('submission.csv')
 initial_choices = create_init(initial_sub)
 model = Model(mat, dummies)
 optimizer = torch.optim.Adam(model.parameters() , lr = 0.1)
 for epoch in tqdm.tqdm_notebook(range(1_001)) :
optimizer.zero_grad()
x, y, v, ent = model()
loss = x + coef*y + v + 0*ent
loss.backward()
optimizer.step()
pos = model.weight.argmax(1 ).cpu().numpy()
pred = []
for i in range(5000):
pred.append(data_choices[i, pos[i]])
 pred = np.array(pred)
 score = cost_function(pred, family_sizes, family_cost_matrix, acc_costs)
 if(score < best_score):
best_score = score
best_pos = pred
print(best_score)
 submission['assigned_day'] = best_pos
submission.to_csv(f'submission.csv')
 if(epoch % 1000 == 0)and epoch != 0:
x = np.round(x.item() ,1)
 y = np.round(y.item() ,1)
 print(f'{epoch}\t{x}\t{y} \t{np.round(score, 2)}')
 if best_score == prev_best_score:
count_failures += 1
if count_failures > 30:
break
coef = coef*1.01
else:
prev_best_score = best_score
count_failures = 0
coef = 1
prev_best_score = best_score
coef = 1
count_failures = 0
for _ in range(10_000):
initial_sub = pd.read_csv('submission.csv')
 initial_choices = create_init(initial_sub)
 model = Model(mat, dummies)
 optimizer = torch.optim.Adam(model.parameters() , lr = 0.1)
 for epoch in tqdm.tqdm_notebook(range(1_001)) :
optimizer.zero_grad()
x, y, v, ent = model()
loss = coef*x + y + v + 10*ent
loss.backward()
optimizer.step()
pos = model.weight.argmax(1 ).cpu().numpy()
pred = []
for i in range(5000):
pred.append(data_choices[i, pos[i]])
 pred = np.array(pred)
 score = cost_function(pred, family_sizes, family_cost_matrix, acc_costs)
 if(score < best_score):
best_score = score
best_pos = pred
print(best_score)
 submission['assigned_day'] = best_pos
submission.to_csv(f'submission.csv')
 if(epoch % 1000 == 0)and epoch != 0:
x = np.round(x.item() ,1)
 y = np.round(y.item() ,1)
 print(f'{epoch}\t{x}\t{y} \t{np.round(score, 2)}')
 if best_score == prev_best_score:
count_failures += 1
if count_failures > 20:
break
coef = coef*1.05
else:
prev_best_score = best_score
count_failures = 0
coef = 1
submission['assigned_day'] = best_pos
submission.to_csv(f'submission.csv' )<load_from_csv>
reg_final = xgboost.XGBRegressor(base_score=bestP['base_score'],
colsample_bylevel=bestP['colsample_bylevel'], colsample_bynode=bestP['colsample_bynode'],
colsample_bytree=bestP['colsample_bytree'],
gamma=bestP['gamma'], learning_rate=bestP['learning_rate'],
max_depth=int(bestP['max_depth']), n_estimators=int(bestP['n_estimators']),
random_state=42, reg_lambda=bestP['reg_lambda'], subsample=bestP['subsample'])
 reg_final.fit(x_train,y_train)
 y_pred = reg_final.predict(df_test_work)
 prediction = pd_out.DataFrame(y_pred)
 output = pd_out.concat([df_test_work['Id'],prediction], axis=1)
 output.columns=['Id','SalePrice']
output.to_csv('HousePrice_submission.csv', index=False)
 print("Submission successful" )