Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<prepare_output>
np.random.seed(92)
Submission=test_data[['id','cuisine']]
Submission.set_index('id',inplace=True )<save_to_csv>
train_data = '/kaggle/input/digit-recognizer/train.csv'
test_data = '/kaggle/input/digit-recognizer/test.csv'
Submission.to_csv('Submission.csv' )<load_pretrained>
train_df = pd.read_csv(train_data)
 print(train_df.shape)
 train_df.head()
archive_train=zipfile.ZipFile('/kaggle/input/whats-cooking/train.json.zip','r')
 train_data=pd.read_json(archive_train.read('train.json'))
train_data.head()<load_pretrained>
test_df = pd.read_csv(test_data)
 print(test_df.shape)
 test_df.head()
archive_test=zipfile.ZipFile('/kaggle/input/whats-cooking/test.json.zip','r')
 test_data=pd.read_json(archive_test.read('test.json'))
test_data.head()<count_values>
if 'label' in train_df.columns:
y_train = train_df['label'].values.astype('int32')
 train_df = train_df.drop('label', axis = 1)
 else:
pass
x_train = train_df.values.astype('float32')
 x_test = test_df.values.astype('float32' )
train_data['cuisine'].value_counts()<count_missing_values>
train_max = np.max(x_train)
 train_min = np.min(x_train)
 test_max = np.max(x_test)
 test_min = np.min(x_test)
train_data.isna().sum()<count_missing_values>
x_train = x_train/255.0
x_test = x_test/255.0
test_data.isna().sum()<define_variables>
norm_train_max = np.max(x_train)
 norm_train_min = np.min(x_train)
 norm_test_max = np.max(x_test)
 norm_test_min = np.min(x_test )
train_ingredients_count={}
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
if j in train_ingredients_count.keys() :
train_ingredients_count[j]+=1
else:
train_ingredients_count[j]=1<define_variables>
y_train= to_categorical(y_train)
 num_classes = y_train.shape[1]
num_classes
test_ingredients_count={}
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
if j in test_ingredients_count.keys() :
test_ingredients_count[j]+=1
else:
test_ingredients_count[j]=1<define_variables>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16,(5,5),activation='relu', input_shape=(28,28,1)) ,
tf.keras.layers.Conv2D(16,(5,5), activation= 'relu'),
tf.keras.layers.Conv2D(16,(5,5), activation= 'relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(32,(3,3), activation= 'relu'),
tf.keras.layers.Conv2D(32,(3,3), activation= 'relu'),
tf.keras.layers.Conv2D(32,(3,3), activation = 'relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.25),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.50),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation = 'relu'),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 print('input shape :', model.input_shape)
 print('output shape :', model.output_shape )
ingredients_missing_train=[]
for i in test_ingredients_count.keys() :
if i not in train_ingredients_count.keys() :
ingredients_missing_train.append(i)
 print(len(ingredients_missing_train))<define_variables>
for i in ingredients_missing_train:
train_ingredients_count[i]=0
print(len(train_ingredients_count))<define_variables>
ingredients_missing=[]
for i in train_ingredients_count.keys() :
if i not in test_ingredients_count.keys() :
ingredients_missing.append(i)
 print(len(ingredients_missing))<define_variables>
for i in ingredients_missing:
test_ingredients_count[i]=0
print(len(test_ingredients_count))<feature_engineering>
model.compile(loss = 'categorical_crossentropy', optimizer= RMSprop(lr=0.003), metrics = ['acc'] )
for i in train_ingredients_count.keys() :
train_data[i]=np.zeros(len(train_data))
for i in test_ingredients_count.keys() :
test_data[i]=np.zeros(len(test_data))<filter>
train_generator = image.ImageDataGenerator()
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
train_data[j].iloc[i]=1<filter>
X = x_train
Y = y_train
X_train, X_val, Y_train , Y_val = train_test_split(x_train,y_train, test_size= 0.05, random_state = 92)
 print(X_train.shape)
 batches = train_generator.flow(X_train, Y_train, batch_size=32)
 val_batches = train_generator.flow(X_val, Y_val, batch_size=32 )
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
test_data[j].iloc[i]=1<drop_column>
history = model.fit_generator(
 generator=batches,
steps_per_epoch=batches.n,
epochs=20,
validation_data=val_batches,
validation_steps=val_batches.n,
)
test_data=test_data[train_data.drop('cuisine',axis=1 ).columns]<prepare_x_and_y>
predictions = model.predict_classes(x_test, verbose=0)
<split><EOS> 
submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("DR.csv", index=False, header=True )