Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<compute_test_metric>
print(os.listdir(".. /input"))
data, validate_data<prepare_x_and_y>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
train_set_x, train_set_y = data.drop('type', 1), data['type']<choose_model_class>
Y_train = train["label"]
X_train = train.drop(labels="label",axis=1)
 Y_train.value_counts()
classifier = GridSearchCV(
 KNeighborsClassifier() ,
param_grid={
'n_neighbors': np.arange(1, 100),
'p': np.arange(1, 10)
 },
scoring='accuracy',
cv=3
)<train_model>
X_train = X_train/255.0
test = test/255.0
classifier.fit(train_set_x, train_set_y )<find_best_params>
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
scores = classifier.cv_results_['mean_test_score']
scores, scores.mean() , scores.max()<find_best_params>
Y_train = to_categorical(Y_train,num_classes=10 )
classifier.best_params_<predict_on_test>
X_train, X_test, Y_train, Y_test = train_test_split(X_train, Y_train, test_size = 0.05 )
np.mean(classifier.predict(train_set_x)== train_set_y )<predict_on_test>
def conv_layer(x,concat_axis,nb_filter,dropout_rate=None,weight_decay=1E-4):
x = BatchNormalization(axis=concat_axis,
gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_filter,(3,3),padding='same',kernel_regularizer=l2(weight_decay),use_bias=False )(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 return x
def transition_layer(x,concat_axis,nb_filter,dropout_rate=None,weight_decay=1E-4):
x = BatchNormalization(axis=concat_axis,
gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_filter,(1,1),padding='same',kernel_regularizer=l2(weight_decay),use_bias=False )(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 x = AveragePooling2D(( 2,2),strides=(2,2))(x)
 return x
def denseblock(x,concat_axis,nb_filter,nb_layers,growth_rate,dropout_rate=None,weight_decay=1E-4):
list_features = [x]
for i in range(nb_layers):
x = conv_layer(x,concat_axis,growth_rate,dropout_rate=None,weight_decay=1E-4)
 list_features.append(x)
 x = Concatenate(axis=concat_axis )(list_features)
 nb_filter += growth_rate
return x,nb_filter
def Densenet(nb_classes,img_dim,depth,nb_dense_block,nb_filter,growth_rate,
dropout_rate=None,weight_decay=1E-4):
if K.image_dim_ordering() == "th":
concat_axis = 1
elif K.image_dim_ordering() == "tf":
concat_axis = -1
model_input = Input(shape=img_dim)
 assert(depth-4)%3 == 0 , "Depth must be 4*N +3"
nb_layers = int(( depth-4)/ 3)
 x = Conv2D(nb_filter,(3,3),padding='same',use_bias=False,
kernel_regularizer=l2(weight_decay))(model_input)
 for block_id in range(nb_dense_block-1):
x,nb_filter = denseblock(x,concat_axis,nb_filter,nb_layers,growth_rate,
dropout_rate=None,weight_decay=1E-4)
 x = transition_layer(x,concat_axis,nb_filter,dropout_rate=None,weight_decay=1E-4)
 x = BatchNormalization(axis=concat_axis,
gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
 x = Activation('relu' )(x)
 x = GlobalAveragePooling2D(data_format=K.image_data_format() )(x)
 x = Dense(nb_classes,activation='softmax',kernel_regularizer=l2(weight_decay),
bias_regularizer=l2(weight_decay))(x)
 densenet = Model(inputs=[model_input], outputs=[x], name="DenseNet")
 return densenet
submission = classifier.predict(validate_data )<save_to_csv>
model = Densenet(nb_classes=10,
img_dim=(28,28,1),
depth = 34,
nb_dense_block = 5,
growth_rate=12,
nb_filter=32,
dropout_rate=0.2,
weight_decay=1E-4)
 model.summary()
pd.DataFrame({'id': validate_data_ids, 'type': submission} ).to_csv('submission.csv', index=False )<load_pretrained>
model_filepath = 'model.h5'
batch_size=64
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)
 lr_reduce = ReduceLROnPlateau(monitor='val_acc', factor=0.1, epsilon=1e-5, patience=2, verbose=1)
 msave = ModelCheckpoint(model_filepath, save_best_only=True )
warnings.filterwarnings('ignore');
zipfile.ZipFile('/kaggle/input/ghouls-goblins-and-ghosts-boo/train.csv.zip' ).extractall()
zipfile.ZipFile('/kaggle/input/ghouls-goblins-and-ghosts-boo/test.csv.zip' ).extractall()
%matplotlib inline 
<train_model>
model.compile(loss='categorical_crossentropy',
optimizer = Adamax() ,
metrics=['accuracy'])
 model.fit(X_train ,Y_train,
batch_size = 64,
validation_data =(X_test,Y_test),
epochs = 20,
callbacks=[lr_reduce,msave,annealer],
verbose = 1 )
def N_net(train, test, target):
hidden_layer_sizes=(100,)
 activation = 'relu'
solver = 'adam'
batch_size = 'auto'
alpha = 0.0001
random_state = 0
max_iter = 10000
early_stopping = True
clf = MLPClassifier(
 hidden_layer_sizes=hidden_layer_sizes,
activation=activation,
solver=solver,
batch_size=batch_size,
alpha=alpha,
random_state=random_state,
max_iter=max_iter, 
)
 clf.fit(train, target)
 SAVE_TRAINED_DATA_PATH = 'train1.learn'
joblib.dump(clf, SAVE_TRAINED_DATA_PATH)
 clf1 = joblib.load(SAVE_TRAINED_DATA_PATH)
 predict_no = clf1.predict(test)
 predict = np.round(np.round(predict_no, decimals=1))
return predict<train_model>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<split><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )