Kaggle competition: Human Protein Atlas Image Classification
<SOS> metric: MacroFScore Kaggle data source: human-protein-atlas-image-classification<categorify>
%matplotlib inline
labelencoder_X = LabelEncoder()
X_train[:, 1] = labelencoder_X.fit_transform(X_train[:, 1])
 X_train[:, 6] = labelencoder_X.fit_transform(X_train[:, 6])
 X_test[:, 1] = labelencoder_X.fit_transform(X_test[:, 1])
 X_test[:, 6] = labelencoder_X.fit_transform(X_test[:, 6] )<correct_missing_values>
try:
shutil.copytree(".. /input/keras-pretrained-models", "/tmp/.keras/models")
 except FileExistsError:
pass
imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
 X_train[:,:] = imp.fit_transform(X_train[:,:])
 X_test[:,:] = imp.fit_transform(X_test[:,:] )<categorify>
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
onehotencoder = OneHotEncoder(categorical_features = [6])
 X_train = onehotencoder.fit_transform(X_train ).toarray()
onehotencoder2 = OneHotEncoder(categorical_features = [6])
 X_test = onehotencoder2.fit_transform(X_test ).toarray()<train_model>
from PIL import Image
from zipfile import ZipFile
import h5py
import cv2
from tqdm import tqdm
classifier = XGBClassifier(learning_rate=0.001,n_estimators=1000)
 classifier.fit(X_train, Y_train )<predict_on_test>
src_dir = '.. /input/human-protein-atlas-image-classification'
Y_pred = classifier.predict(X_test )<save_to_csv>
train_labels = pd.read_csv(os.path.join(src_dir, "train.csv"))
print(train_labels.shape)
 train_labels.head(10 )
ans =pd.DataFrame({'PassengerId':pid_col, 'Survived':Y_pred})
 ans.to_csv("titanicPred.csv", index=False )<set_options>
test_labels = pd.read_csv(os.path.join(src_dir, "sample_submission.csv"))
print(test_labels.shape)
 test_labels.head()
%load_ext autoreload
%autoreload 2
%matplotlib inline<set_options>
SH =(139, 139)
 ID_LIST_TRAIN = train_labels.Id.tolist()
sns.set(style="darkgrid")
 warnings.filterwarnings('ignore' )<define_variables>
img_cache_train = np.load('.. /input/139x139-resized-numpy-array/img_cache_train_resize_139x139.npz')['x']
PATH = ".. /input/house-prices-advanced-regression-techniques/"<load_from_csv>
def get_arr(Id, test=False):
if test:
arr = get_arr0(Id, test=True)
 arr = cv2.resize(arr, SH ).astype('float32')
 else:
ii = ID_LIST_TRAIN.index(Id)
 arr = img_cache_train[ii]
arr = arr.astype('float32')
 return arr
