Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<save_to_csv>
sns.set()
sns.set_style('dark')
model = lgb.LGBMClassifier()
model.fit(X_selected,Y)
 p = model.predict(test_selected)
 sample = pd.read_csv('/kaggle/input/1056lab-brain-cancer-classification/sampleSubmission.csv',index_col = 0)
 sample['type'] = p
sample.to_csv('predict_lgbm_100.csv',header = True )<load_from_csv>
IS_LOCAL = False
USE_REDUCED = False
data_index = 2*int(IS_LOCAL)+ int(USE_REDUCED)
 train_path =('.. /input/santander-customer-transaction-prediction/train.csv',
'.. /input/santandersmall/train_small.csv',
'train.csv',
'train_small.csv')[data_index]
test_path =('.. /input/santander-customer-transaction-prediction/test.csv',
'.. /input/santandersmall/test_small_with_targets.csv',
'test.csv',
'test_small.csv')[data_index]
train_df = pd.read_csv(train_path)
 test_df = pd.read_csv(test_path )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
<prepare_x_and_y>
features = [col for col in train_df.columns if col not in ['target', 'ID_code']]
if not 'target' in test_df:
test_df['target'] = -1
all_df = pd.concat([train_df, test_df], sort=False )
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.SalePrice.values
all_data = pd.concat(( train, test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<create_dataframe>
unique_count = np.zeros(( test_df.shape[0], len(features)))
 for f, feature in tqdm(enumerate(features), total=len(features)) :
_, i, c = np.unique(test_df[feature], return_counts=True, return_index=True)
 unique_count[i[c == 1], f] += 1
real_sample_indices = np.argwhere(np.sum(unique_count, axis=1)> 0)[:, 0]
synthetic_sample_indices = np.argwhere(np.sum(unique_count, axis=1)== 0)[:, 0]
print('Real:', len(real_sample_indices))
print('Synthetic:', len(synthetic_sample_indices))
del unique_count
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})
 missing_data.head(20 )<data_type_conversions>
all_real_df = pd.concat([train_df, test_df.iloc[real_sample_indices, :]], sort=False)
 for feature in tqdm(features):
real_series = all_real_df[feature]
counts = real_series.groupby(real_series ).count()
full_series = all_df[feature]
all_df[f'{feature}_count'] = full_series.map(counts)
 del all_real_df
del real_series
del full_series
all_data["PoolQC"] = all_data["PoolQC"].fillna("None" )<data_type_conversions>
for feature in tqdm(features):
all_df[feature] = StandardScaler().fit_transform(all_df[feature].values.reshape(-1, 1))
all_df[f'{feature}_count'] = MinMaxScaler().fit_transform(all_df[f'{feature}_count'].values.reshape(-1, 1))
all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None" )<data_type_conversions>
for f in range(len(features)) :
features.append(f'{features[f]}_count' )
all_data["Alley"] = all_data["Alley"].fillna("None" )<data_type_conversions>
train_df = all_df.iloc[:train_df.shape[0], :]
test_df = all_df.iloc[train_df.shape[0]:, :]
del all_df
all_data["Fence"] = all_data["Fence"].fillna("None" )<data_type_conversions>
N_SPLITS = 5
BATCH_SIZE = 256
EPOCHS = 100
EARLY_STOPPING_PATIENCE = 15
OPTIMIZER = tf.keras.optimizers.Nadam()
LOSS='binary_crossentropy'
METRICS=[tf.keras.metrics.AUC() ]
all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None" )<groupby>
def get_cnn_model_1() :
model = tf.keras.models.Sequential([
tf.keras.layers.Reshape(( len(features)* 1, 1), input_shape=(len(features)* 1,)) ,
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(1, activation='sigmoid'),
])
 return model
def get_cnn_model() :
model = tf.keras.models.Sequential([
tf.keras.layers.Reshape(( len(features)* 1, 1), input_shape=(len(features)* 1,)) ,
tf.keras.layers.Conv1D(32, 1, activation='elu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv1D(64, 1, activation='elu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(1024, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(512, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.005)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(1, activation='sigmoid'),
])
 return model
def get_nn_model_2() :
model = tf.keras.models.Sequential([
tf.keras.layers.Reshape(( len(features)* 1, 1), input_shape=(len(features)* 1,)) ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.02)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(16, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(1, activation='sigmoid'),
])
 return model
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))<data_type_conversions>
kfold = StratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=42)
 models = []
histories = []
for fold_num,(train_index, val_index)in tqdm(enumerate(kfold.split(train_df[features].values, train_df['target'].values)) , total=N_SPLITS):
print(f'Fold {fold_num+1}/{N_SPLITS}:')
 X_train = train_df.loc[train_index, features].values
y_train = train_df.loc[train_index, 'target'].values.reshape(-1, 1)
 X_val = train_df.loc[val_index, features].values
y_val = train_df.loc[val_index, 'target'].values.reshape(-1, 1)
 model = get_cnn_model_1()
model.compile(optimizer=OPTIMIZER, loss=LOSS, metrics=METRICS)
 early_stopping_callback = tf.keras.callbacks.EarlyStopping(monitor='val_auc', mode='max', patience=EARLY_STOPPING_PATIENCE, restore_best_weights=True)
 history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=EPOCHS, batch_size=BATCH_SIZE, callbacks=[early_stopping_callback])
 histories.append(history)
 val_preds = model.predict(X_val)
 val_auc = roc_auc_score(y_val, val_preds)
 print(f'Fold validation AUC: {val_auc}')
 print()
models.append(model )
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_data[col] = all_data[col].fillna('None' )<data_type_conversions>
train_preds = np.zeros(train_df.shape)
 test_preds = np.zeros(test_df.shape)
 for model in models:
pred_train = model.predict(train_df[features].values)
 pred_test = model.predict(test_df[features].values)
 train_preds += pred_train
test_preds += pred_test
train_preds /= len(models)
 test_preds /= len(models )
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
train_preds = train_preds[:, 0]
test_preds = test_preds[:, 0]
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
train_auc = roc_auc_score(train_df['target'], train_preds)
 print(f'Train AUC: {train_auc}' )
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None' )<data_type_conversions>
test_df = pd.read_csv('test_small_with_targets.csv' )
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )<data_type_conversions>
if test_df['target'][0] != -1:
test_auc = roc_auc_score(test_df['target'], test_preds)
 print(f'Test AUC: {test_auc}' )
all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0] )<drop_column>
sub = pd.DataFrame({'ID_code': test_df['ID_code'], 'target': test_preds})
 sub.to_csv('submission.csv', index=False )
all_data = all_data.drop(['Utilities'], axis=1 )<data_type_conversions>
FileLink('submission.csv' )