Kaggle competition: Kannada MNIST
train = fe(train )<prepare_x_and_y>
print(os.listdir(".. /input"))
directory_train = ".. /input/Kannada-MNIST/train.csv"
directory_test = ".. /input/Kannada-MNIST/test.csv"
X_train = np.array(train[players_col] ).reshape(-1,(len(players_col)) *22)
 play_col = train.drop(players_col+['Yards'], axis=1 ).columns
X_play_col = np.zeros(shape=(X_train.shape[0], len(play_col)))
 for i, col in enumerate(play_col):
X_play_col[:, i] = train[col][::22]<prepare_x_and_y>
test_data = pd.read_csv(directory_test)
 test_data.head(10 )
X_train = np.concatenate([X_train, X_play_col], axis=1)
 y_train = np.zeros(shape=(X_train.shape[0], 199))
for i,yard in enumerate(train['Yards'][::22]):
y_train[i, yard+99:] = np.ones(shape=(1, 100-yard))<import_modules>
train_data = pd.read_csv(directory_train)
 train_data.head(10 )
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Embedding, Concatenate, Flatten, BatchNormalization, Dropout
from keras.callbacks import ModelCheckpoint
from keras.utils.vis_utils import plot_model<choose_model_class>
y_train = train_data["label"]
x_train = train_data.drop(['label'],axis=1)
 y_train = to_categorical(y_train,num_classes=10)
 x_test = test_data.drop(['id'], axis=1 )
folds = 10
seed = 1997
kf = KFold(n_splits = folds, shuffle = True, random_state=seed)
 models_1, models_2 = [], []
def give_me_model_1() :
numerical_inputs = Input(shape=(X_train.shape[1],))
x = Dense(X_train.shape[1], activation='relu' )(numerical_inputs)
 x = BatchNormalization()(x)
 logits1 = Dense(512,activation='relu' )(x)
 logits = Concatenate()([x, logits1])
 logits = Dropout(0.3 )(logits)
 logits1 = Dense(512,activation='relu' )(logits)
 logits = Concatenate()([logits, logits1])
 logits = Dropout(0.2 )(logits)
 logits1 = Dense(256,activation='relu' )(logits)
 logits1 = Concatenate()([logits, logits1])
 logits1 = Dropout(0.1 )(logits1)
 logits1 = Dense(256,activation='relu' )(logits1)
 out = Dense(199, activation='sigmoid' )(logits1)
 model = Model(inputs = numerical_inputs, outputs=out)
 model.compile(optimizer='adam',loss='mse')
 return model
def give_me_model_2() :
numerical_inputs = Input(shape=(X_train.shape[1],))
x = Dense(X_train.shape[1], activation='relu' )(numerical_inputs)
 x = BatchNormalization()(x)
 logits = Dense(512,activation='relu' )(x)
 logits = Dropout(0.3 )(logits)
 logits = Dense(256,activation='relu' )(logits)
 logits = Dropout(0.2 )(logits)
 logits = Dense(256,activation='relu' )(logits)
 out = Dense(199, activation='sigmoid' )(logits)
 model = Model(inputs = numerical_inputs, outputs=out)
 model.compile(optimizer='adam',loss='mse')
 return model
for tr_idx, val_idx in kf.split(X_train, y_train):
tr_x, tr_y = X_train[tr_idx,:], y_train[tr_idx]
vl_x, vl_y = X_train[val_idx,:], y_train[val_idx]
checkpoint_1 = ModelCheckpoint('keras_model_1.hdf5', monitor='val_loss', verbose = 1, save_best_only = True, mode ='auto')
 callbacks_list_1 = [checkpoint_1]
checkpoint_2 = ModelCheckpoint('keras_model_2.hdf5', monitor='val_loss', verbose = 1, save_best_only = True, mode ='auto')
 callbacks_list_2 = [checkpoint_2]
model_1 = give_me_model_1()
model_2 = give_me_model_2()
model_1.fit(tr_x, tr_y,
epochs=10,
verbose=0,
callbacks=callbacks_list_1,
validation_data=(vl_x, vl_y))
model_2.fit(tr_x, tr_y,
epochs=10,
verbose=0,
callbacks=callbacks_list_2,
validation_data=(vl_x, vl_y))
model_1.load_weights('keras_model_1.hdf5')
 model_2.load_weights('keras_model_2.hdf5')
 models_1.append(model_1)
 models_2.append(model_2 )<compute_test_metric>
x_train /= 255.0
x_test /= 255.0
x_train = x_train.values.reshape(x_train.shape[0],28, 28,1)
 x_test = x_test.values.reshape(x_test.shape[0],28, 28,1)
<define_variables>
def separating_data() :
return train_test_split(x_train,
y_train,
test_size = 0.05,
random_state = 42 
)
 x_train, x_validation, y_train, y_validation = train_test_split(x_train,
y_train,
test_size = 0.05,
random_state = 42 )
plot_model(model_1, to_file='model_plot_1.png', show_shapes=True )<define_variables>
max_epochs = 2
model = Sequential()
activations = ['relu', 'softmax']
model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation = activations[0],
input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation = activations[0]))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64,
kernel_size =(3,3),
padding = 'Same',
activation = activations[0]))
model.add(Conv2D(filters = 64,
kernel_size =(3,3),
padding = 'Same',
activation = activations[0]))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = activations[0]))
model.add(Dropout(0.5))
model.add(Dense(10, activation = activations[1]))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=["accuracy"] )
plot_model(model_2, to_file='model_plot_2.png', show_shapes=True )<categorify>
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
for df, sample in tqdm.tqdm(env.iter_test()):
df.drop(['Season', 'DefensePersonnel', 'Temperature', 'Humidity'], axis = 1, inplace = True)
 df['OffenseFormation'] = df['OffenseFormation'].apply(lambda x: x if x in off_form else np.nan)
 df = pd.concat([df.drop(['OffenseFormation'], axis=1), pd.get_dummies(df['OffenseFormation'], prefix='Formation')], axis=1)
 missing_cols = set(dummy_col)- set(df.columns)-set('Yards')
 for c in missing_cols:
df[c] = 0
df = df[dummy_col]
df.drop(['Yards'], axis=1, inplace=True)
 df = fe(df)
 X = np.array(df[players_col] ).reshape(-1,(len(players_col)) *22)
 play_col = df.drop(players_col, axis=1 ).columns
X_play_col = np.zeros(shape=(X.shape[0], len(play_col)))
 for i, col in enumerate(play_col):
X_play_col[:, i] = df[col][::22]
X = np.concatenate([X, X_play_col], axis=1)
 y_pred_1 = np.sum([model.predict(X)for model in models_1], axis=0)/ kf.n_splits
y_pred_2 = np.sum([model.predict(X)for model in models_2], axis=0)/ kf.n_splits
y_pred = y_pred_1 * 0.5 + y_pred_2 * 0.5
for pred in y_pred:
prev = 0
for i in range(len(pred)) :
if pred[i]<prev:
pred[i]=prev
prev=pred[i]
env.predict(pd.DataFrame(data=y_pred,columns=sample.columns))
env.write_submission_file()<install_modules>
generator = ImageDataGenerator()
batches = generator.flow(x_train,
y_train,
batch_size=32)
 batches_value = generator.flow(x_validation,
y_validation,
batch_size=32)
 history = model.fit_generator(generator=batches,
steps_per_epoch=batches.n,
epochs=max_epochs,
validation_data=batches_value,
validation_steps=batches_value.n )
<load_pretrained><EOS> 
my_predictions = model.predict_classes(x_test)
 submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = my_predictions
submission.to_csv("submission.csv",index=False )