Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
%matplotlib inline
prev_app = pd.read_csv('.. /input/home-credit-default-risk/previous_application.csv')
 print(prev_app.shape, apps.shape )<merge>
train = import_data('.. /input/digit-recognizer/train.csv')
 test = import_data('.. /input/digit-recognizer/test.csv')
 y_lab = train['label']
y = tf.keras.utils.to_categorical(y_lab)
 train.drop('label', axis=1, inplace=True )
prev_app_outer = prev_app.merge(apps['SK_ID_CURR'], on='SK_ID_CURR', how='outer', indicator=True )<count_values>
train_df = np.array(train ).reshape(-1, 28, 28, 1)
 test_df = np.array(test ).reshape(-1, 28, 28, 1)
 del train
del test
del y_lab
prev_app_outer['_merge'].value_counts()<sort_values>
def change_size(image):
img = array_to_img(image, scale=False)
 img = img.resize(( 75, 75))
img = img.convert(mode='RGB')
 arr = img_to_array(img)
 return arr.astype(np.float32 )
def missing_data(data):
total = data.isnull().sum().sort_values(ascending = False)
 percent =(data.isnull().sum() /data.isnull().count() *100 ).sort_values(ascending = False)
 return pd.concat([total, percent], axis=1, keys=['Total', 'Percent'] )<groupby>
train_array = [change_size(img)for img in train_df]
train = np.array(train_array)
 del train_array
test_array = [change_size(img)for img in test_df]
test = np.array(test_array)
 del test_array
prev_app.groupby('SK_ID_CURR' ).count()<groupby>
def get_random_eraser(p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3, v_l=0, v_h=255, pixel_level=False):
def eraser(input_img):
if input_img.ndim == 3:
img_h, img_w, img_c = input_img.shape
elif input_img.ndim == 2:
img_h, img_w = input_img.shape
p_1 = np.random.rand()
if p_1 > p:
return input_img
while True:
s = np.random.uniform(s_l, s_h)* img_h * img_w
r = np.random.uniform(r_1, r_2)
 w = int(np.sqrt(s / r))
h = int(np.sqrt(s * r))
left = np.random.randint(0, img_w)
 top = np.random.randint(0, img_h)
 if left + w <= img_w and top + h <= img_h:
break
if pixel_level:
if input_img.ndim == 3:
c = np.random.uniform(v_l, v_h,(h, w, img_c))
if input_img.ndim == 2:
c = np.random.uniform(v_l, v_h,(h, w))
else:
c = np.random.uniform(v_l, v_h)
 input_img[top:top + h, left:left + w] = c
return input_img
return eraser
prev_app.groupby('SK_ID_CURR')['SK_ID_CURR'].count()<merge>
image_gen = ImageDataGenerator(rescale=1./255,
featurewise_center=False,
preprocessing_function=get_random_eraser(v_l=0, v_h=1),
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zoom_range=0.1,
rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.3,
validation_split=0.2)
 train_generator = image_gen.flow(train,
y,
batch_size=32,
shuffle=True,
subset='training',
seed=42)
 valid_generator = image_gen.flow(train,
y,
batch_size=16,
shuffle=True,
subset='validation')
 del train_df
del test_df
del train
app_prev_target = prev_app.merge(app_train[['SK_ID_CURR', 'TARGET']], on='SK_ID_CURR', how='left')
 app_prev_target.shape<define_variables>
model = Sequential()
model.add(tf.keras.applications.resnet50.ResNet50(input_shape =(75, 75, 3),
pooling = 'avg',
include_top = False,
weights = 'imagenet'))
model.add(L.Flatten())
 model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(10, activation='softmax'))
model.compile(optimizer=RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0), loss='categorical_crossentropy', metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
num_columns = [column for column in num_columns if column not in ['SK_ID_PREV', 'SK_ID_CURR', 'TARGET']]
num_columns<count_values>
for layer in model.layers[0].layers:
if layer.name == 'conv5_block1_0_conv':
break
layer.trainable=False
app_prev_target.TARGET.value_counts()<groupby>
history = model.fit(train_generator, validation_data=valid_generator, epochs=20,
steps_per_epoch=train_generator.n//train_generator.batch_size,
validation_steps=valid_generator.n//valid_generator.batch_size,
callbacks=[learning_rate_reduction] )
print(app_prev_target.groupby('TARGET' ).agg({'AMT_ANNUITY': ['mean', 'median', 'count']}))
print(app_prev_target.groupby('TARGET' ).agg({'AMT_APPLICATION': ['mean', 'median', 'count']}))
print(app_prev_target.groupby('TARGET' ).agg({'AMT_CREDIT': ['mean', 'median', 'count']}))<groupby>
test = test/255
<groupby><EOS> 
res = model.predict(test[:])
 output = pd.DataFrame({'ImageId':[ i+1 for i in range(len(res)) ],
'Label': [ xi.argmax() for xi in res]})
 output.to_csv('submission_grid.csv', index=False )