Kaggle competition: Santander Customer Transaction Prediction
categorical=["year","month"]<merge>
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )
train=train.join(month_df,on=["date_block_num","shop_id","item_id"],how="left")
 train["item_cnt_day"]=train["item_cnt_day"].fillna(0 ).clip(0,20 ).astype(np.float16)
 train.head()<concatenate>
train["target"].value_counts()
train=pd.concat([train,test_df],ignore_index=True,sort=False,keys=col)
 train.fillna(0,inplace=True)
 train.head()<merge>
count_class_0, count_class_1 = train.target.value_counts()
df_class_0 = train[train['target'] == 0]
df_class_1 = train[train['target'] == 1]
def create_lag_features(df,feature):
numeric_features=[]
for i in range(1,7):
lagged=df.copy()
lagged=lagged[["date_block_num","shop_id","item_id",feature]]
lagged.columns=["date_block_num","shop_id","item_id",feature+"_lag_"+str(i)]
numeric_features.append(feature+"_lag_"+str(i))
lagged["date_block_num"]+=i
df=df.join(lagged.set_index(["date_block_num","shop_id","item_id"]),on=["date_block_num","shop_id","item_id"],how="left")
 return df,numeric_features<feature_engineering>
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
%%time
train,n_col=create_lag_features(train,"item_cnt_day")
 train=train.fillna(0)
 numeric=n_col<groupby>
def _Model1() :
inp = Input(shape=(200, 1))
d1 = Dense(128, activation='sigmoid' )(inp)
 d2 = Dense(64, activation='relu' )(d1)
 d3 = Dense(32, activation='sigmoid' )(d2)
 d4 = Dense(16, activation='relu' )(d3)
 f2 = Flatten()(d4)
 preds = Dense(1, activation='sigmoid' )(f2)
 model = Model(inputs=inp, outputs=preds)
 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy',recall])
 return model
total_cnt_for_items=sales_train_df[["date_block_num","item_id","item_cnt_day"]].groupby(["date_block_num","item_id"] ).sum()
total_cnt_for_items.columns=["total_cnt_for_items"]
total_cnt_for_items.head()<merge>
def _Model2() :
inp = Input(shape=(200, 1))
d1 = Dense(64, activation='sigmoid' )(inp)
 d2 = Dense(128, activation='relu' )(d1)
 d3 = Dense(32, activation='sigmoid' )(d2)
 d4 = Dense(16, activation='relu' )(d3)
 f2 = Flatten()(d4)
 preds = Dense(1, activation='sigmoid' )(f2)
 model = Model(inputs=inp, outputs=preds)
 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy',recall])
 return model
train=train.join(total_cnt_for_items,on=["date_block_num","item_id"],how="left")
 train.fillna(0,inplace=True)
 train["total_cnt_for_items"]=train["total_cnt_for_items"].astype(np.float16)
 train.head().T<drop_column>
def _Model4() :
inp = Input(shape=(200, 1))
d1 = Dense(128, activation='sigmoid' )(inp)
 d2 = Conv1D(64, 2,activation='relu' )(d1)
 d3 = Dense(32, activation='sigmoid' )(d2)
 d4 = Dense(16, activation='relu' )(d3)
 f2 = Flatten()(d4)
 preds = Dense(1, activation='sigmoid' )(f2)
 model = Model(inputs=inp, outputs=preds)
 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy',recall])
 return model
%%time
train,n_col=create_lag_features(train,"total_cnt_for_items")
 train=train.fillna(0)
 train=train.drop(["total_cnt_for_items"],axis=1)
 numeric=numeric+n_col<groupby>
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=7)
 df_train=train.drop(["ID_code","target"],axis=1)
 y_train=train["target"]
df_test=test.drop(["ID_code"],axis=1)
 scaler=StandardScaler()
df_train=scaler.fit_transform(df_train)
 df_test=scaler.fit_transform(df_test)
total_cnt_for_shops=sales_train_df[["date_block_num","shop_id","item_cnt_day"]].groupby(["date_block_num","shop_id"] ).sum()
total_cnt_for_shops.columns=["total_cnt_for_shops"]
total_cnt_for_shops.head()<merge>
df_train= pd.DataFrame(df_train)
 df_test= pd.DataFrame(df_test)
sum_cnt_shops=total_cnt_for_shops.reset_index()
cnt_shops=sum_cnt_shops[["shop_id","total_cnt_for_shops"]].groupby("shop_id",as_index=False ).mean()
cnt_shops=cnt_shops.join(shops_df.set_index(["shop_id"]),on="shop_id",how="left")
 cnt_shops.head()<feature_engineering>
preds = []
c = 0
oof_preds = np.zeros(( len(df_train), 1))
for train, valid in cv.split(df_train, y_train):
print("VAL %s" % c)
 X_train = np.reshape(df_train.iloc[train].values,(-1, 200, 1))
