Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<groupby>
warnings.filterwarnings("ignore")
 %matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
prev_group = prev_app.groupby('SK_ID_CURR')
 prev_group.head()<create_dataframe>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 sub = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 print("Data are Ready!!" )
prev_agg = pd.DataFrame()
prev_agg['CNT'] = prev_group['SK_ID_CURR'].count()
prev_agg.head()<feature_engineering>
print(f"Training data size is {train.shape}
Testing data size is {test.shape}" )
prev_agg['AVG_CREDIT'] = prev_group['AMT_CREDIT'].mean()
prev_agg['MAX_CREDIT'] = prev_group['AMT_CREDIT'].max()
prev_agg['MIN_CREDIT'] = prev_group['AMT_CREDIT'].min()
prev_agg.head()<merge>
Y_train = train["label"]
X_train = train.drop(labels = ["label"], axis = 1 )
prev_group = prev_app.groupby('SK_ID_CURR')
 prev_agg1 = prev_group['AMT_CREDIT'].agg(['mean', 'max', 'min'])
 prev_agg2 = prev_group['AMT_ANNUITY'].agg(['mean', 'max', 'min'])
 prev_agg = prev_agg1.merge(prev_agg2, on='SK_ID_CURR', how='inner')
 prev_agg.head()<feature_engineering>
( x_train1, y_train1),(x_test1, y_test1)= mnist.load_data()
train1 = np.concatenate([x_train1, x_test1], axis=0)
 y_train1 = np.concatenate([y_train1, y_test1], axis=0)
 Y_train1 = y_train1
X_train1 = train1.reshape(-1, 28*28 )
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>
X_train = X_train / 255.0
test = test / 255.0
X_train1 = X_train1 / 255.0
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']<feature_engineering>
X_train = np.concatenate(( X_train.values, X_train1))
Y_train = np.concatenate(( Y_train, Y_train1))
all_pay = prev_app['AMT_ANNUITY'] * prev_app['CNT_PAYMENT']
prev_app['PREV_INTERESTS_RATE'] =(all_pay/prev_app['AMT_CREDIT'] - 1)/prev_app['CNT_PAYMENT']<define_variables>
Y_train = to_categorical(Y_train, num_classes = 10 )
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']
}<groupby>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )
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() ]<count_values>

 model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation = "softmax"))
prev_app['NAME_CONTRACT_STATUS'].value_counts()<groupby>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
prev_refused_agg = prev_refused.groupby('SK_ID_CURR')['SK_ID_CURR'].count()
prev_refused_agg.shape, prev_amt_agg.shape<create_dataframe>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
pd.DataFrame(prev_refused_agg )<rename_columns>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
prev_refused_agg.reset_index(name='PREV_REFUSED_COUNT' )<merge>
epochs = 50
batch_size = 128
prev_refused_agg = prev_refused_agg.reset_index(name='PREV_REFUSED_COUNT')
 prev_amt_agg = prev_amt_agg.reset_index()
prev_amt_refused_agg = prev_amt_agg.merge(prev_refused_agg, on='SK_ID_CURR', how='left')
 prev_amt_refused_agg.head(10 )<count_values>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 train_gen = datagen.flow(X_train,Y_train, batch_size=batch_size )
prev_amt_refused_agg['PREV_REFUSED_COUNT'].value_counts(dropna=False )<feature_engineering>
history = model.fit(train_gen,
epochs = epochs,validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction],
validation_steps = X_val.shape[0] // batch_size )
prev_amt_refused_agg = prev_amt_refused_agg.fillna(0)
 prev_amt_refused_agg['PREV_REFUSE_RATIO'] = prev_amt_refused_agg['PREV_REFUSED_COUNT'] / prev_amt_refused_agg['PREV_SK_ID_CURR_COUNT']
prev_amt_refused_agg.head(10 )<groupby>
errors =(Y_pred_classes - Y_true != 0)
 Y_pred_classes_errors = Y_pred_classes[errors]
Y_pred_errors = Y_pred[errors]
Y_true_errors = Y_true[errors]
X_val_errors = X_val[errors]
prev_refused_appr_group = prev_app[prev_app['NAME_CONTRACT_STATUS'].isin(['Approved', 'Refused'])].groupby(['SK_ID_CURR', 'NAME_CONTRACT_STATUS'])
 prev_refused_appr_agg = prev_refused_appr_group['SK_ID_CURR'].count().unstack()
prev_refused_appr_agg.head(10 )<drop_column>
Y_pred_errors_prob = np.max(Y_pred_errors,axis = 1)
 true_prob_errors = np.diagonal(np.take(Y_pred_errors, Y_true_errors, axis=1))
