Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<feature_engineering>
%matplotlib inline
print("Setup Complete")
 warnings.filterwarnings('ignore' )
<define_variables>
df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv',na_values='nan')
 df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv',na_values='nan')
 print("Train size:",df_train.shape)
 print("Test size:",df_test.shape)
 train_ID =df_train['Id']
test_ID =df_test['Id']
df_train.drop("Id", axis = 1, inplace = True)
 df_test.drop("Id", axis = 1, inplace = True)
 print("Size of train data after dropping Id: {}".format(df_train.shape))
print("Size of test data after dropping Id: {}".format(df_test.shape))
<define_variables>
train = df_train.drop(df_train[(df_train['GrLivArea']>4000)&(df_train['SalePrice']<300000)].index )
<train_model>
p_df=[]
for col in df_train_num.columns:
stat, p = shapiro(df_train[col])
 p_df.append(p)
 p_df_=pd.DataFrame(p_df,df_train_num.columns ).sort_values(by=0,ascending=True)
 p_df_=p_df_.loc[p_df_[0]>0.05]
p_df_
<choose_model_class>
ntrain = train.shape[0]
ntest = df_test.shape[0]
all_data = pd.concat(( train,df_test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))
categorical_inputs = []
for cat in cat_cols:
categorical_inputs.append(Input(shape=[1], name=cat))
categorical_embeddings = []
for i, cat in enumerate(cat_cols):
categorical_embeddings.append(Embedding(embed_sizes[i], 10 )(categorical_inputs[i]))
categorical_logits = Flatten()(categorical_embeddings[0])
 categorical_logits = Dense(32,activation='relu' )(categorical_logits)
 categorical_logits =Dropout(0.5 )(categorical_logits)
 categorical_logits =BatchNormalization()(categorical_logits)
 categorical_logits = Dense(32,activation='relu' )(categorical_logits)
 numerical_inputs = Input(shape=(11,), name='num')
 numerical_logits = numerical_inputs
numerical_logits = BatchNormalization()(numerical_logits)
 numerical_logits = Dense(128,activation='relu' )(numerical_logits)
 numerical_logits=Dropout(0.5 )(numerical_logits)
 numerical_logits = BatchNormalization()(numerical_logits)
 numerical_logits = Dense(128,activation='relu' )(numerical_logits)
 numerical_logits = Dense(64,activation='relu' )(numerical_logits)
 logits = Concatenate()([numerical_logits,categorical_logits])
 logits = Dense(64,activation='relu' )(logits)
 out = Dense(1, activation='sigmoid' )(logits)
 model = Model(inputs = categorical_inputs + [numerical_inputs], outputs=out)
 model.compile(optimizer='adam',loss=binary_crossentropy )<prepare_x_and_y>
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na} )
def get_input(market_train, indices):
X_num = market_train.loc[indices, num_cols].values
X = {'num':X_num}
for cat in cat_cols:
X[cat] = market_train.loc[indices, cat_cols].values
y =(market_train.loc[indices,'returnsOpenNextMktres10'] >= 0 ).values
r = market_train.loc[indices,'returnsOpenNextMktres10'].values
u = market_train.loc[indices, 'universe']
d = market_train.loc[indices, 'time'].dt.date
return X,y,r,u,d
X_train,y_train,r_train,u_train,d_train = get_input(market_train_df1, train_indices)
 X_valid,y_valid,r_valid,u_valid,d_valid = get_input(market_train_df1, val_indices )<train_model>
all_data_rep=all_data.copy()
all_data_rep["PoolQC"] = all_data_rep["PoolQC"].fillna(0)
 all_data_rep["MiscFeature"] = all_data_rep["MiscFeature"].fillna(0)
 all_data_rep["Fence"] = all_data_rep["Fence"].fillna(0)
 all_data_rep["FireplaceQu"] = all_data_rep["FireplaceQu"].fillna(0)
 all_data_rep["LotFrontage"] = all_data_rep.groupby('Neighborhood')["LotFrontage"].transform(
 lambda x:x.fillna(x.median()))
grd_col=["GarageQual","GarageCond","GarageType","GarageFinish","GarageArea","GarageCars","GarageYrBlt"]
for col in grd_col:
all_data_rep[col] = all_data_rep[col].fillna(0)
 grd_col=["BsmtExposure","BsmtCond","BsmtQual","BsmtFinType1","BsmtFinType2","BsmtUnfSF","BsmtFinSF1","BsmtFinSF2"]
for col in grd_col:
all_data_rep[col] = all_data_rep[col].fillna(0)
 all_data_rep["TotalBsmtSF"] = all_data_rep["TotalBsmtSF"].fillna(0)
 all_data_rep["MasVnrArea"] = all_data_rep["MasVnrArea"].fillna(0)
 all_data_rep["MasVnrType"] = all_data_rep["MasVnrType"].fillna(0)
 all_data_rep['MSZoning']=all_data_rep['MSZoning'].fillna(all_data_rep['MSZoning'].mode() [0])
 all_data_rep["BsmtFullBath"] = all_data_rep["BsmtFullBath"].fillna(0)
 all_data_rep["BsmtHalfBath"] = all_data_rep["BsmtHalfBath"].fillna(0)
 all_data_rep["Functional"] =all_data_rep["Functional"].fillna("Typ")
 all_data_rep["Alley"] = all_data_rep["Alley"].fillna(0)
 mode_col = ['Electrical','KitchenQual', 'Exterior1st', 'Exterior2nd', 'SaleType']
for col in mode_col:
all_data_rep[col] = all_data_rep[col].fillna(all_data_rep[col].mode() [0])
 all_data_rep["MoSold"] = all_data_rep["MoSold"].fillna(0 )
class SWA(keras.callbacks.Callback):
def __init__(self, filepath, swa_epoch):
super(SWA, self ).__init__()
self.filepath = filepath
self.swa_epoch = swa_epoch
def on_train_begin(self, logs=None):
self.nb_epoch = self.params['epochs']
print('Stochastic weight averaging selected for last {} epochs.' 
.format(self.nb_epoch - self.swa_epoch))
def on_epoch_end(self, epoch, logs=None):
if epoch == self.swa_epoch:
self.swa_weights = self.model.get_weights()
elif epoch > self.swa_epoch:
for i in range(len(self.swa_weights)) :
self.swa_weights[i] =(self.swa_weights[i] * 
(epoch - self.swa_epoch)+ self.model.get_weights() [i])/(( epoch - self.swa_epoch)+ 1)
 else:
pass
def on_train_end(self, logs=None):
self.model.set_weights(self.swa_weights)
 print('Final model parameters set to stochastic weight average.')
 self.model.save_weights(self.filepath)
 print('Final stochastic averaged weights saved to file.')
 class SnapshotCallbackBuilder:
def __init__(self, nb_epochs, nb_snapshots, init_lr=0.1):
self.T = nb_epochs
self.M = nb_snapshots
self.alpha_zero = init_lr
def get_callbacks(self, model_prefix='Model'):
callback_list = [
callbacks.ModelCheckpoint("model.hdf5",monitor='val_my_iou_metric',
mode = 'max', save_best_only=True, verbose=1),
swa,
callbacks.LearningRateScheduler(schedule=self._cosine_anneal_schedule)
 ]
