Kaggle competition: Digit Recognizer
print("Dimension :", application_train.dropna(axis=0 ).shape)
 print("결측치가 있는 컬럼 수 :",(application_train.dropna(axis=0 ).isnull().sum() !=0 ).sum())
 application_train.dropna(axis=0 )<train_model>
data_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 data_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
column_list = []
for name in column_series.keys() :
if(column_series[name]>100000):
column_list.append(name)
 print(column_list, len(column_list))<train_model>
print('Number of non-valid elements in training set:', data_train[data_train.isna() == True].count().sum() ,
'
Number of non valid elements in test set:', data_test[data_test.isna() == True].count().sum() )
def show_hist_by_target(df, columns):
cond_1 =(df['TARGET'] == 1)
 cond_0 =(df['TARGET'] == 0)
 for column in columns:
fig, ax = plt.subplots(figsize=(12, 4), nrows=1, ncols=2, squeeze=False)
 if(type(df[column][0])is str):
df_temp = df[["TARGET",column]].value_counts().astype(float)
 idx_temp = df_temp.reset_index(name='RATIO')[column].unique()
for i in range(0,2):
sum_temp = df_temp[i].sum()
for j in idx_temp:
df_temp[(i,j)] = df_temp[(i,j)] / sum_temp
df_temp = df_temp.reset_index(name='RATIO')
 sns.barplot(x="TARGET", y="RATIO", hue=column, data=df_temp,ax=ax[0][0])
 sns.lineplot(x=df[cond_1][column].value_counts().keys().tolist() , y=df[cond_1][column].value_counts() , label = 'target=1', color='red', ax=ax[0][1])
 sns.lineplot(x=df[cond_0][column].value_counts().keys().tolist() , y=df[cond_0][column].value_counts() , label = 'target=0', color='blue', ax=ax[0][1])
 else:
sns.violinplot(x='TARGET', y=column, data=df, ax=ax[0][0])
 sns.histplot(df[cond_1][column], label='target=1', color='red', ax=ax[0][1], kde=True)
 sns.histplot(df[cond_0][column], label='target=0', color='blue', ax=ax[0][1], kde=True)
 plt.show()
plt.close()<sort_values>
data_train_pd = data_train.copy()
abs(cor["TARGET"] ).sort_values()<count_values>
true_labels = data_train.label
data_train = data_train.drop('label', axis = 1 )
application_train.dtypes.value_counts()<categorify>
tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300)
 data_train_embedded = tsne.fit_transform(sample.drop('label', axis = 1))
application_train["FONDKAPREMONT_MODE"]<categorify>
X_train, X_holdout, y_train, y_holdout = train_test_split(data_train_pd.drop('label', axis = 1), data_train_pd.label,
test_size = 0.25, random_state=0)
 knn = KNeighborsClassifier(n_neighbors=10, n_jobs=-1)
 knn.fit(X_train, y_train)
le = LabelEncoder()
le_count = 0
for col in application_train:
if application_train[col].dtype == 'object':
if len(list(application_train[col].unique())) >= 2:
le.fit(application_train[col])
 application_train[col] = le.transform(application_train[col])
 application_test[col] = le.transform(application_test[col])
 le_count += 1
print('Label Encoding : %d 컬럼 라벨 인코딩 완료.' % le_count )<define_variables>
X_train, X_holdout, y_train, y_holdout = train_test_split(data_train_pd.drop('label', axis = 1), data_train_pd.label,
test_size = 0.25, random_state=0)
 bnbclf = BernoulliNB()
bnbclf.fit(X_train, y_train )
application_train["FONDKAPREMONT_MODE"]<count_unique_values>
print("Accuracy score: {:.2f}".format(bnbclf.score(X_holdout, y_holdout)))
 print("Cross-entropy loss: {:.2f}".format(log_loss(np.array(y_holdout), bnbclf.predict_proba(X_holdout))))
application_train.select_dtypes('object' ).apply(pd.Series.nunique, axis = 0 )<define_variables>
bnb_params = {'alpha': np.arange(0.01, 0.1, 0.05),
'binarize' : np.arange(0, 0.5, 0.2),
'fit_prior': [True, False]
}
bnbcv = GridSearchCV(bnbclf, param_grid = bnb_params, cv = 3 )
rel_list = []
for rel_column in rel.index:
if(rel[rel_column]<0.03):
rel_list.append(rel_column)
 print(rel_column )<drop_column>
bnbcv.fit(X_train, y_train)
 bnb_best = bnbcv.best_estimator_
rel_list.remove('SK_ID_CURR' )<drop_column>
bnbcv.best_params_
column_list.remove("EXT_SOURCE_1")
 app_train = application_train<train_model>
print("Accuracy score: {:.2f}".format(bnb_best.score(X_holdout, y_holdout)))
 print("Cross-entropy loss: {:.2f}".format(log_loss(np.array(y_holdout), bnb_best.predict_proba(X_holdout))))
print("Dimension of application_test :", application_test.shape)
 print("결측치가 있는 컬럼 수 :",(application_test.isnull().sum() !=0 ).sum())
 application_test.head()<drop_column>
