Kaggle competition: Digit Recognizer
y_pred = model3.predict(X_test)
 y_pred =np.round(y_pred,6)
 print('min',y_pred.min())
 print('max',y_pred.max())
 RMSE = np.sqrt(mean_squared_error(y_test,y_pred))
print('RMSE',RMSE)
 print(y_pred.tolist() )<predict_on_test>
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Input, Conv2D, Dense, Activation, Flatten, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D, MaxPooling2D, AveragePooling2D, BatchNormalization, LeakyReLU, Concatenate
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import regularizers, optimizers
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
ys_pred =model3.predict(Xf_test)
 ys_pred =np.round(ys_pred,6)
 ys_pred.tolist()<save_to_csv>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
sub = pd.DataFrame({'id':test_df.id,'target':ys_pred})
 sub.to_csv('TPS.csv',index=False)
 sub.head()<create_dataframe>
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size = 0.1, random_state=1337 )
%matplotlib inline
dfk = pd.DataFrame({
'Kernel ID': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U'],
'Score': [ 0.84362, 0.84358, 0.84352, 0.84318, 0.84310, 0.84303, 0.84266, 0.84251, 0.84245, 0.84243, 0.84233, 0.84229, 0.84221, 0.84220, 0.84209, 0.84207, 0.84202, 0.84200, 0.84199, 0.84198, 0.84193],
'File Path': ['.. /input/feb84362/FEB84362.csv', '.. /input/feb84358/FEB84358.csv', '.. /input/feb84352/FEB84352.csv', '.. /input/feb84318/FEB84318.csv', '.. /input/feb84310/FEB84310.csv', '.. /input/feb84303/FEB84303.csv', '.. /input/feb84266/FEB84266.csv', '.. /input/feb84251/FEB84251.csv', '.. /input/feb84245/FEB84245.csv', '.. /input/feb84243/FEB84243.csv', '.. /input/feb84233/FEB84233.csv' , '.. /input/feb84229/FEB84229.csv', '.. /input/feb84221/FEB84221.csv', '.. /input/feb84220/FEB84220.csv', '.. /input/feb84209/FEB84209.csv', '.. /input/feb84207/FEB84207.csv', '.. /input/feb84202/FEB84202.csv', '.. /input/feb84200/FEB84200.csv', '.. /input/feb84199/FEB84199.csv', '.. /input/feb84198/FEB84198.csv', '.. /input/feb84193/FEB84193.csv']
})
 dfk<categorify>
rlr = ReduceLROnPlateau(monitor='accuracy', mode = 'max', factor=0.5, min_lr=1e-7, verbose = 1, patience=5)
 es = EarlyStopping(monitor='accuracy', mode='max', verbose = 1, patience=50)
 mc = ModelCheckpoint('cnn_best_model.h5', monitor='accuracy', mode='max', verbose = 1, save_best_only=True )
def generate(main, support, coeff):
g = main.copy()
for i in main.columns[1:]:
res = []
lm, Is = [], []
lm = main[i].tolist()
ls = support[i].tolist()
for j in range(len(main)) :
res.append(( lm[j] * coeff)+(ls[j] *(1.- coeff)))
 g[i] = res
return g 
<load_from_csv>
def build_model(lr = 0, mt = 0, dr = 0):
model = Sequential(name = 'cnn_mnist')
 model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.3))
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(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(Dropout(dr))
model.add(Dense(10, activation = "softmax"))
opt = optimizers.SGD(lr = lr, momentum = mt)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 return model
support = pd.read_csv(dfk.iloc[0, 2])
 for k in range(1, 6):
main = pd.read_csv(dfk.iloc[k, 2])
 support = generate(main, support, 0.60)
 sub1 = support<load_from_csv>
model = build_model(lr = 0.01, mt = 0.9, dr = 0.5 )
support = sub1
for k in range(6, 14):
main = pd.read_csv(dfk.iloc[k, 2])
 support = generate(main, support, 0.50)
 sub2 = support<compute_test_metric>
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
horizontal_flip=False,
vertical_flip=False 
)
 datagen.fit(X_train )
sub2 = comparison2(sub2, 10, 0.9970, 1.0000 )<load_from_csv>
model.fit_generator(datagen.flow(X_train, y_train, batch_size = 64),
validation_data =(X_valid, y_valid),
steps_per_epoch = X_train.shape[0] // 64,
epochs = 400, verbose = 2,
callbacks = [rlr, es, mc] )
<compute_test_metric><EOS> 
saved_model = load_model('cnn_best_model.h5')
 y_pred = saved_model.predict_classes(X_test, verbose=0)
 submissions=pd.DataFrame({"ImageId": list(range(1, len(y_pred)+1)) ,
"Label": y_pred})
 submissions.to_csv("submission.csv", index=False )