delta_pred_true_errors = Y_pred_errors_prob - true_prob_errors
sorted_dela_errors = np.argsort(delta_pred_true_errors)
 most_important_errors = sorted_dela_errors[-6:]
display_errors(most_important_errors, X_val_errors, Y_pred_classes_errors, Y_true_errors )
prev_refused_appr_agg = prev_refused_appr_agg.fillna(0)
 prev_refused_appr_agg.columns = ['PREV_APPROVED_COUNT', 'PREV_REFUSED_COUNT']
prev_refused_appr_agg = prev_refused_appr_agg.reset_index()
prev_refused_appr_agg.head(10 )<merge>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
prev_agg = prev_amt_agg.merge(prev_refused_appr_agg, on='SK_ID_CURR', how='left')
 prev_agg['PREV_REFUSED_RATIO'] = prev_agg['PREV_REFUSED_COUNT']/prev_agg['PREV_SK_ID_CURR_COUNT']
prev_agg['PREV_APPROVED_RATIO'] = prev_agg['PREV_APPROVED_COUNT']/prev_agg['PREV_SK_ID_CURR_COUNT']
prev_agg = prev_agg.drop(['PREV_REFUSED_COUNT', 'PREV_APPROVED_COUNT'], axis=1)
 prev_agg.head(30 )<categorify>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_submission.csv",index=False )
apps_all = get_apps_processed(apps )<merge>
( x_train1, y_train1),(x_test1, y_test1)= mnist.load_data()
Y_train1 = y_train1
X_train1 = x_train1.reshape(-1, 28*28 )
print(apps_all.shape, prev_agg.shape)
 apps_all = apps_all.merge(prev_agg, on='SK_ID_CURR', how='left')
 print(apps_all.shape )<feature_engineering>
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pd.read_csv('.. /input/digit-recognizer/test.csv' )
object_columns = apps_all.dtypes[apps_all.dtypes == 'object'].index.tolist()
for column in object_columns:
apps_all[column] = pd.factorize(apps_all[column])[0]<drop_column>
train_images = train_data.copy()
train_images = train_images.values
X_train = train_images[:,1:]
y_train = train_images[:,0]
X_test = test_data.values
apps_all_train = apps_all[~apps_all['TARGET'].isnull() ]
apps_all_test = apps_all[apps_all['TARGET'].isnull() ]
apps_all_test = apps_all_test.drop('TARGET', axis=1 )<split>
predictions = np.zeros(( X_train.shape[0]))
ftr_app = apps_all_train.drop(['SK_ID_CURR', 'TARGET'], axis=1)
 target_app = apps_all_train['TARGET']
train_x, valid_x, train_y, valid_y = train_test_split(ftr_app, target_app, test_size=0.3, random_state=2020)
 train_x.shape, valid_x.shape<train_model>
x1=0
x2=0
print("Classifying Kaggle's 'test.csv' using KNN where K=1 and MNIST 70k images.. ")
 for i in range(0,28000):
for j in range(0,70000):
if np.absolute(X_test[i,:]-mnist_image[j,:] ).sum() ==0:
predictions[i]=mnist_label[j]
if i%1000==0:
print(" %d images classified perfectly"%(i),end="")
 if j<60000:
x1+=1
else:
x2+=1
break
if x1+x2==28000:
print(" 28000 images classified perfectly.")
 print("All 28000 images are contained in MNIST.npz Dataset.")
 print("%d images are in MNIST.npz train and %d images are in MNIST.npz test"%(x1,x2))
clf = LGBMClassifier(
 n_jobs=-1,
n_estimators=1000,
learning_rate=0.02,
num_leaves=32,
subsample=0.8,
max_depth=12,
silent=-1,
verbose=-1 
)
 clf.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)], eval_metric= 'auc', verbose= 100,
early_stopping_rounds= 50 )<save_to_csv>
final_pred = predictions[0:28000]
preds = clf.predict_proba(apps_all_test.drop('SK_ID_CURR', axis=1)) [:, 1 ]
apps_all_test['TARGET'] = preds
apps_all_test[['SK_ID_CURR', 'TARGET']].to_csv('prev_baseline_03.csv', index=False )<load_from_csv>
my_submission = pd.DataFrame({'ImageId':np.arange(28000),'Label':final_pred.squeeze().astype(np.int)})
 my_submission.head()
application_train = pd.read_csv('/kaggle/input/home-credit-default-risk/application_train.csv')
 application_test = pd.read_csv('/kaggle/input/home-credit-default-risk/application_test.csv')
<train_model>
my_submission["ImageId"]=my_submission["ImageId"]+1
print("Dimension of application_train :", application_train.shape)
 print("결측치가 있는 컬럼 수 :",(application_train.isnull().sum() !=0 ).sum())
 application_train.head()<train_model>
my_submission.to_csv('best_submission.csv', index=False )