model = Sequential()
model.add(Convolution2D(32,(3, 3), activation='relu', input_shape=(28,28,1)))
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(axis=1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer="zeros", gamma_initializer="ones",))
model.add(Convolution2D(32,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(axis=1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer="zeros", gamma_initializer="ones",))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
app_test = application_test<feature_engineering>
data_train = data_train / 255
data_test = data_test / 255
def data_processing(out, data):
out['APPS_EXT_SOURCE_MEAN'] = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis=1)
 out['APPS_EXT_SOURCE_STD'] = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].std(axis=1)
 out['APPS_EXT_SOURCE_STD'] = out['APPS_EXT_SOURCE_STD'].fillna(out['APPS_EXT_SOURCE_STD'].mean())
 out['APPS_ANNUITY_CREDIT_RATIO'] = data['AMT_ANNUITY']/data['AMT_CREDIT']
out['APPS_GOODS_CREDIT_RATIO'] = data['AMT_GOODS_PRICE']/data['AMT_CREDIT']
out['APPS_ANNUITY_INCOME_RATIO'] = data['AMT_ANNUITY']/data['AMT_INCOME_TOTAL']
out['APPS_GOODS_INCOME_RATIO'] = data['AMT_GOODS_PRICE']/data['AMT_INCOME_TOTAL']
out['APPS_CREDIT_INCOME_RATIO'] = data['AMT_CREDIT']/data['AMT_INCOME_TOTAL']
out['APPS_CNT_FAM_INCOME_RATIO'] = data['AMT_INCOME_TOTAL']/data['CNT_FAM_MEMBERS']
out['APPS_EMPLOYED_BIRTH_RATIO'] = data['DAYS_EMPLOYED']/data['DAYS_BIRTH']
out['APPS_INCOME_EMPLOYED_RATIO'] = data['AMT_INCOME_TOTAL']/data['DAYS_EMPLOYED']
out['APPS_INCOME_BIRTH_RATIO'] = data['AMT_INCOME_TOTAL']/data['DAYS_BIRTH']
out['APPS_CAR_BIRTH_RATIO'] = data['OWN_CAR_AGE'] / data['DAYS_BIRTH']
out['APPS_CAR_EMPLOYED_RATIO'] = data['OWN_CAR_AGE'] / data['DAYS_EMPLOYED']
return out<train_model>
y = np.array(pd.get_dummies(true_labels))
app_train = data_processing(app_train, application_train)
 app_test = data_processing(app_test, application_test)
 app_train.shape, app_test.shape<load_from_csv>
X_train, X_holdout, y_train, y_holdout = train_test_split(data_train, y,
test_size = 0.25, random_state=17 )
prev_app = pd.read_csv('.. /input/home-credit-default-risk/previous_application.csv')
 print(prev_app.shape, app_train.shape )<feature_engineering>
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
factor=0.1,
patience=5,
min_lr=0.000001,
verbose=1 )
prev_app['PREV_CREDIT_DIFF'] = prev_app['AMT_APPLICATION'] - prev_app['AMT_CREDIT']
prev_app['PREV_GOODS_DIFF'] = prev_app['AMT_APPLICATION'] - prev_app['AMT_GOODS_PRICE']
prev_app['PREV_CREDIT_APPL_RATIO'] = prev_app['AMT_CREDIT']/prev_app['AMT_APPLICATION']
prev_app['PREV_ANNUITY_APPL_RATIO'] = prev_app['AMT_ANNUITY']/prev_app['AMT_APPLICATION']
prev_app['PREV_GOODS_APPL_RATIO'] = prev_app['AMT_GOODS_PRICE']/prev_app['AMT_APPLICATION']<feature_engineering>
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
 result = model.fit(X_train, y_train, batch_size=32, epochs=10, verbose=1, validation_data=(X_holdout, y_holdout), callbacks = [reduce_lr] )