df_train=pd.read_csv(f'{PATH}train.csv')
 df_test=pd.read_csv(f'{PATH}test.csv' )<feature_engineering>
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
df_train.SalePrice = np.log1p(df_train.SalePrice )<train_model>
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
print('Number of Training Examples = {}'.format(df_train.shape[0]))
print('Number of Test Examples = {}
'.format(df_test.shape[0]))
print('Training X Shape = {}'.format(df_train.shape))
print('Training y Shape = {}
'.format(df_train['SalePrice'].shape[0]))
print('Test X Shape = {}'.format(df_test.shape))
print('Test y Shape = {}
'.format(df_test.shape[0])) 
<define_variables>
up_sample2 = list(zip(*sorted(list(up_sample.items()), key=lambda x: x[0])))[1]
up_sample2 = np.array(up_sample2)
 up_sample2
ntrain = df_train.shape[0]
ntest = df_test.shape[0]
train_ID = df_train['Id']
test_ID = df_test['Id']<concatenate>
class Seq(object):
sections = None
index = None
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[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_arr(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, sections=sections):
res0 = np.vsplit(arr, sections)
 res = [np.hsplit(ee, sections)for ee in res0]
res = list(chain.from_iterable(res))
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:
img = self.random_transform(arr0)
 else:
img = arr0
xs.append(img.flatten())
 ys.append(cat )
def concat_df(train_data, test_data):
return pd.concat([train_data, test_data], sort=True ).reset_index(drop=True)
 df_all = concat_df(df_train, df_test)
 df_train.name = 'Training Set'
df_test.name = 'Test Set'
df_all.name = 'All Set'
dfs = [df_train, df_test]
df_all.shape<prepare_x_and_y>
from keras import applications
y_train_full = df_train.SalePrice.values
df_all.drop(['SalePrice'], axis=1, inplace=True)
 df_all.drop('Id',axis=1,inplace=True )<count_missing_values>
model_resnet = applications.inception_resnet_v2.InceptionResNetV2(
 include_top=False,
weights='imagenet',
input_tensor=None,
input_shape=(139,139,3),
pooling='avg',
classes=None )
def mark_missing(df):
for col in df.columns:
if df_all[col].isnull().sum() >0:
df_all[col+'_missed']=df_all[col].isnull()<count_missing_values>
def make_trainable_false(model_resnet, trainable=False):
layers = model_resnet.layers
for ilayer in layers:
ilayer.trainable = trainable
return
mark_missing(df_all )<count_missing_values>
model_resnet.layers[1].get_weights() [0][:,:,:,0]
def display_missing(df):
for col in df.columns:
print(col, df[col].isnull().sum())
 print('
')
 for df in dfs:
print(format(df.name))
display_missing(df)
 def display_only_missing(df):
all_data_na =(df.isnull().sum() / len(df)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 print(missing_data )<count_missing_values>
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(1e-4))(oup)
 model_cnvt = Model(inp, oup, name='model_cnvt')
 return model_cnvt
display_only_missing(df_all )<data_type_conversions>
model_cnvt = make_model_cnvt(img_dim, img_shape)
 model_cnvt.summary()
for col in('Alley','MasVnrType','BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1',
'BsmtFinType2','FireplaceQu','GarageType', 'GarageFinish', 'GarageQual',
'GarageCond','PoolQC','Fence','MiscFeature'):
df_all[col] = df_all[col].fillna('None' )<count_missing_values>
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
display_only_missing(df_all )<data_type_conversions>
def make_model(img_dim, model_cnvt, model_resnet, model_classifier):

 inp = Input(shape=(img_dim,), name='input')
 oup = model_cnvt(inp)
 oup = model_resnet(oup)
 oup = model_classifier(oup)
 oup = Activation('linear', name='path_cls_cls' )(oup)
 model = Model(inp, oup, name='model')
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['categorical_accuracy', 'binary_accuracy'])
 return {
'model_classifier': model_classifier,
'model_resnet': model_resnet,
'model_cnvt': model_cnvt,
'model': model
}
models = make_model(img_dim, model_cnvt, model_resnet, model_classifier)
 models['model'].summary()
for col in('GarageYrBlt', 'GarageArea', 'GarageCars','BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath','MasVnrArea'):
df_all[col] = df_all[col].fillna(0 )<count_missing_values>

 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
display_only_missing(df_all )<categorify>
model_resnet.layers[1].get_weights() [0][:,:,:,0]
for col in('Utilities','Functional','SaleType','KitchenQual','Exterior2nd','Exterior1st','Electrical'):
df_all[col].fillna(df_all[col].mode() [0], inplace=True)
<count_missing_values>
make_trainable_false(model_resnet, trainable=False)
 models['model'].compile(loss=focal_loss,
optimizer='adam',
metrics=['categorical_accuracy', 'binary_accuracy'] )
display_only_missing(df_all )<count_missing_values>
model_resnet.layers[1].get_weights() [0][:,:,:,0]
df_all.MSZoning.isnull().sum()<data_type_conversions>
seq = Seq(train_labels, extend=False, aug=True, batch_size=128)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=5,
steps_per_epoch=len(seq),
callbacks=callbacks )
df_all["MSZoning"] = df_all["MSZoning"].fillna("None" )<count_missing_values>
model_resnet.layers[1].get_weights() [0][:,:,:,0]
display_only_missing(df_all )<count_missing_values>
seq = Seq(train_labels, extend=False, aug=False, batch_size=32)
 print(len(seq))
xs, ys = next(seq)
 print(xs['input'].shape)
 y_pred = models['model_cnvt'].predict(xs['input'])
 print(y_pred.shape )
df_all['LotFrontage'].isnull().sum()<categorify>
Image.fromarray(np.uint8(( y_pred[0]+1)/2*255))
df_all["LotFrontage"] = df_all.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))<count_missing_values>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred )
df_all['LotFrontage'].isnull().sum()<count_missing_values>
pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1 )
display_only_missing(df_all )<split>
def calc_threshold(pred):
threshold_dic = {}
for idx in tqdm(range(28)) :
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
def quick_get_dumm(df):
X_train_full=df.iloc[:ntrain]
X_train, X_valid, y_train, y_valid = train_test_split(pd.get_dummies(X_train_full), y_train_full, random_state=42)
 return X_train, X_valid, y_train, y_valid<prepare_x_and_y>
threshold_dic = calc_threshold(pred )
X_train, X_valid, y_train, y_valid = quick_get_dumm(df_all )<compute_test_metric>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
def rmse(x,y): return math.sqrt(((x-y)**2 ).mean())
 def print_score(m,X_train=X_train, X_valid=X_valid, y_train=y_train, y_valid=y_valid):
