Kaggle competition: Human Protein Atlas Image Classification
<SOS> metric: MacroFScore Kaggle data source: human-protein-atlas-image-classification<categorify>
now = datetime.datetime.now()
print(now )
def mapping(titanic_data):
titanic_data['Sex'] = titanic_data['Sex'].map({"female": 1, "male": 0} ).astype(int)
 return titanic_data
titanic_data = mapping(titanic_data)
 test_data = mapping(test_data)
 def encode_features(titanic_data, test_data):
features = ['Cabin_letter','Embarked']
df_combined = pd.concat([titanic_data, test_data])
 for feature in features:
le = preprocessing.LabelEncoder()
le = le.fit(df_combined[feature])
 titanic_data[feature] = le.transform(titanic_data[feature])
 test_data[feature] = le.transform(test_data[feature])
 return titanic_data, test_data
titanic_data, test_data = encode_features(titanic_data, test_data )<prepare_x_and_y>
%matplotlib inline
titanic_data.head()
test_data.head()
features = ['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', 'Title', 'Cabin_letter', 'IsAlone']
X = titanic_data [features]
y = titanic_data['Survived']<train_model>
try:
shutil.copytree(".. /input/keras-pretrained-models", "/tmp/.keras/models")
 except FileExistsError:
pass
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.50, random_state=1)
 n_est = 1000
learn = 0.10
max_dp = 3
xgb_model = XGBClassifier(n_estimators=n_est, learning_rate=learn, max_depth=max_dp)
 xgb_model.fit(train_X, train_y)
 xgb_predictions = xgb_model.predict(test_X)
 print("Accuracy: {0}".format(accuracy_score(test_y, xgb_predictions)) )<compute_train_metric>
Conv1D, Conv2D, Conv3D, \
Conv2DTranspose, \
AveragePooling1D, AveragePooling2D, \
MaxPooling1D, MaxPooling2D, MaxPooling3D, \
GlobalAveragePooling1D, \
GlobalMaxPooling1D, GlobalMaxPooling2D, GlobalMaxPooling3D, \
LocallyConnected1D, LocallyConnected2D, \
concatenate, Flatten, Average, Activation, \
RepeatVector, Permute, Reshape, Dot, \
multiply, dot, add, \
PReLU, \
Bidirectional, TimeDistributed, \
SpatialDropout1D, \
BatchNormalization
xgb_final = make_pipeline(XGBClassifier(n_estimators=n_est,
learning_rate=learn,
xgbclassifier__early_stopping_rounds=5,
xgbclassifier__eval_set=[(X, y)]))
scores = cross_val_score(xgb_final, X, y, scoring='accuracy', cv=3)
 print('XGB Pipeline Cross-Validation Accuracy: %2f' %scores.mean())
 print(scores)
 xgb_final.fit(X, y);<save_to_csv>
from PIL import Image
from zipfile import ZipFile
import h5py
import cv2
from tqdm import tqdm
ids = test_data['PassengerId']
final_data = test_data [features]
predictions = xgb_final.predict(final_data);
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False)
 output.head()<set_options>
src_dir = '.. /input/human-protein-atlas-image-classification'
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
train_labels = pd.read_csv(os.path.join(src_dir, "train.csv"))
print(train_labels.shape)
 train_labels.head(10 )
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 combine = [df_train, df_test]
df_train.name = 'Train'
df_test.name = 'Test'<prepare_x_and_y>
test_labels = pd.read_csv(os.path.join(src_dir, "sample_submission.csv"))
print(test_labels.shape)
 test_labels.head()
kfolds = KFold(n_splits=10, shuffle=True, random_state=14)
 target = np.log1p(df_train['SalePrice'])
 old_target = df_train['SalePrice']<predict_on_test>
SH_ALL =(512, 512)
 SH =(256, 256)
 ID_LIST_TRAIN = train_labels.Id.tolist()
def get_dollars(estimator, kfolds, data, target, old_target):
scores = []
train = data.copy()
for i,(train_index, test_index)in enumerate(kfolds.split(target)) :
training = train.iloc[train_index,:]
valid = train.iloc[test_index,:]
tr_label = target.iloc[train_index]
val_label = target.iloc[test_index]
estimator.fit(training, tr_label)
 pred = estimator.predict(valid)
 or_result = old_target.iloc[test_index]
score = mean_absolute_error(y_pred=np.expm1(pred), y_true=or_result)
 scores.append(score)
 return round(np.mean(scores),3)
 def get_coef(clsf, ftrs):
imp = clsf.steps[1][1].coef_.tolist()
feats = ftrs
result = pd.DataFrame({'feat':feats,'score':imp})
 result = result.sort_values(by=['score'],ascending=False)
 return result<compute_train_metric>
scores = []
or_scores = []
for i,(train_index, test_index)in enumerate(kfolds.split(target)) :
df_train['prediction'] = np.nan
print("Fold {} in progress".format(i))
base = target.iloc[train_index].median()
result = target.iloc[test_index]
or_result = old_target.iloc[test_index]
df_train['prediction'].iloc[test_index] = base
prediction = [p for p in df_train['prediction'].dropna() ]
print("Predicting with median {}".format(round(base,3)))
 score = mean_squared_error(y_pred= prediction, y_true=result)
 or_score = mean_absolute_error(y_pred=np.expm1(prediction), y_true=or_result)
 print("Scoring {}".format(score))
print("MAE {}$".format(or_score))
scores.append(score)
 or_scores.append(or_score)
 df_train.drop('prediction', axis=1, inplace=True)
 print("_"*40)
 print("Baseline: {} +- {}".format(round(np.mean(scores),3), round(np.std(scores),3)))
 print('MAE: {} +- {}'.format(round(np.mean(or_scores),3), round(np.std(or_scores),3)) )<init_hyperparams>