prev_app['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace=True)
 prev_app['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace= True)
 prev_app['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace= True)
 prev_app['DAYS_LAST_DUE'].replace(365243, np.nan, inplace= True)
 prev_app['DAYS_TERMINATION'].replace(365243, np.nan, inplace= True)
 prev_app['PREV_DAYS_LAST_DUE_DIFF'] = prev_app['DAYS_LAST_DUE_1ST_VERSION'] - prev_app['DAYS_LAST_DUE']<define_variables>
layer_names = [layer.name for layer in model.layers]
layer_outputs = [layer.output for layer in model.layers]
layer_outputs = [layer_outputs[0], layer_outputs[2]]
feature_map_model = Model(model.input, layer_outputs)
 im = X_train[99:100,:]
feature_maps = feature_map_model.predict(im )
agg_dict = {
'SK_ID_CURR':['count'],
'AMT_CREDIT':['mean', 'max', 'sum'],
'AMT_ANNUITY':['mean', 'max', 'sum'],
'AMT_APPLICATION':['mean', 'max', 'sum'],
'AMT_DOWN_PAYMENT':['mean', 'max', 'sum'],
'AMT_GOODS_PRICE':['mean', 'max', 'sum'],
'RATE_DOWN_PAYMENT': ['min', 'max', 'mean'],
'DAYS_DECISION': ['min', 'max', 'mean'],
'CNT_PAYMENT': ['mean', 'sum'],
'PREV_CREDIT_DIFF':['mean', 'max', 'sum'],
'PREV_CREDIT_APPL_RATIO':['mean', 'max'],
'PREV_GOODS_DIFF':['mean', 'max', 'sum'],
'PREV_GOODS_APPL_RATIO':['mean', 'max'],
'PREV_DAYS_LAST_DUE_DIFF':['mean', 'max', 'sum'],
'PREV_INTERESTS_RATE':['mean', 'max']
}
prev_group = prev_app.groupby('SK_ID_CURR')
 prev_amt_agg = prev_group.agg(agg_dict)
 prev_amt_agg.columns = ['PREV_' +('_' ).join(column ).upper() for column in prev_amt_agg.columns.ravel() ]
prev_amt_agg.head()<merge>
augumentator = tf.keras.preprocessing.image.ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.15,
shear_range=0.1,
zoom_range=0.1,
validation_split=0.0,
horizontal_flip=False,
vertical_flip=False)
 augumentator.fit(X_train )
prev_app_merge = app_train.merge(prev_amt_agg, on='SK_ID_CURR', how='left', indicator=True)
 prev_app_merge = prev_app_merge.drop(columns=['_merge'])
 prev_app_merge.shape<count_values>
history = model.fit(augumentator.flow(X_train, y_train, batch_size = 32), epochs = 10,
validation_data =(X_holdout, y_holdout), verbose = 1, callbacks = [reduce_lr] )
prev_app['NAME_CONTRACT_STATUS'].value_counts()<define_variables>
mnist = tf.keras.datasets.mnist 
(X_train_mnist, y_train_mnist),(X_val_mnist, y_val_mnist)= mnist.load_data()
cond_refused =(prev_app['NAME_CONTRACT_STATUS'] == 'Refused')
 cond_approved =(prev_app['NAME_CONTRACT_STATUS'] == 'Approved')
 prev_refused = prev_app[cond_refused]
prev_approved = prev_app[cond_approved]
prev_refused.shape, prev_approved.shape, prev_app.shape<groupby>
y_train_mnist = np.array(pd.get_dummies(pd.Series(y_train_mnist)))
 y_holdout_mnist = np.array(pd.get_dummies(pd.Series(y_val_mnist)) )
prev_refused = prev_refused.groupby('SK_ID_CURR')
 prev_approved = prev_approved.groupby('SK_ID_CURR' )<count_values>
X_train_mnist = X_train_mnist.reshape(-1, 28, 28, 1)
 X_holdout_mnist = X_val_mnist.reshape(-1, 28, 28, 1)
 X_train_mnist = X_train_mnist / 255
X_holdout_mnist = X_holdout_mnist /255
prev_refused = prev_refused['NAME_CONTRACT_TYPE'].count()
prev_refused.name = "PRE_CONTRACT_REFUSED"
prev_approved = prev_approved['NAME_CONTRACT_TYPE'].count()
prev_approved.name = "PRE_CONTRACT_APPROVED"<merge>
X_train_ext = np.concatenate(( X_train, X_train_mnist), axis = 0)
 X_holdout_ext = np.concatenate(( X_holdout, X_holdout_mnist), axis = 0)
 y_train_ext = np.concatenate(( y_train, y_train_mnist), axis = 0)
 y_holdout_ext = np.concatenate(( y_holdout, y_holdout_mnist), axis = 0 )
prev_app_merge = prev_app_merge.merge(prev_approved, on='SK_ID_CURR', how='left', indicator=False)
 prev_app_merge = prev_app_merge.merge(prev_refused, on='SK_ID_CURR', how='left', indicator=False)
 prev_app_merge['PRE_CONTRACT_APPROVED_RATE'] = prev_app_merge['PRE_CONTRACT_APPROVED'] /(prev_app_merge['PRE_CONTRACT_APPROVED'] + prev_app_merge['PRE_CONTRACT_REFUSED'])
 prev_app_merge['PRE_CONTRACT_REFUSED_RATE'] = prev_app_merge['PRE_CONTRACT_REFUSED'] /(prev_app_merge['PRE_CONTRACT_APPROVED'] + prev_app_merge['PRE_CONTRACT_REFUSED'])
 prev_app_merge.head()<categorify>
model.fit(X_train_ext, y_train_ext, batch_size=32, epochs=20, verbose=1, validation_data=(X_holdout, y_holdout), callbacks = [reduce_lr] )
prev_app_merge = prev_app_merge.replace(float('NaN'),0)
 prev_app_merge.head()<load_from_csv>
predictions = model.predict(data_test ).argmax(axis = 1)
 predictions
submission = pd.DataFrame({'ImageId':np.arange(1, len(predictions)+1), 'Label':predictions})
 submission.to_csv('submission.csv', index=False )