Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<categorify>
%matplotlib inline
SEED = 42
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case)
 train_input = bert_encoder(df.iloc[:, 3], tokenizer, max_len=160)
 test_input = bert_encoder(test.iloc[:, 3], tokenizer, max_len=160 )<train_model>
train = pd.read_csv(INPUT_DIR+'/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv(INPUT_DIR+'/house-prices-advanced-regression-techniques/test.csv' )
NUM_MODELS = 1
BATCH_SIZE = 16
LSTM_UNITS = 64
EPOCHS = 1
DENSE_HIDDEN_UNITS = 256
checkpoint_predictions = []
checkpoint_val_pred = []
weights = []
for model_idx in range(NUM_MODELS):
model = build_model(bert_layer, max_len=160)
 for global_epoch in range(EPOCHS):
history = model.fit(
 train_input, df.iloc[: ,4],
batch_size=BATCH_SIZE,
epochs=7,
verbose=1,
callbacks=[
LearningRateScheduler(lambda epoch: 2e-6 *(0.6** global_epoch))
] 
)
 checkpoint_predictions.append(model.predict(test_input, batch_size=32 ).flatten())
 weights.append(2 ** global_epoch)
 del model
gc.collect()<save_to_csv>
Y_train = train['SalePrice'].reset_index(drop=True)
 train=train.drop(['Id'], axis=1)
 test_ids = test['Id'].reset_index(drop=True)
 test=test.drop(['Id'], axis=1)
 print(len(train),len(test))
test_pred = np.average(checkpoint_predictions, weights=weights, axis=0)
 submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
df = pd.concat([train, test],sort=False)
 df = df.reset_index(drop=True )
train_df = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test_df = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 submission_df = pd.read_csv("/kaggle/input/digit-recognizer/sample_submission.csv" )<train_model>
def print_missing(df):
for col in df.columns.tolist() :
if df[col].isnull().sum() :
print('{}: {}'.format(col, df[col].isnull().sum()))
X_train = train_df.iloc[:, 1:].values
y_train = train_df.iloc[:, 0].values
X_test = test_df.values
print(f"X_train shape: {X_train.shape}")
 print(f"y_train shape: {y_train.shape}")
 print(f"X_test shape: {X_test.shape}" )<train_model>
df.loc[df['Alley'].isnull() ,'Alley'] = 'None'
df.loc[df['Fence'].isnull() ,'Fence'] = 'None'
df.loc[df['MiscFeature'].isnull() ,'MiscFeature'] = 'None'
X_train_combined = np.r_[X_train, X_train_add]
y_train_combined = np.r_[y_train, y_train_add]
del X_train
del X_train_add
del y_train
del y_train_add
print(f"X_train_combined shape: {X_train_combined.shape}")
 print(f"y_train_combined shape: {y_train_combined.shape}" )<train_model>
df.loc[np.logical_and(df['PoolArea'] != 0, df['PoolQC'].isnull()),
['PoolArea', 'PoolQC', 'OverallQual']]
class ImageReshaper(BaseEstimator, TransformerMixin):

 def __init__(self, shape):
self.shape = shape
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return X.reshape(self.shape )<choose_model_class>
df.loc[2420,['PoolQC']] = 2
df.loc[2599,['PoolQC']] = 2
df.loc[2503,['PoolQC']] = 3
df['PoolQC'].fillna('None', inplace=True )
def build_lenet5_model() :

 model = Sequential()
model.add(Conv2D(6, kernel_size=5, activation='relu',
input_shape=(28,28,1)))
 model.add(MaxPooling2D())
 model.add(Conv2D(16, kernel_size=5, activation='relu'))
model.add(MaxPooling2D())
 model.add(Flatten())
 model.add(Dense(400, activation='relu'))
model.add(Dense(120, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
 return model<choose_model_class>
df['MasVnrType'].value_counts()
def build_custom_lenet5_model() :

 model = Sequential()
model.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
 return model<train_model>
df.loc[df['MasVnrArea'].isnull() | df['MasVnrType'].isnull() ][['MasVnrArea','MasVnrType']]
stratified_fold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 for fold, indices in enumerate(stratified_fold.split(X_train_combined, y_train_combined)) :
X_train_, y_train_ = X_train_combined[indices[0]], y_train_combined[indices[0]]
X_test_, y_test_ = X_train_combined[indices[1]], y_train_combined[indices[1]]
model_pipeline = Pipeline([ 
('min_max_scaler', MinMaxScaler()), 
('image_reshaper', ImageReshaper(shape=(-1, 28, 28, 1))), 
('model', KerasClassifier(build_lenet5_model, epochs=5, batch_size=32))
])
 model_pipeline.fit(X_train_, y_train_)
 predictions = model_pipeline.predict(X_test_)
 print(f"Classification report for Fold {fold + 1}:")
 print(classification_report(y_test_, predictions, digits=3), end="
")
 print(f"Confusion Matrix for Fold {fold + 1}:")
 print(confusion_matrix(y_test_, predictions), end="
")
 del X_train_
del X_test_
del y_train_
del y_test_<train_model>
df.loc[df['MasVnrArea'].isnull() ,['MasVnrArea','MasVnrType']] = [0, 'None']
df.loc[df['MasVnrType'].isnull() ,['MasVnrType']] = 'None'
lenet5_model = Pipeline([ 
('min_max_scaler', MinMaxScaler()), 
('image_reshaper', ImageReshaper(shape=(-1, 28, 28, 1))), 
('model', KerasClassifier(build_lenet5_model, epochs=5, batch_size=32))
])
 custom_lenet5_model = Pipeline([ 
('min_max_scaler', MinMaxScaler()), 
('image_reshaper', ImageReshaper(shape=(-1, 28, 28, 1))), 
('model', KerasClassifier(build_custom_lenet5_model, epochs=20, batch_size=32))
])
 lenet5_model.fit(X_train_combined, y_train_combined)
 lenet5_model_predictions = lenet5_model.predict_proba(X_test)
 custom_lenet5_model.fit(X_train_combined, y_train_combined)
 custom_lenet5_model_predictions = custom_lenet5_model.predict_proba(X_test )<prepare_output>
print(len(df.loc[df['BsmtQual'].isnull() & df['BsmtCond'].isnull() & df['BsmtExposure'].isnull() &
df['BsmtFinType1'].isnull() & df['BsmtFinType2'].isnull() ]))
predictions = lenet5_model_predictions + custom_lenet5_model_predictions
predictions = np.argmax(predictions, axis=1 )<save_to_csv>
df.loc[(df['BsmtFinType1'].notnull())&(df['BsmtQual'].isnull() | df['BsmtCond'].isnull() | df['BsmtExposure'].isnull() | df['BsmtFinType2'].isnull())][['BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinSF1','BsmtFinType2',\
'BsmtFinSF2','BsmtUnfSF','TotalBsmtSF','BsmtFullBath','BsmtHalfBath']]
submission_df["Label"] = predictions
submission_df.to_csv('submissions.csv', index=False)
 FileLink('submissions.csv' )<load_from_csv>
df.BsmtQual.loc[[2217, 2218]] = df.BsmtQual.mode() [0]
df.BsmtCond.loc[[2040, 2185, 2524]]= df.BsmtCond.mode() [0]
df.BsmtFinType2.loc[[332]] = df.BsmtFinType2.mode() [0]
df.BsmtExposure.loc[[948, 1487, 2348]] = df.BsmtExposure.mode() [0]
df.BsmtQual.fillna('None', inplace=True)
 df.BsmtCond.fillna('None', inplace=True)
 df.BsmtExposure.fillna('None', inplace=True)
 df.BsmtFinType1.fillna('None', inplace=True)
 df.BsmtFinType2.fillna('None', inplace=True)
 df.BsmtFinSF1.fillna(0, inplace=True)
 df.BsmtFinSF2.fillna(0, inplace=True)
 df.BsmtUnfSF.fillna(0, inplace=True)
 df.TotalBsmtSF.fillna(0, inplace=True)
 df.BsmtFullBath.fillna(0, inplace=True)
 df.BsmtHalfBath.fillna(0, inplace=True )
%matplotlib inline
data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 data.describe()<define_variables>
len(df.loc[df.GarageType.isnull() & df.GarageFinish.isnull() & df.GarageQual.isnull() & df.GarageCond.isnull() ,
['GarageType','GarageYrBlt','GarageFinish','GarageCars','GarageArea','GarageQual','GarageCond']] )
columnFilter = ["Survived", "Pclass", "Sex", "Age", "SibSp", "Parch"]
filteredData = data[columnFilter]
filteredTestData = test_data[columnFilter[1:]]
filteredData.head()<categorify>
df.loc[df.GarageType.notnull() &(df.GarageFinish.isnull() | df.GarageQual.isnull() | df.GarageCond.isnull()),
['GarageType','GarageFinish','GarageCars','GarageArea','GarageQual','GarageCond']]
def oneHotEncode(dataToEncode, column):
onehot = pd.get_dummies(dataToEncode[column])
 dataToEncode = dataToEncode.join(onehot)
 dataToEncode = dataToEncode.drop(columns=column)
 return dataToEncode
filteredData = oneHotEncode(filteredData, "Pclass")
 filteredData.head()<feature_engineering>
df.loc[[2127, 2576], ['GarageCond', 'GarageQual', 'GarageFinish', 'GarageCars', 'GarageArea']] = [df['GarageCond'].mode() [0], df['GarageQual'].mode() [0], df['GarageFinish'].mode() [0], df['GarageCars'].mode() [0], df['GarageArea'].mean() ]
def sex_to_numerical(d):
sex = d["Sex"]
if sex == "male":
return 1
return 0
filteredData['Sex'] = filteredData.apply(sex_to_numerical, axis=1)
 filteredData.head()<normalization>
df.GarageYrBlt.replace(2207, 2007, inplace=True)
 emp_garageYr = df.loc[df.GarageYrBlt.isnull() ].index
df.GarageYrBlt.iloc[emp_garageYr] = df.YearBuilt.iloc[emp_garageYr]
def normalize_age(dataToNormalize):
scaler = MinMaxScaler()
dataToNormalize["Age"] = scaler.fit_transform(dataToNormalize["Age"].values.reshape(-1,1))
return dataToNormalize
filteredData.Age.fillna(( filteredData['Age'].mean()), inplace=True)
 filteredData = normalize_age(filteredData)
 filteredData.head()<split>
df.loc[df['GarageType'].isnull() , ['GarageYrBlt']] = 0
df['GarageType'].fillna('None', inplace=True)
 df['GarageFinish'].fillna('None',inplace=True)
 df['GarageCars'].fillna(0,inplace=True)
 df['GarageArea'].fillna(0,inplace=True)
 df['GarageQual'].fillna('None',inplace=True)
 df['GarageCond'].fillna('None',inplace=True )
X = filteredData[filteredData.columns[1:]]
y = filteredData[filteredData.columns[0]]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
 X_train = X_train.to_numpy()
X_test = X_test.to_numpy()
y_train = y_train.to_numpy()
y_test = y_test.to_numpy()<train_model>
df['FireplaceQu'].fillna('None', inplace=True )
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy'])
 history = model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test))<categorify>
for feature in ['Exterior1st', 'Exterior2nd', 'Electrical', 'KitchenQual', 'MSZoning', 'Functional', 'SaleType', 'Utilities']:
df[feature] = df.groupby(['Neighborhood', 'MSSubClass'])[feature].apply(lambda x: x.fillna(x.mode() [0]))
df['LotFrontage'] = df.groupby(['Neighborhood'])['LotFrontage'].apply(lambda x : x.fillna(x.median()))
filteredTestData = oneHotEncode(filteredTestData, "Pclass")
 filteredTestData['Sex'] = filteredTestData.apply(sex_to_numerical, axis=1)
 filteredTestData.Age.fillna(( filteredTestData['Age'].mean()), inplace=True)
 filteredTestData = normalize_age(filteredTestData )<train_model>
print('Training Set SalePrice Skew: {}'.format(train['SalePrice'].skew()))
print('Training Set SalePrice Kurtosis: {}'.format(train['SalePrice'].kurt()))
print('Training Set SalePrice Mean: {}'.format(train['SalePrice'].mean()))
print('Training Set SalePrice Median: {}'.format(train['SalePrice'].median()))
print('Training Set SalePrice Max: {}'.format(train['SalePrice'].max()))
model.fit(X, y, epochs=100 )<predict_on_test>
train_corr[train_corr['Feature_1'] == 'SalePrice']
predictions = model.predict(filteredTestData)
 predictions = np.where(predictions > 0.5, 1, 0)
 predictions<save_to_csv>
df['TotalSF'] = df['TotalBsmtSF'] + df['1stFlrSF'] + df['2ndFlrSF']
df['TotalBath'] = df['FullBath']+df['BsmtFullBath']+(df['BsmtHalfBath']+df['HalfBath'])*0.5
df['TotalPorchSF'] = df['OpenPorchSF'] + df['3SsnPorch'] + df['EnclosedPorch'] + df['ScreenPorch']
predictions = pd.DataFrame(predictions ).rename(columns={0: "Survived"})
 predictions.index.name ="PassengerId"
predictions.index += 892
predictions.to_csv("predictions.csv")
 predictions<set_options>
df['YearBuiltRemod'] = df['YearBuilt'] + df['YearRemodAdd']
print(f'Correaltion with Target variable: {df.SalePrice.corr(df.YearBuiltRemod ).round(3)}' )
%matplotlib inline<define_variables>
df['HasPool'] = df['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 df['HasFireplace'] = df['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 df['isNewer'] = 0
df.loc[df['YrSold'] == df['YearBuilt'], 'isNewer'] = 1
IMG_ROWS = 28
IMG_COLS = 28
NUM_CLASSES = 10
TEST_SIZE = 0.1
RANDOM_STATE = 2018
NO_EPOCHS = 150
PATIENCE = 20
VERBOSE = 1
BATCH_SIZE = 128
IS_LOCAL = False
if(IS_LOCAL):
PATH=".. /input/digit-recognizer/"
else:
PATH=".. /input/"
print(os.listdir(PATH))<load_from_csv>
df_ = df.copy()
for f in ['MSSubClass', 'MoSold', 'YrSold', 'OverallQual', 'OverallCond']:
df_[f] = df_[f].astype('category')
 num_features = df_.drop(columns=['SalePrice'] ).select_dtypes(include=np.number ).columns.tolist()
cat_features = df_.select_dtypes(exclude=np.number ).columns.tolist()
train_file = PATH+"train.csv"
test_file = PATH+"test.csv"
train_df = pd.read_csv(train_file)
 test_df = pd.read_csv(test_file )<train_model>
print(len(num_features), len(cat_features))
df_num = df_[num_features]
df_cat = df_[cat_features]
print("MNIST train - rows:",train_df.shape[0]," columns:", train_df.shape[1])
 print("MNIST test - rows:",test_df.shape[0]," columns:", test_df.shape[1] )<groupby>
skew_features = [
'LotFrontage', 'MasVnrArea', 'BsmtFinSF1',
'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', '2ndFlrSF',
'LowQualFinSF', 'GrLivArea', 'WoodDeckSF', 'OpenPorchSF',
'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'MiscVal',
'TotalSF', 'TotalPorchSF',
]
for feature in skew_features:
df[feature] = boxcox1p(df[feature], boxcox_normmax(df[feature] + 1))
def get_classes_distribution(data):
label_counts = data["label"].value_counts()
total_samples = len(data)
 for i in range(len(label_counts)) :
label = label_counts.index[i]
count = label_counts.values[i]
percent =(count / total_samples)* 100
print("{}: {} or {}%".format(label, count, percent))
get_classes_distribution(train_df )<define_variables>
exterQual = {'Fa': 1, 'TA': 2, 'Gd': 3,'Ex': 4}
exterCond = {'Po': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}
bsmtQual = {'None': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}
bsmtCond = {'None': 0, 'Po': 1, 'Fa': 2,'TA': 3, 'Gd': 4}
bsmtExposure = {'None': 0, 'No': 1, 'Mn': 2,'Av': 3, 'Gd': 4}
bsmtFinType1 = {'None': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}
bsmtFinType2 = {'None': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}
heatingQC = {'Po': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}
kitchenQual = {'Fa': 0, 'TA': 1, 'Gd': 2, 'Ex': 3}
firePlaceQu = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
garageQual = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
garageCond = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
functional = {'Typ': 0, 'Min1': 1, 'Min2': 1, 'Mod': 2, 'Maj1': 3, 'Maj2': 3, 'Sev': 4}
landSlope = { 'Sev': 1, 'Mod': 2, 'Gtl': 3 }
df['ExterQual'] = df['ExterQual'].map(exterQual)
 df['ExterCond'] = df['ExterCond'].map(exterCond)
 df['BsmtQual'] = df['BsmtQual'].map(bsmtQual)
 df['BsmtCond'] = df['BsmtCond'].map(bsmtCond)
 df['BsmtExposure'] = df['BsmtExposure'].map(bsmtExposure)
 df['BsmtFinType1'] = df['BsmtFinType1'].map(bsmtFinType1)
 df['BsmtFinType2'] = df['BsmtFinType2'].map(bsmtFinType2)
 df['HeatingQC'] = df['HeatingQC'].map(heatingQC)
 df['KitchenQual'] = df['KitchenQual'].map(kitchenQual)
 df['FireplaceQu'] = df['FireplaceQu'].map(firePlaceQu)
 df['GarageQual'] = df['GarageQual'].map(garageQual)
 df['GarageCond'] = df['GarageCond'].map(garageCond)
 df['Functional'] = df['Functional'].map(functional)
 df['LandSlope'] = df['LandSlope'].map(landSlope )
def sample_images_data(data, hasLabel=True):
sample_images = []
sample_labels = []
if(hasLabel):
for k in range(0,10):
samples = data[data["label"] == k].head(4)
 for j, s in enumerate(samples.values):
img = np.array(samples.iloc[j, 1:] ).reshape(IMG_ROWS,IMG_COLS)
 sample_images.append(img)
 sample_labels.append(samples.iloc[j, 0])
 else:
samples = data.iloc[random.sample(range(1, 10000), 40),]
for j, s in enumerate(samples.values):
img = np.array(samples.iloc[j, 0:] ).reshape(IMG_ROWS,IMG_COLS)
 sample_images.append(img)
 sample_labels.append(-1)
 print("Total number of sample images to plot: ", len(sample_images))
return sample_images, sample_labels
train_sample_images, train_sample_labels = sample_images_data(train_df )<prepare_x_and_y>
df.drop(columns=['Street', 'Utilities', 'Fireplaces',
'PoolArea', 'PoolQC', 'GarageYrBlt',
'GarageArea', 'TotalBsmtSF', '1stFlrSF',
'FullBath', 'YearBuilt', 'YearRemodAdd',
], inplace=True )
def data_preprocessing(raw, hasLabel=True):
start_pixel = 0
if(hasLabel):
start_pixel = 1
if(hasLabel):
out_y = keras.utils.to_categorical(raw.label, NUM_CLASSES)
 else:
out_y = None
num_images = raw.shape[0]
x_as_array = raw.values[:,start_pixel:]
x_shaped_array = x_as_array.reshape(num_images, IMG_ROWS, IMG_COLS, 1)
 out_x = x_shaped_array / 255
return out_x, out_y<prepare_x_and_y>
df = pd.get_dummies(df,columns=df.select_dtypes(exclude=np.number ).columns.tolist())
 df.shape
X, y = data_preprocessing(train_df)
 X_test, y_test = data_preprocessing(test_df,hasLabel=False )<split>
print(train[np.logical_and(train['OverallQual'] < 5, train['SalePrice'] > 200000)].index)
 print(train[np.logical_and(train['GrLivArea'] > 4000, train['SalePrice'] < 300000)].index)
 print(df[np.logical_and(df['OverallQual']==4, df['SalePrice'] > 200000)].index)
 outliers=[457, 523, 1298]
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=TEST_SIZE, random_state=RANDOM_STATE )<train_model>
sparse=[]
for feature in X_test.columns:
counts = X_test[feature].value_counts()
zeros = counts.iloc[0]
if zeros/len(X_test)> 99.94:
sparse.append(feature)
 print(feature)
 X_test.drop(columns=sparse, inplace=True)
 X_train.drop(columns=sparse, inplace=True )
print("MNIST train - rows:",X_train.shape[0]," columns:", X_train.shape[1:4])
 print("MNIST valid - rows:",X_val.shape[0]," columns:", X_val.shape[1:4])
 print("MNIST test - rows:",X_test.shape[0]," columns:", X_test.shape[1:4] )<choose_model_class>
X_train = X_train.drop(X_train.index[outliers])
 Y_train = Y_train.drop(Y_train.index[outliers])
 y_train = Y_train.apply(np.log1p )
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same",
kernel_initializer='he_normal',input_shape=(IMG_ROWS, IMG_COLS, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size=(3, 3), strides=2,padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=(3, 3), strides=2,padding='same', activation='relu'))
model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(NUM_CLASSES, activation='softmax'))<choose_model_class>
scaler = RobustScaler()
x_train = pd.DataFrame(scaler.fit_transform(X_train),
index=X_train.index,
columns=X_train.columns)
 X_test = pd.DataFrame(scaler.transform(X_test),
index=X_test.index,
columns=X_test.columns )
model.compile(loss = "categorical_crossentropy", optimizer="adam", metrics=["accuracy"] )<save_to_csv>
kfolds = KFold(n_splits=8, shuffle=True, random_state=42)
 def cv_rmse(model, X=x_train, y=y_train):
rmse = np.sqrt(-cross_val_score(model, X, y,scoring="neg_mean_squared_error",cv=kfolds))
return(rmse)
 def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def fit_model(model, X=x_train, y=y_train):
return model.fit(X, y )
plot_model(model, to_file='model.png')
 SVG(model_to_dot(model ).create(prog='dot', format='svg'))<define_variables>
def grid_search(model, param_grid, cv=5,
scoring='neg_mean_squared_error'):
grid_model = GridSearchCV(model,
param_grid,
cv=cv,
scoring=scoring,
return_train_score=True 
)
 fit_model(grid_model)
 cvres = grid_model.cv_results_
for mean_score, std_score, params in zip(cvres["mean_test_score"], cvres["std_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), std_score, params)
 return grid_model
NO_EPOCHS = 10<train_model>
param_grid=[{'alpha': [0.0005, 0.0007]}]
lasso = Lasso(max_iter=1e7, random_state=SEED)
 grid_lasso = grid_search(lasso, param_grid, cv=kfolds )
earlystopper = EarlyStopping(monitor='loss', patience=PATIENCE, verbose=VERBOSE)
 checkpointer = ModelCheckpoint('best_model.h5',
monitor='val_acc',
verbose=VERBOSE,
save_best_only=True,
save_weights_only=True)
 history = model.fit(X_train, y_train,
batch_size=BATCH_SIZE,
epochs=NO_EPOCHS,
verbose=1,
validation_data=(X_val, y_val),
callbacks=[earlystopper, checkpointer] )<train_model>
param_grid=[
{
'alpha': [ 0.008],
'l1_ratio': [0.0056, 0.009, 0.01, 0.05],
}
]
elastic = ElasticNet(max_iter=1e7, random_state=SEED)
 grid_elastic = grid_search(elastic, param_grid, cv=kfolds )
print("run model - predict validation set")
 score = model.evaluate(X_val, y_val, verbose=0)
 print(f'Last validation loss: {score[0]}, accuracy: {score[1]}')
 model_optimal = model
model_optimal.load_weights('best_model.h5')
 score = model_optimal.evaluate(X_val, y_val, verbose=0)
 print(f'Best validation loss: {score[0]}, accuracy: {score[1]}' )<predict_on_test>
param_grid=[
{
'C': np.logspace(1.5,2,4),
'epsilon': [0.05, 0.06],
'gamma':[0.0001]
}
]
svr= SVR()
grid_svr = grid_search(svr, param_grid, cv=kfolds )
def predict_show_classes(model, X_val, y_val):
predicted_classes = model.predict_classes(X_val)
 y_true = np.argmax(y_val,axis=1)
 correct = np.nonzero(predicted_classes==y_true)[0]
incorrect = np.nonzero(predicted_classes!=y_true)[0]
print("Correct predicted classes:",correct.shape[0])
 print("Incorrect predicted classes:",incorrect.shape[0])
 target_names = ["Class {}:".format(i)for i in range(NUM_CLASSES)]
print(classification_report(y_true, predicted_classes, target_names=target_names))
return correct, incorrect<predict_on_test>
param_grid=[
{
'learning_rate': [0.01],
'max_depth':[ 4 ],
}
]
gbr = GradientBoostingRegressor(
 n_estimators=3000, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state=SEED)
 grid_gbr = grid_search(gbr, param_grid, cv=kfolds )
correct, incorrect = predict_show_classes(model, X_val, y_val )<predict_on_test>
param_grid=[
{
'learning_rate': [0.005, 0.009, 0.01],
'n_estimators': [5000]
}
]
lightgbm = LGBMRegressor(objective='regression',
num_leaves=4,max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
random_state=SEED,verbose=-1 
)
 grid_lgb = grid_search(lightgbm, param_grid, cv=kfolds )
correct, incorrect = predict_show_classes(model_optimal, X_val, y_val )<predict_on_test>
param_grid=[
{
'learning_rate': [0.1],
'n_estimators': [3460],
'max_depth':[ 3 ],
'gamma':[0.001],
'subsample':[0.7]
}
]
xgb = XGBRegressor(n_jobs=3)
 grid_xgb = grid_search(xgb, param_grid, cv=kfolds )
y_cat = model.predict(X_test, batch_size=64 )<prepare_output>
stacking = StackingRegressor(estimators=[ 
('ridge', grid_ridge.best_estimator_), 
('lasso', grid_lasso.best_estimator_), 
('elastic',grid_elastic.best_estimator_), 
('xgb', grid_xgb.best_estimator_), 
('lgb', grid_lgb.best_estimator_)
 ],
final_estimator=grid_xgb.best_estimator_,
passthrough=True)
 stacking_model=fit_model(stacking)
 loss=rmsle(y_train, stacking_model.predict(x_train))
print("Stacking:
Loss: %.4f"%(loss))
y_pred = np.argmax(y_cat,axis=1 )<save_to_csv>
def blend(x):
return(
 0.1*grid_elastic.best_estimator_.predict(x)+
0.05*grid_lasso.best_estimator_.predict(x)+
0.1*grid_ridge.best_estimator_.predict(x)+
0.1*grid_svr.best_estimator_.predict(x)+
0.15*grid_gbr.best_estimator_.predict(x)+
0.1*grid_lgb.best_estimator_.predict(x)+
0.1*grid_xgb.best_estimator_.predict(x)+
0.3*stacking.predict(x)
)
 y_pred = blend(x_train)
 scores=rmsle(y_train, y_pred)
 print("Loss:
%.4f"%(scores))
Y_test = blend(X_test )
<save_to_csv><EOS> 
Y_test = np.expm1(Y_test)
 def save_csv(y):
submission = pd.DataFrame()
submission['Id'] = test_ids
submission['SalePrice'] = y
submission.to_csv('submission.csv',index=False)
 save_csv(Y_test )