y_cat_train_dic = {}
for icat in range(28):
target = str(icat)
 y_cat_train_5 = np.array([int(target in ee.split())for ee in train_labels.Target.tolist() ])
 y_cat_train_dic[icat] = y_cat_train_5
modelname = ['Baseline']
lassoscore = [0.16]
ridgescore = [0.16]
kaggle_lasso = [0.41899]
kaggle_ridge = [0.41899]
timeelapsed = [600]
maecv_lasso = [55646.558]
maecv_ridge = [55646.558]<count_missing_values>
up_sample = {}
for k in y_cat_train_dic:
v = y_cat_train_dic[k].sum()
up_sample[k] = np.ceil(( train_labels.shape[0]/28)/ v)
 up_sample
for df in combine:
if df.name == 'Train':
mis_train = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_train.append(col)
 print("_"*40)
 print("_"*40)
 if df.name == 'Test':
mis_test = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_test.append(col)
 print("
")
 print(mis_train)
 print("_"*40)
 print(mis_test )<categorify>
up_sample2 = list(zip(*sorted(list(up_sample.items()), key=lambda x: x[0])))[1]
up_sample2 = np.array(up_sample2)
 up_sample2
butch_train = df_train[[col for col in df_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = df_test[[col for col in df_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 butch_train = butch_train[[col for col in butch_test.columns]] 
(butch_train.columns != butch_test.columns ).sum()<normalization>
class Seq(object):
sections = None
index = None
SPLIT_LIST = [
np.array(range(256)) ,
np.array(range(256)) + 256
]
def __init__(self, df, extend=False, aug=False, test=False, batch_size=32):
self.shaffle = None
self.extend = extend
self.aug = aug
self.test = test
self.batch_size = batch_size
self.df = df
self.ids = self.df.Id.tolist()
self.reversed = sorted(range(SH_ALL[0]), reverse=True)
 self.initialize_it()
self.len = 1
for _ in self.it:
self.len += 1
self.initialize_it()
def initialize_it(self):
if self.shaffle:

 raise NotImplementedError
self.it = iter(range(0, len(self.ids), self.batch_size))
self.idx_next = self.it.__next__()
def __len__(self):
return self.len
def __iter__(self):
return self
def __next__(self):
idx = self.idx_next
self.ids_part = self.ids[idx:(( idx+self.batch_size)if idx+self.batch_size<len(self.ids)else len(self.ids)) ]
res = self.getpart(self.ids_part)
 try:
self.idx_next = self.it.__next__()
except StopIteration:
self.initialize_it()
return res
def __getitem__(self, id0):
arr, tgts = self.get_data(id0)
 cat = self.convert_tgts(tgts)
 return arr, cat
k_list = list(range(4))
def random_transform(self, arr):
k = random.choice(self.k_list)
 arr0 = np.rot90(arr, k=k)
 if random.randint(0,1):
arr0 = arr0[self.reversed,:,:]
if random.randint(0,1):
arr0 = arr0[:,self.reversed,:]
return arr0
def convert_tgts(self, tgts):
try:
cats = to_categorical(tgts, num_classes=28)
 cat = cats.sum(axis=0)
 except TypeError:
cat = np.zeros(( 28,))
return cat
def get_data(self, id0):
arr = get_arr0(id0, test=self.test)
 try:
y0 =(self.df.Target[self.df.Id == id0] ).tolist() [0]
y1 = y0.split()
y = [int(ee)for ee in y1]
except AttributeError:
y = None
return arr, y
def getpart(self, ids):
xs = []
ys = []
for id0 in ids:
self.extend_data(id0, xs, ys)
 x = np.stack(xs)
 y = np.stack(ys)
 x_dummy = np.zeros(( len(x), 1))
x_ret = {
'input': x,
'input_cls': y,
}
y_ret = {
'path_fit_cls_img': x_dummy,
'path_cls_img_cls': y,
'path_fit_imgA': x_dummy,
'path_fit_img_cls_img': x_dummy,
'path_cls_cls': y,
'path_img_cls': y,
'path_img_img_cls': y,
'path_fit_cls_img_imgE': x_dummy,
}
return(x_ret, y_ret)
 def split(self, arr):
res = []
for row_idx in self.SPLIT_LIST:
for col_idx in self.SPLIT_LIST:
res.append(arr[:, col_idx][row_idx].flatten())
 return res
def extend_data(self, id0, xs, ys):
arr0, cat = self[id0]
if self.extend:
mm = up_sample2[cat==1].max()
mm = int(mm)
 for ii in range(mm):
if self.aug:
img = self.random_transform(arr0)
 else:
img = arr0
xs.append(img.flatten())
 ys.append(cat)
 else:
if self.aug:
img0 = self.random_transform(arr0)
 else:
img0 = arr0
res = self.split(img0)
 res = np.concatenate(res)
 xs.append(res)
 ys.append(cat )
scl =('scl', RobustScaler() )<choose_model_class>
arr, y = seq.get_data('ad5a4858-bb9d-11e8-b2b9-ac1f6b6435d0')
 print(arr.shape)
 show_arr(arr )
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print("_"*40)
 print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<choose_model_class>
from keras import applications
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol<choose_model_class>
def make_trainable_false(model_resnet, trainable=False):
layers = model_resnet.layers
for ilayer in layers:
ilayer.trainable = trainable
return
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.0075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<compute_train_metric>
def make_model_cnvt(img_dim, img_shape):

 inp = Input(shape=(img_dim,))
oup = Reshape(img_shape )(inp)
 oup = Conv2D(3,
kernel_size=1,
strides=1,
padding='same',
activation='tanh',
kernel_regularizer=regularizers.l2(0))(oup)
 model_cnvt = Model(inp, oup, name='model_cnvt')
 return model_cnvt
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol<compute_test_metric>
def make_model_classifier(input_dim=1536):
inp_cls = Input(( input_dim,))
oup_cls = Dense(28 )(inp_cls)
 oup_cls = Activation('sigmoid' )(oup_cls)
 model_classifier = Model(inp_cls, oup_cls, name='classifier')
 return model_classifier
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )<prepare_x_and_y>
def make_model(img_dim, model_cnvt, model_resnet, model_classifier):
inp0 = Input(shape=(int(img_dim/4),), name='input0')
 oup0 = model_cnvt(inp0)
 oup0 = model_resnet(oup0)
 oup0 = model_classifier(oup0)
 oup0 = Activation('linear', name='path_cls_cls' )(oup0)
 model0 = Model(inp0, oup0, name='model0')

def fn(x, idx):
x1 = K.permute_dimensions(x,(1,0,2,))
x2 = K.gather(x1, idx)
 return x2
inp = Input(shape=(img_dim,), name='input')
 oup = Reshape(( 4,int(img_dim/4)) )(inp)
 img0 = Lambda(lambda x: fn(x, 0))(oup)
 img1 = Lambda(lambda x: fn(x, 1))(oup)
 img2 = Lambda(lambda x: fn(x, 2))(oup)
 img3 = Lambda(lambda x: fn(x, 3))(oup)
 oup = Lambda(lambda x: K.stack(x, 1))([model0(img0), model0(img1), model0(img2), model0(img3)])
 oup = GlobalMaxPooling1D()(oup)
 oup = Activation('linear', name='path_cls_cls' )(oup)
 model = Model(inp, oup, name='model')
 return {
'model_classifier': model_classifier,
'model_resnet': model_resnet,
'model_cnvt': model_cnvt,
'model': model,
'model0': model0
}
target = np.log1p(df_train[df_train.GrLivArea < 4500]['SalePrice'])
 old_target = df_train[df_train.GrLivArea < 4500]['SalePrice']<data_type_conversions>
model_cnvt.layers[2].get_weights()
for df in combine:
df.loc[df.LotFrontage.isnull() , 'LotFrontage'] = 0
df.loc[df.Alley.isnull() , 'Alley'] = "NoAlley"
df['MSSubClass'] = df['MSSubClass'].astype(str)
 fil =(( df.BsmtQual.isnull())&(df.BsmtCond.isnull())&(df.BsmtExposure.isnull())& 
(df.BsmtFinType1.isnull())&(df.BsmtFinType2.isnull()))
fil1 =(( df.BsmtQual.notnull())|(df.BsmtCond.notnull())|(df.BsmtExposure.notnull())| 
(df.BsmtFinType1.notnull())|(df.BsmtFinType2.notnull()))
df.loc[fil1, 'MisBsm'] = 0
df.loc[fil, 'MisBsm'] = 1
df.loc[fil, 'BsmtQual'] = "NoBsmt"
df.loc[fil, 'BsmtCond'] = "NoBsmt"
df.loc[fil, 'BsmtExposure'] = "NoBsmt"
df.loc[fil, 'BsmtFinType1'] = "NoBsmt"
df.loc[fil, 'BsmtFinType2'] = "NoBsmt"
df.loc[(df.Fireplaces == 0)&(df.FireplaceQu.isnull()), 'FireplaceQu'] = "NoFire"
fil =(( df.GarageYrBlt.isnull())&(df.GarageType.isnull())&(df.GarageFinish.isnull())& 
(df.GarageQual.isnull())&(df.GarageCond.isnull()))
fil1 =(( df.GarageYrBlt.notnull())|(df.GarageType.notnull())|(df.GarageFinish.notnull())| 
(df.GarageQual.notnull())|(df.GarageCond.notnull()))
df.loc[fil1, 'MisGarage'] = 0
df.loc[fil, 'MisGarage'] = 1
df.loc[df.GarageYrBlt > 2200, 'GarageYrBlt'] = 2007
df.loc[fil, 'GarageYrBlt'] = 0
df.loc[fil, 'GarageType'] = "NoGrg"
df.loc[fil, 'GarageFinish'] = "NoGrg"
df.loc[fil, 'GarageQual'] = "NoGrg"
df.loc[fil, 'GarageCond'] = "NoGrg"
df.loc[df.Fence.isnull() , 'Fence'] = "NoFence"
df_test[['BsmtUnfSF',
'TotalBsmtSF',
'BsmtFinSF1',
'BsmtFinSF2']] = df_test[['BsmtUnfSF',
'TotalBsmtSF',
'BsmtFinSF1',
'BsmtFinSF2']].fillna(0 )<count_missing_values>
model_cnvt.load_weights('.. /input/resnet50-4x256-max-1/model_1_cnvt.h5')
 model_cnvt.layers[2].get_weights()
for df in combine:
if df.name == 'Train':
mis_train = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_train.append(col)
 print("_"*40)
 print("_"*40)
 if df.name == 'Test':
mis_test = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_test.append(col)
 print("
")
 print(mis_train)
 print("_"*40)
 print(mis_test )<categorify>
model_resnet = applications.resnet50.ResNet50(
 include_top=False,
weights='imagenet',
input_tensor=None,
input_shape=list(SH)+[3],
pooling='avg',
classes=None )
butch_train = df_train[[col for col in df_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = df_test[[col for col in df_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
df_train = df_train[df_train.GrLivArea < 4500] 
(butch_train.columns != butch_test.columns ).sum()<choose_model_class>
model_resnet.layers[2].get_weights() [0][0,0,0]
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<choose_model_class>
model_resnet.load_weights('.. /input/resnet50-4x256-max-1/model_1_resnet.h5')
 model_resnet.layers[2].get_weights() [0][0,0,0]
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol<choose_model_class>
model_classifier.layers[1].get_weights() [0][0]
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.0075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<compute_train_metric>
model_classifier.load_weights('.. /input/resnet50-4x256-max-1/model_1_classifier.h5')
 model_classifier.layers[1].get_weights() [0][0]
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol<compute_test_metric>
THRESHOLD = 0.5
K_epsilon = K.epsilon()
def f1(y_true, y_pred):
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), THRESHOLD), K.floatx())
 tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K_epsilon)
 r = tp /(tp + fn + K_epsilon)
 f1 = 2*p*r /(p+r+K_epsilon)
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def f1_loss(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K_epsilon)
 r = tp /(tp + fn + K_epsilon)
 f1 = 2*p*r /(p+r+K_epsilon)
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return 1-K.mean(f1 )
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )<drop_column>

 gamma = 2.0
epsilon = K.epsilon()
def focal_loss(y_true, y_pred):
pt = y_pred * y_true +(1-y_pred)*(1-y_true)
 pt = K.clip(pt, epsilon, 1-epsilon)
 CE = -K.log(pt)
 FL = K.pow(1-pt, gamma)* CE
loss = K.sum(FL, axis=1)
 return loss
print(df_train.shape, df_test.shape)
 df_train.drop("PoolQC", axis=1, inplace=True)
 df_train.drop("MiscFeature", axis=1, inplace=True)
 df_test.drop("PoolQC", axis=1, inplace=True)
 df_test.drop("MiscFeature", axis=1, inplace=True)
 print(df_train.shape, df_test.shape )<count_missing_values>
class LearningRateReducer(Callback):
def __init__(self, schedule,
cycle_epoch=2, steps_per_epoch=100,
start_lr=0.001, end_lr=0.0001, verbose=0):
super(LearningRateReducer, self ).__init__()
self.schedule = schedule
self.steps_per_epoch = steps_per_epoch
self.cycle_epoch = cycle_epoch
self.start_lr = start_lr
self.end_lr = end_lr
self.verbose = verbose
def on_train_begin(self, epoch, logs=None):
self.lrf = {'lr': [], 'loss': []}
self.epoch = None
def on_epoch_begin(self, epoch, logs=None):
self.epoch = epoch
def on_batch_begin(self, batch, logs=None):
lr = self.schedule(self.epoch, batch,
cycle_epoch=self.cycle_epoch,
steps_per_epoch=self.steps_per_epoch,
start_lr=self.start_lr, end_lr=self.end_lr)
 K.set_value(self.model.optimizer.lr, lr)
 self.lrf['lr'].append(lr)
 return
def on_batch_end(self, batch, logs={}):
loss = logs.get('loss')
 self.lrf['loss'].append(loss )
print("Size of train set before my butchering: {}".format(df_train.shape))
for f in df_train.columns:
df_train = df_train[pd.notnull(df_train[f])]
cols = df_train.columns
print("Start printing the missing values...")
 mis_train = []
for col in cols:
mis = df_train[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df_train.shape[0] * 100, 3)))
 mis_train.append(col)
 print("...done printing the missing values")
 print(mis_train)
 print("Size of train set after my butchering: {}".format(df_train.shape))<statistical_test>
def lr_schedule(epoch, batch,
cycle_epoch=2, steps_per_epoch=100,
start_lr=0.002, end_lr=0.0002):
by =(np.log(start_lr)- np.log(end_lr)) /(steps_per_epoch*cycle_epoch-1)
 ii = divmod(epoch, cycle_epoch)[1]*steps_per_epoch + batch
lr = np.exp(np.log(start_lr)- ii * by)
 return lr
def find_segment(df, feat):
mis = df[feat].isnull().sum()
cols = df.columns
seg = []
for col in cols:
vc = df[df[feat].isnull() ][col].value_counts(dropna=False ).iloc[0]
if(vc == mis):
seg.append(col)
 return seg
def find_mode(df, feat):
md = df[df[feat].isnull() ][find_segment(df, feat)].dropna(axis=1 ).mode()
md = pd.merge(df, md, how='inner')[feat].mode().iloc[0]
return md
def find_median(df, feat):
md = df[df[feat].isnull() ][find_segment(df, feat)].dropna(axis=1 ).mode()
md = pd.merge(df, md, how='inner')[feat].median()
return md
def similar_mode(df, col, feats):
sm = df[df[col].isnull() ][feats]
md = pd.merge(df, sm, how='inner')[col].mode().iloc[0]
return md
def similar_median(df, col, feats):
sm = df[df[col].isnull() ][feats]
md = pd.merge(df, sm, how='inner')[col].median()
return md<categorify>
LEARNING_RATE = 0.001
print(datetime.datetime.now() - now )
md = find_mode(df_test, 'MSZoning')
 print("MSZoning {}".format(md))
df_test[['MSZoning']] = df_test[['MSZoning']].fillna(md)
 md = 'AllPub'
df_test[['Utilities']] = df_test[['Utilities']].fillna(md)
 md = find_mode(df_test, 'MasVnrType')
 print("MasVnrType {}".format(md))
df_test[['MasVnrType']] = df_test[['MasVnrType']].fillna(md)
 md = find_mode(df_test, 'MasVnrArea')
 print("MasVnrArea {}".format(md))
df_test[['MasVnrArea']] = df_test[['MasVnrArea']].fillna(md)
 simi = ['BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
md = similar_mode(df_test, 'BsmtQual', simi)
 print("BsmtQual {}".format(md))
df_test[['BsmtQual']] = df_test[['BsmtQual']].fillna(md)
 simi = ['BsmtQual', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
md = similar_mode(df_test, 'BsmtCond', simi)
 print("BsmtCond {}".format(md))
df_test[['BsmtCond']] = df_test[['BsmtCond']].fillna(md)
 simi = ['BsmtQual', 'BsmtCond', 'BsmtFinType1', 'BsmtFinType2']
md = similar_mode(df_test, 'BsmtExposure', simi)
 print("BsmtExposure {}".format(md))
df_test[['BsmtExposure']] = df_test[['BsmtExposure']].fillna(md)
 simi = ['BsmtQual', 'BsmtCond', 'BsmtFinType1', 'BsmtFinType2']
md = similar_median(df_test, 'BsmtFullBath', simi)
 print("BsmtFullBath {}".format(md))
df_test[['BsmtFullBath']] = df_test[['BsmtFullBath']].fillna(md)
 simi = ['BsmtQual', 'BsmtCond', 'BsmtFinType1', 'BsmtFinType2']
md = similar_median(df_test, 'BsmtHalfBath', simi)
 print("BsmtHalfBath {}".format(md))
df_test[['BsmtHalfBath']] = df_test[['BsmtHalfBath']].fillna(md)
 md = df_test.KitchenQual.mode().iloc[0]
print("KitchenQual {}".format(md))
df_test[['KitchenQual']] = df_test[['KitchenQual']].fillna(md)
 md = 'Typ'
df_test[['Functional']] = df_test[['Functional']].fillna(md)
 simi = ['GarageType', 'MisGarage']
md = similar_median(df_test, 'GarageYrBlt', simi)
 print("GarageYrBlt {}".format(md))
df_test[['GarageYrBlt']] = df_test[['GarageYrBlt']].fillna(md)
 md = 'Unf'
print("GarageFinish {}".format(md))
df_test[['GarageFinish']] = df_test[['GarageFinish']].fillna(md)
 simi = ['GarageType', 'MisGarage']
md = similar_median(df_test, 'GarageArea', simi)
 print("GarageArea {}".format(md))
df_test[['GarageArea']] = df_test[['GarageArea']].fillna(md)
 simi = ['GarageType', 'MisGarage', 'GarageFinish']
md = similar_mode(df_test, 'GarageQual', simi)
 print("GarageQual {}".format(md))
df_test[['GarageQual']] = df_test[['GarageQual']].fillna(md)
 simi = ['GarageType', 'MisGarage', 'GarageFinish']
md = similar_mode(df_test, 'GarageCond', simi)
 print("GarageCond {}".format(md))
df_test[['GarageCond']] = df_test[['GarageCond']].fillna(md)
 simi = ['GarageType', 'MisGarage']
md = similar_median(df_test, 'GarageCars', simi)
 print("GarageCars {}".format(md))
df_test[['GarageCars']] = df_test[['GarageCars']].fillna(md)
 cols = df_test.columns
mis_test = []
print("Start printing the missing values...")
 for col in cols:
mis = df_test[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df_test.shape[0] * 100, 3)))
 mis_test.append(col)
 print("...done printing the missing values" )<categorify>
models['model'].compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['categorical_accuracy', 'binary_accuracy', f1] )
butch_train = df_train[[col for col in df_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = df_test[[col for col in df_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 print(list(set(butch_train.columns)- set(butch_test.columns)))
 butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train[df_train.GrLivArea < 4500]['SalePrice'])
 old_target = df_train[df_train.GrLivArea < 4500]['SalePrice'] 
(butch_train.columns != butch_test.columns ).sum()<choose_model_class>
seq = Seq(train_labels, extend=False, aug=True, batch_size=8)
 print(len(seq))
lr_scheduler = LearningRateReducer(lr_schedule,
cycle_epoch=1, steps_per_epoch=len(seq),
start_lr=LEARNING_RATE, end_lr=LEARNING_RATE/10)
 callbacks = [lr_scheduler]
hst = models['model'].fit_generator(seq, epochs=1,
steps_per_epoch=len(seq),
callbacks=callbacks )
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<choose_model_class>
seq = Seq(train_labels, extend=False, aug=False, batch_size=32)
 print(len(seq))
xs, ys = next(seq)
 print(xs['input'].shape)
 xs['input'].reshape(( 32,4,-1)).shape
y_pred = models['model_cnvt'].predict(xs['input'].reshape(( 32,4,-1)) [0])
 print(y_pred.shape )
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol<define_search_space>
tmp = np.vstack(
 [
np.hstack([y_pred[0], y_pred[1]]),
np.hstack([y_pred[2], y_pred[3]])
 ])
 tmp.shape
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<compute_train_metric>
Image.fromarray(np.uint8(( tmp+1)/2*255))
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol<compute_test_metric>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=32)
 len(seq_pred )
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )<save_to_csv>
pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 pred.shape
prediction = np.expm1(best_ridge.predict(butch_test))
sub = pd.DataFrame()
sub['Id'] = df_test['Id']
sub['SalePrice'] = prediction
sub.to_csv('best_ridge_full_imputation.csv',index=False )<drop_column>
def calc_threshold(pred):
threshold_dic = {}
for idx in tqdm(range(28)) :
threshold_dic[idx] = 0.5
m = 0
for ii in range(100):
threshold0 = ii*0.01
f1_val = f1_score(y_cat_train_dic[idx], threshold0<(pred[:,idx]))
if m < f1_val:
threshold_dic[idx] = threshold0+0.005
m = f1_val
return threshold_dic
df_train.drop("Exterior1st", axis=1, inplace=True)
 df_test.drop("Exterior1st", axis=1, inplace=True)
 df_train.drop("Exterior2nd", axis=1, inplace=True)
 df_test.drop("Exterior2nd", axis=1, inplace=True)
 df_train.drop("SaleType", axis=1, inplace=True)
 df_test.drop("SaleType", axis=1, inplace=True )<feature_engineering>
warnings.filterwarnings('ignore')
 threshold_dic = calc_threshold(pred)
 threshold_dic
def tr_ExtQual(df):
df.loc[df.ExterQual == 'Fa', 'ExterQual'] = 0
df.loc[df.ExterQual == 'TA', 'ExterQual'] = 1
df.loc[df.ExterQual == 'Gd', 'ExterQual'] = 2
df.loc[df.ExterQual == 'Ex', 'ExterQual'] = 3
df.ExterQual = pd.to_numeric(df.ExterQual)
 return df
def tr_ExtQual_plus(df):
df.loc[df.ExterQual == 'Fa', 'ExterQual'] = 1
df.loc[df.ExterQual == 'TA', 'ExterQual'] = 1
df.loc[df.ExterQual == 'Gd', 'ExterQual'] = 2
df.loc[df.ExterQual == 'Ex', 'ExterQual'] = 3
df.ExterQual = pd.to_numeric(df.ExterQual)
 return df
def tr_ExtCond(df):
df.loc[df.ExterCond == 'Po', 'ExterCond'] = 0
df.loc[df.ExterCond == 'Fa', 'ExterCond'] = 1
df.loc[df.ExterCond == 'TA', 'ExterCond'] = 2
df.loc[df.ExterCond == 'Gd', 'ExterCond'] = 3
df.loc[df.ExterCond == 'Ex', 'ExterCond'] = 4
df.ExterCond = pd.to_numeric(df.ExterCond)
 return df
def tr_ExtCond_plus(df):
df.loc[df.ExterCond == 'Po', 'ExterCond'] = 1
df.loc[df.ExterCond == 'Fa', 'ExterCond'] = 1
df.loc[df.ExterCond == 'TA', 'ExterCond'] = 1
df.loc[df.ExterCond == 'Gd', 'ExterCond'] = 2
df.loc[df.ExterCond == 'Ex', 'ExterCond'] = 2
df.ExterCond = pd.to_numeric(df.ExterCond)
 return df
def tr_BsmtQu(df):
df.loc[df.BsmtQual == 'NoBsmt', 'BsmtQual'] = 0
df.loc[df.BsmtQual == 'Fa', 'BsmtQual'] = 1
df.loc[df.BsmtQual == 'TA', 'BsmtQual'] = 2
df.loc[df.BsmtQual == 'Gd', 'BsmtQual'] = 3
df.loc[df.BsmtQual == 'Ex', 'BsmtQual'] = 4
df.BsmtQual = pd.to_numeric(df.BsmtQual)
 return df
def tr_BsmtQu_plus(df):
df.loc[df.BsmtQual == 'NoBsmt', 'BsmtQual'] = 0
df.loc[df.BsmtQual == 'Fa', 'BsmtQual'] = 1
df.loc[df.BsmtQual == 'TA', 'BsmtQual'] = 4
df.loc[df.BsmtQual == 'Gd', 'BsmtQual'] = 10
df.loc[df.BsmtQual == 'Ex', 'BsmtQual'] = 21
df.BsmtQual = pd.to_numeric(df.BsmtQual)
 return df
def tr_BsmtCo(df):
df.loc[df.BsmtCond == 'NoBsmt', 'BsmtCond'] = 0
df.loc[df.BsmtCond == 'Po', 'BsmtCond'] = 1
df.loc[df.BsmtCond == 'Fa', 'BsmtCond'] = 2
df.loc[df.BsmtCond == 'TA', 'BsmtCond'] = 3
df.loc[df.BsmtCond == 'Gd', 'BsmtCond'] = 4
df.BsmtCond = pd.to_numeric(df.BsmtCond)
 return df
def tr_BsmtExp(df):
df.loc[df.BsmtExposure == 'NoBsmt', 'BsmtExposure'] = 0
df.loc[df.BsmtExposure == 'No', 'BsmtExposure'] = 1
df.loc[df.BsmtExposure == 'Mn', 'BsmtExposure'] = 2
df.loc[df.BsmtExposure == 'Av', 'BsmtExposure'] = 3
df.loc[df.BsmtExposure == 'Gd', 'BsmtExposure'] = 4
df.BsmtExposure = pd.to_numeric(df.BsmtExposure)
 return df
def tr_BsmtExp_plus(df):
df.loc[df.BsmtExposure == 'NoBsmt', 'BsmtExposure'] = 0
df.loc[df.BsmtExposure == 'No', 'BsmtExposure'] = 6
df.loc[df.BsmtExposure == 'Mn', 'BsmtExposure'] = 7
df.loc[df.BsmtExposure == 'Av', 'BsmtExposure'] = 8
df.loc[df.BsmtExposure == 'Gd', 'BsmtExposure'] = 12
df.BsmtExposure = pd.to_numeric(df.BsmtExposure)
 return df
def tr_HeatQ(df):
df.loc[df.HeatingQC == 'Po', 'HeatingQC'] = 0
df.loc[df.HeatingQC == 'Fa', 'HeatingQC'] = 1
df.loc[df.HeatingQC == 'TA', 'HeatingQC'] = 2
df.loc[df.HeatingQC == 'Gd', 'HeatingQC'] = 3
df.loc[df.HeatingQC == 'Ex', 'HeatingQC'] = 4
df.HeatingQC = pd.to_numeric(df.HeatingQC)
 return df
def tr_HeatQ_plus(df):
df.loc[df.HeatingQC == 'Po', 'HeatingQC'] = 1
df.loc[df.HeatingQC == 'Fa', 'HeatingQC'] = 1
df.loc[df.HeatingQC == 'TA', 'HeatingQC'] = 3
df.loc[df.HeatingQC == 'Gd', 'HeatingQC'] = 4
df.loc[df.HeatingQC == 'Ex', 'HeatingQC'] = 7
df.HeatingQC = pd.to_numeric(df.HeatingQC)
 return df
def tr_KitcQu(df):
df.loc[df.KitchenQual == 'Fa', 'KitchenQual'] = 1
df.loc[df.KitchenQual == 'TA', 'KitchenQual'] = 2
df.loc[df.KitchenQual == 'Gd', 'KitchenQual'] = 3
df.loc[df.KitchenQual == 'Ex', 'KitchenQual'] = 4
df.KitchenQual = pd.to_numeric(df.KitchenQual)
 return df
def tr_KitcQu_plus(df):
df.loc[df.KitchenQual == 'Fa', 'KitchenQual'] = 1
df.loc[df.KitchenQual == 'TA', 'KitchenQual'] = 4
df.loc[df.KitchenQual == 'Gd', 'KitchenQual'] = 10
df.loc[df.KitchenQual == 'Ex', 'KitchenQual'] = 21
df.KitchenQual = pd.to_numeric(df.KitchenQual)
 return df
def tr_FireQu(df):
df.loc[df.FireplaceQu == 'NoFire', 'FireplaceQu'] = 0
df.loc[df.FireplaceQu == 'Po', 'FireplaceQu'] = 1
df.loc[df.FireplaceQu == 'Fa', 'FireplaceQu'] = 2
df.loc[df.FireplaceQu == 'TA', 'FireplaceQu'] = 3
df.loc[df.FireplaceQu == 'Gd', 'FireplaceQu'] = 4
df.loc[df.FireplaceQu == 'Ex', 'FireplaceQu'] = 5
df.FireplaceQu = pd.to_numeric(df.FireplaceQu)
 return df
def tr_FireQu_plus(df):
df.loc[df.FireplaceQu == 'NoFire', 'FireplaceQu'] = 0
df.loc[df.FireplaceQu == 'Po', 'FireplaceQu'] = 0
df.loc[df.FireplaceQu == 'Fa', 'FireplaceQu'] = 2
df.loc[df.FireplaceQu == 'TA', 'FireplaceQu'] = 3
df.loc[df.FireplaceQu == 'Gd', 'FireplaceQu'] = 4
df.loc[df.FireplaceQu == 'Ex', 'FireplaceQu'] = 8
df.FireplaceQu = pd.to_numeric(df.FireplaceQu)
 return df
def tr_GarQu(df):
df.loc[df.GarageQual == 'NoGrg', 'GarageQual'] = 0
df.loc[df.GarageQual == 'Po', 'GarageQual'] = 1
df.loc[df.GarageQual == 'Fa', 'GarageQual'] = 2
df.loc[df.GarageQual == 'TA', 'GarageQual'] = 3
df.loc[df.GarageQual == 'Gd', 'GarageQual'] = 4
if df.name == 'Train':
df.loc[df.GarageQual == 'Ex', 'GarageQual'] = 5
df.GarageQual = pd.to_numeric(df.GarageQual)
 return df
def tr_GarCo(df):
df.loc[df.GarageCond == 'NoGrg', 'GarageCond'] = 0
df.loc[df.GarageCond == 'Po', 'GarageCond'] = 1
df.loc[df.GarageCond == 'Fa', 'GarageCond'] = 2
df.loc[df.GarageCond == 'TA', 'GarageCond'] = 3
df.loc[df.GarageCond == 'Gd', 'GarageCond'] = 4
df.loc[df.GarageCond == 'Ex', 'GarageCond'] = 5
df.GarageCond = pd.to_numeric(df.GarageCond)
 return df
def tr_MSZo(df):
df.loc[(df.MSZoning == 'RH')|(df.MSZoning == 'RM'), 'MSZoning'] = 'ResMedHig'
df.loc[(df.MSZoning == 'FV'), 'MSZoning'] = 'Vil'
df.loc[(df.MSZoning == 'RL')|(df.MSZoning == 'C(all)'), 'MSZoning'] = 'ResLowCom'
return df
def tr_Alley(df):
df.loc[(df.Alley == 'Grvl')|(df.Alley == 'Pave'), 'Alley'] = 'Alley'
df.loc[df.Alley == 'NoAlley', 'Alley'] = 'NoAlley'
return df
def tr_LotSh(df):
irr = ['IR1', 'IR2', 'IR3']
df.loc[(df.LotShape.isin(irr)) , 'LotShape'] = 'Irreg'
df.loc[df.LotShape == 'Reg', 'LotShape'] = 'Reg'
return df
def tr_Cond1(df):
ArtFee = ['Artery', 'Feedr']
stat = ['RRAe', 'RRAn', 'RRNe', 'RRNn']
pos = ['PosA', 'PosN']
df.loc[(df.Condition1.isin(ArtFee)) , 'Condition1'] = 'ArtFee'
df.loc[(df.Condition1.isin(stat)) , 'Condition1'] = 'Station'
df.loc[(df.Condition1.isin(pos)) , 'Condition1'] = 'Station'
df.loc[df.Condition1 == 'Norm', 'Condition1'] = 'Norm'
return df
def tr_BldTy(df):
df.loc[(df.BldgType == '2fmCon')|(df.BldgType == 'Duplex'), 'BldgType'] = '2FamDup'
df.loc[(df.BldgType == 'Twnhs')|(df.BldgType == 'TwnhsE'), 'BldgType'] = 'Twnhs+E'
df.loc[(df.BldgType == '1Fam'), 'BldgType'] = '1Fam'
return df
def tr_HSty(df):
onepl = ['1.5Fin', '1.5Unf']
twopl = ['2.5Fin', '2.5Unf', '2Story']
spl = ['SFoyer', 'SLvl']
df.loc[df.HouseStyle.isin(onepl), 'HouseStyle'] = '1.5'
df.loc[df.HouseStyle.isin(twopl), 'HouseStyle'] = '2plus'
df.loc[df.HouseStyle.isin(spl), 'HouseStyle'] = 'Split'
df.loc[df.HouseStyle == '1Story', 'HouseStyle'] = "1Story"
return df
def tr_Found(df):
fancy = ['BrkTil', 'Stone', 'Wood']
cement = ['PConc', 'Slab']
df.loc[df.Foundation.isin(fancy), 'Foundation'] = 'Fancy'
df.loc[df.Foundation.isin(cement), 'Foundation'] = 'Cement'
df.loc[df.Foundation == 'CBlock', 'Foundation'] = 'Cider'
return df
def tr_MasVnrTy(df):
df.loc[df.MasVnrType == 'None', 'MasVnrType'] = 'None'
df.loc[df.MasVnrType == 'Stone', 'MasVnrType'] = 'Stone'
df.loc[(df.MasVnrType == 'BrkCmn')|(df.MasVnrType == 'BrkFace'), 'MasVnrType'] = 'Bricks'
return df
def tr_Elec(df):
df.loc[df.Electrical == "SBrkr", "Electrical"] = "SBrkr"
fuse = ['FuseA', 'FuseF', 'FuseP', 'Mix']
df.loc[df.Electrical.isin(fuse), "Electrical"] = "Fuse"
return df
def tr_GrgTy(df):
incl = ['Attchd', 'Basment', 'BuiltIn']
escl = ['2Types', 'CarPort', 'Detchd']
df.loc[df.GarageType.isin(incl), 'GarageType'] = 'Connected'
df.loc[df.GarageType.isin(escl), 'GarageType'] = 'NonConnected'
df.loc[df.GarageType == 'NoGrg', 'GarageType'] = 'NoGrg'
return df
def tr_PvdDr(df):
df.loc[df.PavedDrive == 'N', 'PavedDrive'] = 'N'
df.loc[df.PavedDrive == 'P', 'PavedDrive'] = 'N'
df.loc[df.PavedDrive == 'Y', 'PavedDrive'] = 'Y'
return df
def tr_Fence(df):
df.loc[df.Fence == 'NoFence', 'Fence'] = 'NoFence'
df.loc[(df.Fence == 'MnPrv')|(df.Fence == 'GdPrv'), 'Fence'] = 'Prv'
df.loc[(df.Fence == 'MnWw')|(df.Fence == 'GdWo'), 'Fence'] = 'Wo'
return df<categorify>

 for ii in range(28):
print(ii, f1_score(y_cat_train_dic[ii], threshold_dic[ii]<pred[:,ii]))
tr_train = df_train.copy()
tr_test = df_test.copy()
tr_train.name = 'Train'
tr_test.name = 'Test'
tr_train = tr_GarQu(tr_train)
 tr_test = tr_GarQu(tr_test)
 tr_train = tr_FireQu(tr_train)
 tr_test = tr_FireQu(tr_test)
 tr_train = tr_HeatQ_plus(tr_train)
 tr_test = tr_HeatQ_plus(tr_test)
 tr_train = tr_PvdDr(tr_train)
 tr_test = tr_PvdDr(tr_test)
 tr_train = tr_GrgTy(tr_train)
 tr_test = tr_GrgTy(tr_test)
 tr_train = tr_ExtQual_plus(tr_train)
 tr_test = tr_ExtQual_plus(tr_test)
 tr_train = tr_MasVnrTy(tr_train)
 tr_test = tr_MasVnrTy(tr_test)
 tr_train = tr_GarCo(tr_train)
 tr_test = tr_GarCo(tr_test)
 tr_train = tr_BsmtCo(tr_train)
 tr_test = tr_BsmtCo(tr_test)
 tr_train = tr_Elec(tr_train)
 tr_test = tr_Elec(tr_test)
 tr_train = tr_BsmtQu_plus(tr_train)
 tr_test = tr_BsmtQu_plus(tr_test)
 tr_train = tr_LotSh(tr_train)
 tr_test = tr_LotSh(tr_test)
 tr_train = tr_Alley(tr_train)
 tr_test = tr_Alley(tr_test )<categorify>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=32)
 seq_test
butch_train = tr_train[[col for col in tr_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = tr_test[[col for col in tr_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 print(set(df_train.columns)- set(df_test.columns))
butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train['SalePrice'])
(butch_train.columns != butch_test.columns ).sum()<choose_model_class>
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 pred_test.shape
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<choose_model_class>
def make_test(pred):
test_labels1 = test_labels.copy()
test_labels1['Predicted'] = [str(ee)for ee in np.argmax(pred, axis=1)]
print(test_labels1.head())
 test_labels2 = test_labels1.copy()
for ii in range(test_labels2.shape[0]):
threshold = list(zip(*sorted(list(threshold_dic.items()), key=lambda x:x[0], reverse=False)))[1]
idx = threshold < pred[ii,:]
tgt = test_labels2['Predicted'][ii]
tgt = [tgt] + [str(ee)for ee in np.arange(28)[idx]]
tgt = set(tgt)
 tgt = ' '.join(tgt)
 test_labels2['Predicted'][ii] = tgt
print(test_labels2.head())
 return test_labels1, test_labels2
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol<define_search_space>
test_labels1_1, test_labels1_2 = make_test(pred_test )
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<compute_train_metric>
test_labels1_2.to_csv('InceptionResNetV1_2.csv', index=False )
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol<compute_test_metric>

 No = 1
models['model_cnvt'].save_weights('model_{}_cnvt.h5'.format(No))
models['model_resnet'].save_weights('model_{}_resnet.h5'.format(No))
models['model_classifier'].save_weights('model_{}_classifier.h5'.format(No))
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )<define_variables>
df_pred1 = pd.DataFrame(pred)
 df_pred1.columns = ['cls'+ str(ii)for ii in range(28)]
df_pred1 = pd.concat([train_labels.copy() , df_pred1], axis=1)
 df_pred1.head()
df_pred1.to_csv('proba_{}.csv'.format(No), index=False )
<choose_model_class><EOS> 
df_pred1_test = pd.DataFrame(pred_test)
 df_pred1_test.columns = ['cls'+ str(ii)for ii in range(28)]
df_pred1_test = pd.concat([test_labels.copy() , df_pred1_test], axis=1)
 df_pred1_test.head()
df_pred1_test.to_csv('proba_test_{}.csv'.format(No), index=False )