Kaggle competition: Digit Recognizer
model = load_model('mnist_model_best.hdf5')
 score = model.evaluate(X_val, y_val, verbose=0)
 accuracy = 100*score[1]
print('Validation accuracy: %.4f%%' % accuracy )<save_to_csv>
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers,models,initializers
predictions = model.predict_classes(X_test, verbose=1)
 submissions = pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("mnist_MLP_test.csv",index=False )<import_modules>
np.random.seed(20200422)
 tf.random.set_seed(20200422 )
import pandas as pd
from matplotlib import pyplot
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from numpy import asarray, unique, argmax
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
from keras.optimizers import RMSprop<load_from_csv>
sample_submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 data.head()<count_unique_values>
train_labels = tf.keras.utils.to_categorical(train['label'],10)
 train.drop('label',axis=1,inplace=True)
 train_images = train/255
test_images = test/255
unique = data['label'].unique()
print("Unique Numbers :",unique)
 n_classes = len(unique)
 print("Number of classes :",n_classes )<drop_column>
model = models.Sequential()
model.add(layers.Reshape(( 28,28,1),input_shape=(28*28,),name='reshape'))
model.add(layers.Conv2D(32,(5,5),padding='same',
kernel_initializer=initializers.TruncatedNormal() ,
use_bias=True,activation='relu',name='conv_filter1'))
model.add(layers.MaxPooling2D(( 2,2),name='max_pooling1'))
model.add(layers.Conv2D(64,(5,5),padding='same',
kernel_initializer=initializers.TruncatedNormal() ,
use_bias=True,activation='relu',name='conv_filter2'))
model.add(layers.MaxPooling2D(( 2,2),name='max_pooling2'))
model.add(layers.Flatten(name='flatten'))
model.add(layers.Dense(1024,activation='relu',
kernel_initializer=initializers.TruncatedNormal() ,
name='hidden'))
model.add(layers.Dropout(rate=0.5,name='dropout'))
model.add(layers.Dense(10,activation='softmax',name='softmax'))
model.summary()
x = data.drop(labels = ["label"], axis=1)
 print("X= ",x )<prepare_x_and_y>
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['acc'])
 histroy = model.fit(train_images,train_labels,batch_size=128,epochs=10 )
y = data['label']
print("Target Variable: ",y )<split>
pd.DataFrame({'acc':histroy.history['acc'],'loss':histroy.history['loss']} ).plot()
<train_model><EOS> 
predict_value = model.predict(test_images)
 predict_index = np.argmax(predict_value,axis=1)
 sample_submission['Label'] = predict_index
sample_submission.to_csv('submission.csv',index=False )