res = [rmse(m.predict(X_train), y_train), rmse(m.predict(X_valid), y_valid),
m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )<train_model>
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1 )
m_rf = RandomForestRegressor(n_estimators=160, min_samples_leaf=1, max_features=0.5, n_jobs=-1, oob_score=True, random_state=42)
 m_rf.fit(X_train, y_train)
 print_score(m_rf )<train_on_grid>
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
def elastic_score(X,y):
elastic = ElasticNet(random_state=1)
 param = {'l1_ratio' : [0],
'alpha' : [0.017]}
elastic = GridSearchCV(elastic, param, cv=5, scoring='neg_mean_squared_error')
 elastic.fit(X,y)
 print('Elastic:', np.sqrt(elastic.best_score_*-1))
return elastic<compute_test_metric>
test_labels1_1, test_labels1_2 = make_test(pred_test )
elastic_score(X_train, y_train )<train_model>
test_labels1_2.to_csv('InceptionResNetV1_2.csv', index=False )
m_xgb = XGBRegressor(n_estimators=160, learning_rate=0.05, random_state=42)
 m_xgb.fit(X_train, y_train)
 print_score(m_xgb )<data_type_conversions>

 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))
def return_original_na(df, feature):
df[feature].loc[df.index[df[feature+'_missed'] == True].tolist() ]=np.nan
return df[feature]<categorify>
ls -la
df_all['LotFrontage']=return_original_na(df_all, 'LotFrontage' )<count_missing_values>
model_resnet = applications.inception_resnet_v2.InceptionResNetV2(
 include_top=False,
weights='imagenet',
input_tensor=None,
input_shape=(139,139,3),
pooling='avg',
classes=None)
df_all['LotFrontage'].isnull().sum()<count_missing_values>
models['model'].compile(loss=focal_loss,
optimizer='adam',
metrics=['categorical_accuracy', 'binary_accuracy'] )
display_only_missing(df_all )<categorify>
seq = Seq(train_labels, aug=True, batch_size=64)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=5,
steps_per_epoch=len(seq),
callbacks=[] )
def filling_na_with_predictions(df, feature):

 flag_object=0
if df[feature].isnull().sum() >0:
na_rows_idxs=df[df[feature].isnull() ].index
df=pd.concat([ pd.Series(df[feature]), pd.get_dummies(df.drop([feature], axis=1)) ], axis=1)
 Feature_Train_X=df.drop(df[df[feature].isnull() ].index ).drop([feature], axis=1)
 Feature_Train_y=df[feature].drop(df[df[feature].isnull() ].index ).values
Feature_Predict_X=df[df[feature].isnull() ].drop([feature], axis=1)
 if is_string_dtype(df[feature])or is_categorical_dtype(df[feature]):
flag_object=1
le = LabelEncoder()
le.fit(Feature_Train_y)
 Feature_Train_y=le.transform(Feature_Train_y)
 elastic = ElasticNet(random_state=1)
 param = {'l1_ratio' : [0],
'alpha' : [0.017]}
elastic = GridSearchCV(elastic, param, cv=5, scoring='neg_mean_squared_error')
 elastic.fit(Feature_Train_X,Feature_Train_y)
 fillna_values=elastic.predict(Feature_Predict_X)
 if flag_object==1:
fillna_values=le.inverse_transform(np.around(fillna_values ).astype(int))
df[feature]=df[feature].fillna(pd.Series(index=na_rows_idxs,data=fillna_values))
return df[feature]
else:
print('There were no NA values' )<feature_engineering>
model_resnet.layers[1].get_weights() [0][:,:,:,0]
df_tmp_train=df_all.iloc[:ntrain]
df_tmp_train['SalePrice']=y_train_full
df_tmp_test=df_all.iloc[ntrain:]<categorify>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
df_tmp_train['LotFrontage']=filling_na_with_predictions(df_tmp_train, "LotFrontage")
 df_tmp_test['LotFrontage']=filling_na_with_predictions(df_tmp_test, "LotFrontage" )<concatenate>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels2_1, test_labels2_2 = make_test(pred_test)
 test_labels2_2.head()
df_tmp_train.drop(['SalePrice'], axis=1, inplace=True)
 df_all = concat_df(df_tmp_train, df_tmp_test )<count_missing_values>
test_labels2_2.to_csv('InceptionResNetV2_2.csv', index=False )
df_all['LotFrontage'].isnull().sum()<train_model>

 No = 2
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))
def evaluate(df):
X_train, X_valid, y_train, y_valid = quick_get_dumm(df)
 elastic_score(X_train, y_train)
 m_xgb.fit(X_train, y_train)
 print('XGBoost')
 print_score(m_xgb,X_train, X_valid, y_train, y_valid)
 m_rf.fit(X_train, y_train)
 print('Random Forest')
 print_score(m_rf,X_train, X_valid, y_train, y_valid )<prepare_output>
