Kaggle competition: NCTU BDALAB 2020 Onboard
def feature_engineering(is_train=True):
if is_train:
print("processing train_V2.csv")
 df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test_V2.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
df['rankPoints'] = np.where(df['rankPoints'] <= 0 ,0 , df['rankPoints'])
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y<normalization>
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, roc_auc_score
from sklearn.tree import DecisionTreeClassifier
%%time
X, y = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(X )<train_model>
X_train = pd.read_csv(".. /input/nctu-bdalab-2020-onboard/data_train.csv", index_col="index")
 Y_train = pd.read_csv(".. /input/nctu-bdalab-2020-onboard/answer_train.csv", index_col="index")
 X_train, X_test, Y_train, Y_test = train_test_split(X_train, Y_train, test_size=0.2 )
print("X", X.shape, X.max() , X.min())
 scaler.transform(X)
 print("X", X.shape, X.max() , X.min() )<choose_model_class>
model = DecisionTreeClassifier()
model.fit(X_train, Y_train )
model = Sequential()
model.add(Dense(X.shape[1], input_dim=X.shape[1], activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dense(128, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(BatchNormalization())
 model.add(Dense(1, activation='linear'))
model.summary()<choose_model_class>
train_pred = model.predict(X_train)
 print(classification_report(Y_train, train_pred))
print(roc_auc_score(Y_train, train_pred))
optimizer = optimizers.Adam(lr=0.01, epsilon=1e-8, decay=1e-4, amsgrad=False)
 model.compile(optimizer=optimizer, loss='mse', metrics=['mae'])
 checkpoint_name = 'Weights-{epoch:03d}--{val_loss:.5f}.hdf5'
checkpoint = ModelCheckpoint(checkpoint_name, monitor='val_loss', verbose = 1, save_best_only = True, mode ='auto')
 callbacks_list = [checkpoint]<train_model>
test_pred = model.predict(X_test)
 print(classification_report(Y_test, test_pred))
print(roc_auc_score(Y_test, test_pred))
<normalization><EOS> 
X_submit = pd.read_csv(".. /input/nctu-bdalab-2020-onboard/data_test.csv", index_col="index")
 pred = model.predict(X_submit)
 pred_df = pd.DataFrame(data={'default.payment.next.month': pred} ).reset_index()
pred_df.to_csv("./pred.csv", index=False )