return callback_list
def _cosine_anneal_schedule(self, t):
cos_inner = np.pi *(t %(self.T // self.M))
cos_inner /= self.T // self.M
cos_out = np.cos(cos_inner)+ 1
return float(self.alpha_zero / 2 * cos_out )<train_model>
all_data_rep.isnull().sum().sort_values(ascending=False)[:3]
check_point = ModelCheckpoint('model.hdf5',verbose=True, save_best_only=True)
 early_stop = EarlyStopping(patience=5,verbose=True)
 model.fit(X_train,y_train.astype(int),
validation_data=(X_valid,y_valid.astype(int)) ,
epochs=5,
verbose=True,
callbacks=[early_stop,check_point] )<compute_test_metric>
all_data_rep['TotalSF'] = all_data_rep['TotalBsmtSF'] + all_data_rep['1stFlrSF'] + all_data_rep['2ndFlrSF']
r_valid = r_valid.clip(-1,1)
 x_t_i = confidence_valid * r_valid * u_valid
data = {'day' : d_valid, 'x_t_i' : x_t_i}
df = pd.DataFrame(data)
 x_t = df.groupby('day' ).sum().values.flatten()
mean = np.mean(x_t)
 std = np.std(x_t)
 score_valid = mean / std
print(score_valid )<define_variables>
all_data_rep_enc_1=all_data_rep.copy()
days = env.get_prediction_days()<categorify>
all_data_rep_enc_1['MSSubClass']=all_data_rep_enc_1['MSSubClass'].astype(str)
 all_data_rep_enc_1['YrSold']=all_data_rep_enc_1['YrSold'].astype(str)
 all_data_rep_enc_1['MoSold']=all_data_rep_enc_1['MoSold'].astype(str)
n_days = 0
prep_time = 0
prediction_time = 0
packaging_time = 0
predicted_confidences = np.array([])
 for(market_obs_df, news_obs_df, predictions_template_df)in days:
n_days +=1
print(n_days,end=' ')
 t = time.time()
market_obs_df['assetCode_encoded'] = market_obs_df[cat].astype(str ).apply(lambda x: encode(encoders[i], x))
market_obs_df[num_cols] = market_obs_df[num_cols].fillna(0)
 market_obs_df[num_cols] = scaler.transform(market_obs_df[num_cols])
 X_num_test = market_obs_df[num_cols].values
X_test = {'num':X_num_test}
X_test['assetCode'] = market_obs_df['assetCode_encoded'].values
prep_time += time.time() - t
t = time.time()
market_prediction = model.predict(X_test)[:,0]*2 -1
predicted_confidences = np.concatenate(( predicted_confidences, market_prediction))
prediction_time += time.time() -t
t = time.time()
preds = pd.DataFrame({'assetCode':market_obs_df['assetCode'],'confidence':market_prediction})
 predictions_template_df = predictions_template_df.merge(preds,how='left' ).drop('confidenceValue',axis=1 ).fillna(0 ).rename(columns={'confidence':'confidenceValue'})
 env.predict(predictions_template_df)
 packaging_time += time.time() - t
env.write_submission_file()
total = prep_time + prediction_time + packaging_time
print(f'Preparing Data: {prep_time:.2f}s')
 print(f'Making Predictions: {prediction_time:.2f}s')
 print(f'Packing: {packaging_time:.2f}s')
 print(f'Total: {total:.2f}s' )<define_variables>
qual_map = {0: 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
qual_col=['FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond','ExterQual',
'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual']
for col in qual_col:
all_data_rep_enc_1[col] = all_data_rep_enc_1[col].map(qual_map)
 bsm_exp_map={0: 0, 'No': 1, 'Mn': 2, 'Av': 3, 'Gd': 4}
all_data_rep_enc_1['BsmtExposure'] = all_data_rep_enc_1['BsmtExposure'].map(bsm_exp_map)
mapping_dict = {
'ind_empleado' : {-99:0, 'N':1, 'B':2, 'F':3, 'A':4, 'S':5},
'sexo' : {'V':0, 'H':1, -99:2},
'ind_nuevo' : {'0':0, '1':1, -99:2},
'indrel' : {'1':0, '99':1, -99:2},
'indrel_1mes' : {-99:0, '1.0':1, '1':1, '2.0':2, '2':2, '3.0':3, '3':3, '4.0':4, '4':4, 'P':5},
'tiprel_1mes' : {-99:0, 'I':1, 'A':2, 'P':3, 'R':4, 'N':5},
'indresi' : {-99:0, 'S':1, 'N':2},
'indext' : {-99:0, 'S':1, 'N':2},
'conyuemp' : {-99:0, 'S':1, 'N':2},
'indfall' : {-99:0, 'S':1, 'N':2},
'tipodom' : {-99:0, '1':1},
'ind_actividad_cliente' : {'0':0, '1':1, -99:2},
'segmento' : {'02 - PARTICULARES':0, '03 - UNIVERSITARIO':1, '01 - TOP':2, -99:2},
'pais_residencia' : {'LV': 102, 'BE': 12, 'BG': 50, 'BA': 61, 'BM': 117, 'BO': 62, 'JP': 82, 'JM': 116, 'BR': 17, 'BY': 64, 'BZ': 113, 'RU': 43, 'RS': 89, 'RO': 41, 'GW': 99, 'GT': 44, 'GR': 39, 'GQ': 73, 'GE': 78, 'GB': 9, 'GA': 45, 'GN': 98, 'GM': 110, 'GI': 96, 'GH': 88, 'OM': 100, 'HR': 67, 'HU': 106, 'HK': 34, 'HN': 22, 'AD': 35, 'PR': 40, 'PT': 26, 'PY': 51, 'PA': 60, 'PE': 20, 'PK': 84, 'PH': 91, 'PL': 30, 'EE': 52, 'EG': 74, 'ZA': 75, 'EC': 19, 'AL': 25, 'VN': 90, 'ET': 54, 'ZW': 114, 'ES': 0, 'MD': 68, 'UY': 77, 'MM': 94, 'ML': 104, 'US': 15, 'MT': 118, 'MR': 48, 'UA': 49, 'MX': 16, 'IL': 42, 'FR': 8, 'MA': 38, 'FI': 23, 'NI': 33, 'NL': 7, 'NO': 46, 'NG': 83, 'NZ': 93, 'CI': 57, 'CH': 3, 'CO': 21, 'CN': 28, 'CM': 55, 'CL': 4, 'CA': 2, 'CG': 101, 'CF': 109, 'CD': 112, 'CZ': 36, 'CR': 32, 'CU': 72, 'KE': 65, 'KH': 95, 'SV': 53, 'SK': 69, 'KR': 87, 'KW': 92, 'SN': 47, 'SL': 97, 'KZ': 111, 'SA': 56, 'SG': 66, 'SE': 24, 'DO': 11, 'DJ': 115, 'DK': 76, 'DE': 10, 'DZ': 80, 'MK': 105, -99: 1, 'LB': 81, 'TW': 29, 'TR': 70, 'TN': 85, 'LT': 103, 'LU': 59, 'TH': 79, 'TG': 86, 'LY': 108, 'AE': 37, 'VE': 14, 'IS': 107, 'IT': 18, 'AO': 71, 'AR': 13, 'AU': 63, 'AT': 6, 'IN': 31, 'IE': 5, 'QA': 58, 'MZ': 27},
'canal_entrada' : {'013': 49, 'KHP': 160, 'KHQ': 157, 'KHR': 161, 'KHS': 162, 'KHK': 10, 'KHL': 0, 'KHM': 12, 'KHN': 21, 'KHO': 13, 'KHA': 22, 'KHC': 9, 'KHD': 2, 'KHE': 1, 'KHF': 19, '025': 159, 'KAC': 57, 'KAB': 28, 'KAA': 39, 'KAG': 26, 'KAF': 23, 'KAE': 30, 'KAD': 16, 'KAK': 51, 'KAJ': 41, 'KAI': 35, 'KAH': 31, 'KAO': 94, 'KAN': 110, 'KAM': 107, 'KAL': 74, 'KAS': 70, 'KAR': 32, 'KAQ': 37, 'KAP': 46, 'KAW': 76, 'KAV': 139, 'KAU': 142, 'KAT': 5, 'KAZ': 7, 'KAY': 54, 'KBJ': 133, 'KBH': 90, 'KBN': 122, 'KBO': 64, 'KBL': 88, 'KBM': 135, 'KBB': 131, 'KBF': 102, 'KBG': 17, 'KBD': 109, 'KBE': 119, 'KBZ': 67, 'KBX': 116, 'KBY': 111, 'KBR': 101, 'KBS': 118, 'KBP': 121, 'KBQ': 62, 'KBV': 100, 'KBW': 114, 'KBU': 55, 'KCE': 86, 'KCD': 85, 'KCG': 59, 'KCF': 105, 'KCA': 73, 'KCC': 29, 'KCB': 78, 'KCM': 82, 'KCL': 53, 'KCO': 104, 'KCN': 81, 'KCI': 65, 'KCH': 84, 'KCK': 52, 'KCJ': 156, 'KCU': 115, 'KCT': 112, 'KCV': 106, 'KCQ': 154, 'KCP': 129, 'KCS': 77, 'KCR': 153, 'KCX': 120, 'RED': 8, 'KDL': 158, 'KDM': 130, 'KDN': 151, 'KDO': 60, 'KDH': 14, 'KDI': 150, 'KDD': 113, 'KDE': 47, 'KDF': 127, 'KDG': 126, 'KDA': 63, 'KDB': 117, 'KDC': 75, 'KDX': 69, 'KDY': 61, 'KDZ': 99, 'KDT': 58, 'KDU': 79, 'KDV': 91, 'KDW': 132, 'KDP': 103, 'KDQ': 80, 'KDR': 56, 'KDS': 124, 'K00': 50, 'KEO': 96, 'KEN': 137, 'KEM': 155, 'KEL': 125, 'KEK': 145, 'KEJ': 95, 'KEI': 97, 'KEH': 15, 'KEG': 136, 'KEF': 128, 'KEE': 152, 'KED': 143, 'KEC': 66, 'KEB': 123, 'KEA': 89, 'KEZ': 108, 'KEY': 93, 'KEW': 98, 'KEV': 87, 'KEU': 72, 'KES': 68, 'KEQ': 138, -99: 6, 'KFV': 48, 'KFT': 92, 'KFU': 36, 'KFR': 144, 'KFS': 38, 'KFP': 40, 'KFF': 45, 'KFG': 27, 'KFD': 25, 'KFE': 148, 'KFB': 146, 'KFC': 4, 'KFA': 3, 'KFN': 42, 'KFL': 34, 'KFM': 141, 'KFJ': 33, 'KFK': 20, 'KFH': 140, 'KFI': 134, '007': 71, '004': 83, 'KGU': 149, 'KGW': 147, 'KGV': 43, 'KGY': 44, 'KGX': 24, 'KGC': 18, 'KGN': 11}
}
cat_cols = list(mapping_dict.keys())
 target_cols = ['ind_ahor_fin_ult1','ind_aval_fin_ult1','ind_cco_fin_ult1','ind_cder_fin_ult1','ind_cno_fin_ult1','ind_ctju_fin_ult1','ind_ctma_fin_ult1','ind_ctop_fin_ult1','ind_ctpp_fin_ult1','ind_deco_fin_ult1','ind_deme_fin_ult1','ind_dela_fin_ult1','ind_ecue_fin_ult1','ind_fond_fin_ult1','ind_hip_fin_ult1','ind_plan_fin_ult1','ind_pres_fin_ult1','ind_reca_fin_ult1','ind_tjcr_fin_ult1','ind_valo_fin_ult1','ind_viv_fin_ult1','ind_nomina_ult1','ind_nom_pens_ult1','ind_recibo_ult1']
target_cols = target_cols[2:]
def getTarget(row):
	tlist = []
	for col in target_cols:
		if row[col].strip() in ['', 'NA']:
			target = 0
		else:
			target = int(float(row[col]))
		tlist.append(target)
 	return tlist
def getIndex(row, col):
	val = row[col].strip()
	if val not in ['','NA']:
		ind = mapping_dict[col][val]
	else:
		ind = mapping_dict[col][-99]
	return ind
def getAge(row):
	mean_age = 40.
 	min_age = 20.
 	max_age = 90.
 	range_age = max_age - min_age
	age = row['age'].strip()
	if age == 'NA' or age == '':
		age = mean_age
	else:
		age = float(age)
 		if age < min_age:
			age = min_age
		elif age > max_age:
			age = max_age
	return round(( age - min_age)/ range_age, 4)
 def getCustSeniority(row):
	min_value = 0.
 	max_value = 256.
 	range_value = max_value - min_value
	missing_value = 0.
 	cust_seniority = row['antiguedad'].strip()
	if cust_seniority == 'NA' or cust_seniority == '':
		cust_seniority = missing_value
	else:
		cust_seniority = float(cust_seniority)
 		if cust_seniority < min_value:
			cust_seniority = min_value
		elif cust_seniority > max_value:
			cust_seniority = max_value
	return round(( cust_seniority-min_value)/ range_value, 4)
 def getRent(row):
	min_value = 0.
 	max_value = 1500000.
 	range_value = max_value - min_value
	missing_value = 101850.
 	rent = row['renta'].strip()
	if rent == 'NA' or rent == '':
		rent = missing_value
	else:
		rent = float(rent)
 		if rent < min_value:
			rent = min_value
		elif rent > max_value:
			rent = max_value
	return round(( rent-min_value)/ range_value, 6)
 def processData(in_file_name, cust_dict):
	x_vars_list = []
	y_vars_list = []
	for row in csv.DictReader(in_file_name):
		
		if row['fecha_dato'] not in ['2015-05-28', '2015-06-28', '2016-05-28', '2016-06-28']:
			continue
		cust_id = int(row['ncodpers'])
 		if row['fecha_dato'] in ['2015-05-28', '2016-05-28']:	
			target_list = getTarget(row)
 			cust_dict[cust_id] = target_list[:]
			continue
		x_vars = []
		for col in cat_cols:
			x_vars.append(getIndex(row, col))
		x_vars.append(getAge(row))
		x_vars.append(getCustSeniority(row))
		x_vars.append(getRent(row))
		if row['fecha_dato'] == '2016-06-28':
			prev_target_list = cust_dict.get(cust_id, [0]*22)
 			x_vars_list.append(x_vars + prev_target_list)
 		elif row['fecha_dato'] == '2015-06-28':
			prev_target_list = cust_dict.get(cust_id, [0]*22)
 			target_list = getTarget(row)
 			new_products = [max(x1 - x2,0)for(x1, x2)in zip(target_list, prev_target_list)]
			if sum(new_products)> 0:
				for ind, prod in enumerate(new_products):
					if prod>0:
						assert len(prev_target_list)== 22
						x_vars_list.append(x_vars+prev_target_list)
 						y_vars_list.append(ind)
 	return x_vars_list, y_vars_list, cust_dict
			
def runXGB(train_X, train_y, seed_val=0):
	param = {}
	param['objective'] = 'multi:softprob'
	param['eta'] = 0.05
	param['max_depth'] = 8
	param['silent'] = 1
	param['num_class'] = 22
	param['eval_metric'] = "mlogloss"
	param['min_child_weight'] = 1
	param['subsample'] = 0.7
	param['colsample_bytree'] = 0.7
	param['seed'] = seed_val
	num_rounds = 50
	plst = list(param.items())
 	xgtrain = xgb.DMatrix(train_X, label=train_y)
 	model = xgb.train(plst, xgtrain, num_rounds)	
	return model
if __name__ == "__main__":
	start_time = datetime.datetime.now()
	data_path = ".. /input/"
	train_file = open(data_path + "train_ver2.csv")
 	x_vars_list, y_vars_list, cust_dict = processData(train_file, {})
 	train_X = np.array(x_vars_list)
 	train_y = np.array(y_vars_list)
 	print(np.unique(train_y))
	del x_vars_list, y_vars_list
	train_file.close()
	print(train_X.shape, train_y.shape)
 	print(datetime.datetime.now() -start_time)
 	test_file = open(data_path + "test_ver2.csv")
 	x_vars_list, y_vars_list, cust_dict = processData(test_file, cust_dict)
 	test_X = np.array(x_vars_list)
 	del x_vars_list
	test_file.close()
	print(test_X.shape)
 	print(datetime.datetime.now() -start_time)
 	print("Building model.. ")
 	model = runXGB(train_X, train_y, seed_val=0)
 	del train_X, train_y
	print("Predicting.. ")
 	xgtest = xgb.DMatrix(test_X)
 	preds = model.predict(xgtest)
 	del test_X, xgtest
	print(datetime.datetime.now() -start_time)
 	print("Getting the top products.. ")
 	target_cols = np.array(target_cols)
 	preds = np.argsort(preds, axis=1)
 	preds = np.fliplr(preds)[:,:7]
	test_id = np.array(pd.read_csv(data_path + "test_ver2.csv", usecols=['ncodpers'])['ncodpers'])
 	final_preds = [" ".join(list(target_cols[pred])) for pred in preds]
	out_df = pd.DataFrame({'ncodpers':test_id, 'added_products':final_preds})
 	out_df.to_csv('sub_xgb_new.csv', index=False)
 	print(datetime.datetime.now() -start_time )<save_to_csv>
all_data_rep_enc_1.isnull().sum().sort_values(ascending=False)[:3]
out_df.to_csv('sub_xgb_new.csv', index=False)
 print("Success" )<import_modules>
train["SalePrice"] = np.log1p(train["SalePrice"])
 check_skewness('SalePrice' )
import tensorflow as tf<import_modules>
y_train=train["SalePrice"]
from keras import backend as K
from keras.optimizers import Adam, SGD, Adagrad, Adadelta
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, LearningRateScheduler, CSVLogger
from keras.models import Model, Sequential, load_model, model_from_json
from keras.layers import Flatten, Dense, Activation, Input, Dropout, Activation, BatchNormalization, Reshape
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D<import_modules>
all_data_rep_enc_1.dtypes[all_data_rep_enc_1.dtypes=='object'].index
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MultiLabelBinarizer<import_modules>
skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.5
for feat in skewed_features:
all_data_rep_enc_1[feat] = boxcox1p(all_data_rep_enc_1[feat], lam )
import random
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import cv2<define_variables>
all_data_rep_enc_1=all_data_rep_enc_1.drop(['Utilities'],axis=1 )
data_path = '.. /input'
train_path = os.path.join(data_path, 'train')
 test_path = os.path.join(data_path,'test')
 labels_path = os.path.join(data_path, 'train.csv' )<load_from_csv>
cat_feat=all_data_rep_enc_1.dtypes[all_data_rep_enc_1.dtypes== "object"].index
cat_feat
def pick_label(x):
t = x
if '27' in t:
return '27'
elif '15' in t:
return '15'
elif '10' in t:
return '10'
elif '9' in t:
return '9'
elif '8' in t:
return '8'
elif '0' in t:
return '0'
elif '25' in t:
return '25'
else:
return t[0]
train_labels = pd.read_csv(labels_path,index_col=False)
 labels_dict = dict(zip(train_labels.values[ : ,0], train_labels.values[ : , 1]))
t = {k:pick_label(v.split())for(k, v)in labels_dict.items() }
train_labels['t'] = t.values()
t_arr = np.array(list(t.items())) 
<split>
from sklearn import metrics
from sklearn.metrics import mean_squared_error
from sklearn import feature_selection
from catboost import CatBoostRegressor
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
train_ids, val_ids = train_test_split(train_labels, stratify=t_arr[ :, 1],
test_size=0.1, random_state=48 )<split>
X_train_, X_valid, y_train_, y_valid = train_test_split(
 X_train, y_train, test_size=0.33, random_state=241 )
labels = [item.split() for item in train_labels['Target']]
mlb = MultiLabelBinarizer()
mlb.fit(labels)
 classes = mlb.classes_
y_val = mlb.transform([item.split() for item in val_ids['Target']] )<choose_model_class>
cb_model = CatBoostRegressor(iterations=3500,learning_rate=0.03,od_type='Iter',od_wait=1500,
depth=6,random_strength=1,
l2_leaf_reg=10,
sampling_frequency='PerTree', 
)
 zzz=cb_model.fit(X_train_, y_train_,
eval_set=(X_valid,y_valid),
cat_features=cat_feat,use_best_model=True,
verbose=True )
def model(sample_shape):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape=sample_shape, name='conv1'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32, kernel_size=(3, 3), name='conv2'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32, kernel_size=(3, 3), name='conv2b'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), name='pool1'))
model.add(Conv2D(64, kernel_size=(3, 3), name='conv3'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64, kernel_size=(3, 3), name='conv4'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), name='pool2'))
model.add(Conv2D(128, kernel_size=(3, 3), name='conv5'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128, kernel_size=(3, 3), name='conv6'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), name='pool3'))
model.add(Conv2D(256, kernel_size=(3, 3), name='conv7'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(256, kernel_size=(3, 3), name='conv8'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(GlobalAveragePooling2D())
 model.add(Dense(4096, name='fc1'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dense(28))
model.add(Activation('sigmoid'))
return model 
<define_variables>
model = cb_model
grid_par=[{'iterations':list(range(3138,3139)) }]
cv=KFold(n_splits=5,shuffle=True,random_state=241)
 gs = GridSearchCV(model,scoring='neg_mean_squared_error',cv=cv,param_grid=grid_par )
DEPTH = 3
BATCH_SIZE = 32
IMG_SIZE = 256
SAMPLE_SHAPE =(IMG_SIZE, IMG_SIZE, DEPTH)
 SEED = 1234
random.seed(SEED )<train_model>
gs.fit(X_train, y_train,cat_features=cat_feat,use_best_model=True,verbose=True )
K.clear_session()
model = model(SAMPLE_SHAPE )<define_variables>
gs.cv_results_
EPOCHS = 50
val_steps = int(np.ceil(len(val_ids)/BATCH_SIZE))
num_steps = int(np.ceil(len(train_ids)/BATCH_SIZE))
print('train_size: ', len(train_ids), 'batch: ', BATCH_SIZE, 'num steps: ', num_steps )<categorify>
model_out= CatBoostRegressor(iterations=3138,learning_rate=0.03,
depth=6,random_strength=1,
l2_leaf_reg=10,
sampling_frequency='PerTree')
 CAT=model_out.fit(X_train, y_train,
cat_features=cat_feat,
verbose=True)
 test = x_test.copy()
cat_pred=CAT.predict(test )
def f1(y_true, y_pred, thresholds= np.array(28*[0.5])) :
m, n = y_true.shape
p = tf.cast(tf.greater(y_pred, thresholds), tf.float32)
 tp = tf.reduce_sum(y_true * p, 0)
 num_pos = tf.reduce_sum(tf.cast(y_true, tf.float32), 0)
 pred_pos = tf.reduce_sum(p, 0)
 precision = tp/(pred_pos + K.epsilon())
 recall = tp /(num_pos + K.epsilon())
 f1 = tf.reduce_mean(tf.divide(2*precision*recall,(precision + recall + K.epsilon())))
 K.get_session().run(tf.local_variables_initializer())
 return f1 
<choose_model_class>
finalMd = np.expm1(cat_pred)
 finalMd
<train_model><EOS> 
sample_submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv")
 sample_submission.iloc[:,1] = finalMd
sample_submission.to_csv("sample_submission.csv", index=False )