seq = Seq(train_labels, aug=True, batch_size=128)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=5,
steps_per_epoch=len(seq),
callbacks=[] )
evaluate(df_all )<categorify>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
df_all['MSZoning']=return_original_na(df_all, 'MSZoning' )<count_missing_values>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels3_1, test_labels3_2 = make_test(pred_test)
 test_labels3_2.head()
display_only_missing(df_all )<filter>
test_labels3_2.to_csv('InceptionResNetV3_2.csv', index=False )
df_all[df_all['MSZoning'].isnull() ].index<categorify>

 No = 3
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))
df_all['MSZoning']=filling_na_with_predictions(df_all, 'MSZoning' )<filter>
def lr_schedule(epoch):
lr = 1e-4
print('Learning rate: ', lr)
 return lr
df_all['MSZoning'].loc[df_all.index[df_all['MSZoning'+'_missed'] == True].tolist() ]<prepare_output>
lr_scheduler = LearningRateScheduler(lr_schedule)
 callbacks = [lr_scheduler]
seq = Seq(train_labels, aug=True, batch_size=128)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=5,
steps_per_epoch=len(seq),
callbacks=callbacks )
evaluate(df_all )<categorify>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
for col in('Utilities','Functional','SaleType','KitchenQual','Exterior2nd','Exterior1st','Electrical'):
print('Filling with most common:
',df_all[col].loc[df_all.index[df_all[col+'_missed'] == True].tolist() ])
 df_all[col]=return_original_na(df_all, col)
 df_all[col]=filling_na_with_predictions(df_all, col)
 print('Filling with predictions:
',df_all[col].loc[df_all.index[df_all[col+'_missed'] == True].tolist() ] )<prepare_output>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels4_1, test_labels4_2 = make_test(pred_test)
 test_labels4_2.head()
evaluate(df_all )<prepare_x_and_y>
test_labels4_2.to_csv('InceptionResNetV4_2.csv', index=False )
df_train_save=df_all.iloc[:ntrain]
df_train_save['SalePrice']=y_train_full
df_test_save=df_all.iloc[ntrain:] 
<save_to_csv>

 No = 4
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))
df_train_save.to_csv('train_clean.csv', index=False)
 df_test_save.to_csv('test_clean.csv', index=False )<sort_values>
def lr_schedule(epoch):
lr = 1e-4
print('Learning rate: ', lr)
 return lr
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
skewness = df_all.select_dtypes(include=numerics ).apply(lambda x: skew(x))
skew_index = skewness[abs(skewness)>= 0.85].index
skewness[skew_index].sort_values(ascending=False )<feature_engineering>
lr_scheduler = LearningRateScheduler(lr_schedule)
 callbacks = [lr_scheduler]
seq = Seq(train_labels, aug=True, batch_size=128)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=5,
steps_per_epoch=len(seq),
callbacks=callbacks )
lam = 0.15
for column in skew_index:
df_all[column] = boxcox1p(df_all[column], lam)
<prepare_output>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
evaluate(df_all )<categorify>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels5_1, test_labels5_2 = make_test(pred_test)
 test_labels5_2.head()
df_all=pd.get_dummies(df_all )<split>
test_labels5_2.to_csv('InceptionResNetV5_2.csv', index=False )
X_test=df_all.iloc[ntrain:]
X_train_full=df_all.iloc[:ntrain]
X_train, X_valid, y_train, y_valid = train_test_split(pd.get_dummies(X_train_full), y_train_full )<drop_column>

 No = 5
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))
all_features = df_all.keys()
df_all = df_all.drop(df_all.loc[:,(df_all==0 ).sum() >=(df_all.shape[0]*0.984)],axis=1)
 df_all = df_all.drop(df_all.loc[:,(df_all==1 ).sum() >=(df_all.shape[0]*0.984)],axis=1)
 remain_features = df_all.keys()
remov_features = [st for st in all_features if st not in remain_features]
print(len(remov_features), 'features were removed:', remov_features )<prepare_output>
ls -la
evaluate(df_all )<normalization>
seq = Seq(train_labels, extend=False, aug=False, batch_size=32)
 print(len(seq))
xs, ys = next(seq)
 print(xs['input'].shape)
 y_pred = models['model_cnvt'].predict(xs['input'])
 print(y_pred.shape )
<prepare_output><EOS> 
Image.fromarray(np.uint8(( y_pred[0]+1)/2*255))