y_train_ = y_train.iloc[train].values
X_valid = np.reshape(df_train.iloc[valid].values,(-1, 200, 1))
y_valid = y_train.iloc[valid].values
model = _Model2()
history=model.fit(X_train, y_train_, validation_data=(X_valid, y_valid), epochs=20, verbose=2, batch_size=256)
 X_test = np.reshape(df_test.values,(200000, 200, 1))
curr_preds = model.predict(X_test, batch_size=2048)
 oof_preds[valid] = model.predict(X_valid)
 preds.append(curr_preds)
 c += 1
auc = roc_auc_score(y_train, oof_preds)
 print("CV_AUC: {}".format(auc))
cnt_shops.loc[cnt_shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
cnt_shops['shop_city'] = cnt_shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 cnt_shops.loc[cnt_shops.shop_city == '!Якутск', 'shop_city'] = 'Якутск'
cnt_shops['shop_type'] = cnt_shops['shop_name'].str.split(' ' ).map(lambda x: x[1])
 cnt_shops.head() 
<merge>
prediction=(preds[0]+preds[1]+preds[2]+preds[3]+preds[4])/5
train=train.join(cnt_shops[["shop_id","shop_city","shop_type"]].set_index(["shop_id"]),on="shop_id",how="left")
 train.fillna(0,inplace=True)
 train["shop_city"]=train["shop_city"].astype(str)
 train["shop_type"]=train["shop_type"].astype(str)
 train.head().T<define_variables>
sub = pd.DataFrame()
sub["ID_code"] = test["ID_code"]
sub["target"] = prediction
sub.to_csv("submission-cnn-ksplit.csv", index=False )
categorical=categorical+["shop_city","shop_type"]<merge>
sub.to_csv("submission-cnn-ksplit.csv", index=False )
train=train.join(total_cnt_for_shops,on=["date_block_num","shop_id"],how="left")
 train.fillna(0,inplace=True)
 train["total_cnt_for_shops"]=train["total_cnt_for_shops"].astype(np.float16)
 train.head().T<drop_column>
preds = []
c = 0
oof_preds = np.zeros(( len(df_train), 1))
df_under_x=df_test_under.drop(["ID_code","target"],axis=1)
 df_under_y=df_test_under["target"]
for train, valid in cv.split(df_under_x,df_under_y):
print("VAL %s" % c)
 X_train = np.reshape(df_under_x.iloc[train].values,(-1, 200, 1))
y_train_ = df_under_y.iloc[train].values
X_valid = np.reshape(df_under_x.iloc[valid].values,(-1, 200, 1))
y_valid = df_under_y.iloc[valid].values
model = _Model1()
history=model.fit(X_train, y_train_, validation_data=(X_valid, y_valid), epochs=20, verbose=2, batch_size=256)
 X_test = np.reshape(df_test.values,(200000, 200, 1))
curr_preds = model.predict(X_test, batch_size=2048)
 oof_preds[valid] = model.predict(X_valid)
 preds.append(curr_preds)
 c += 1
auc = roc_auc_score(y_train, oof_preds)
 print("CV_AUC: {}".format(auc))
%%time
train,n_col=create_lag_features(train,"total_cnt_for_shops")
 train=train.fillna(0)
 train=train.drop(["total_cnt_for_shops"],axis=1)
 numeric=numeric+n_col<groupby>
x_train, x_test, y_train, y_test = train_test_split(df_test_under.drop(["ID_code","target"],axis=1),
df_test_under["target"], test_size=0.3, random_state=42)
total_cnt_for_item_categories=sales_train_df[["date_block_num","item_category_id","item_cnt_day"]].groupby(["date_block_num","item_category_id"] ).sum()
total_cnt_for_item_categories.columns=["total_cnt_for_item_categories"]
total_cnt_for_item_categories.head()<feature_engineering>
X_train = np.reshape(x_train.values,(-1, 200, 1))
X_valid = np.reshape(x_test.values,(-1, 200, 1))
y_valid =y_test
model = _Model1()
history1 = model.fit(X_train,
y_train,
epochs=40,
verbose=2,batch_size=256,
validation_data=(X_valid, y_valid))
sum_cnt_for_item_category["category"]=sum_cnt_for_item_category.item_category_name.str.split("-" ).str[0]
sum_cnt_for_item_category["subcategory"]=sum_cnt_for_item_category.item_category_name.str.split("-" ).str[1]
sum_cnt_for_item_category.head()<data_type_conversions>
model = _Model2()
history2 = model.fit(X_train,
y_train,
epochs=50,
verbose=2,batch_size=256,
validation_data=(X_valid, y_valid))
<define_variables><EOS> 
model = _Model3()
history4 = model.fit(X_train,
y_train,
epochs=20,
verbose=2,batch_size=256,
validation_data=(X_valid, y_valid))