Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<init_hyperparams>
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
print(f'Training Set Shape = {df_train.shape} - Patients = {df_train["Patient"].nunique() }')
 print(f'Training Set Memory Usage = {df_train.memory_usage().sum() / 1024 ** 2:.2f} MB')
 print(f'Test Set Shape = {df_test.shape} - Patients = {df_test["Patient"].nunique() }')
 print(f'Test Set Memory Usage = {df_test.memory_usage().sum() / 1024 ** 2:.2f} MB' )<groupby>
train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 holdout = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )
training_sample_counts = df_train.rename(columns={'Weeks': 'Samples'} ).groupby('Patient' ).agg('count')['Samples'].value_counts()
print(f'Training Set FVC Measurements Per Patient
{("-")* 41}
{training_sample_counts}' )<load_from_csv>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 X_test.drop(labels=['id'], axis=1, inplace=True)
 X_test = X_test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10)
 len_test = len(X_test )
df_submission = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv')
 df_submission.head()<set_options>
X_holdout = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 y_holdout = X_holdout['label']
X_holdout.drop(labels=['label'], axis=1, inplace=True)
 X_holdout = X_holdout.astype('float32')/ 255.
 X_holdout = X_holdout.values.reshape(X_holdout.shape[0], 28, 28, 1 ).astype('float32' )
warnings.filterwarnings("ignore" )<define_variables>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )
def seed_everything(seed=777):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed )<define_variables>
nets = 4
model = [0] *nets
for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size = 4, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
OUTPUT_DICT = './'
ID = 'Patient_Week'
TARGET = 'FVC'
SEED = 777
seed_everything(seed=SEED)
 N_FOLD = 7<load_from_csv>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * nets
epochs = 45
for j in range(nets):
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size=64),
epochs = epochs, steps_per_epoch = X_train2.shape[0]//64,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 j+1,epochs,max(history[j].history['accuracy']),max(history[j].history['val_accuracy'])) )
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv')
 otest = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv' )<create_dataframe>
from sklearn.metrics import accuracy_score
train = pd.concat([train,otest])
 output = pd.DataFrame()
gb = train.groupby('Patient')
 tk0 = tqdm(gb, total=len(gb))
for _, usr_df in tk0:
usr_output = pd.DataFrame()
for week, tmp in usr_df.groupby('Weeks'):
rename_cols = {'Weeks': 'base_Week', 'FVC': 'base_FVC', 'Age': 'base_Age'}
tmp = tmp.rename(columns=rename_cols)
 drop_cols = ['Age', 'Sex', 'SmokingStatus', 'Percent']
_usr_output = usr_df.drop(columns=drop_cols ).rename(columns={'Weeks': 'predict_Week'} ).merge(tmp, on='Patient')
 _usr_output['Week_passed'] = _usr_output['predict_Week'] - _usr_output['base_Week']
usr_output = pd.concat([usr_output, _usr_output])
 output = pd.concat([output, usr_output])
 train = output[output['Week_passed']!=0].reset_index(drop=True )<feature_engineering>
results = np.zeros(( X_test.shape[0],10))
results_holdout = np.zeros(( X_holdout.shape[0],10))
for j in range(nets):
results = results + model[j].predict(X_test)
 results_holdout = results_holdout + model[j].predict(X_holdout)
 results_holdout = np.argmax(results_holdout,axis = 1)
 holdout_accuracy = accuracy_score(y_holdout, results_holdout)
 print(" Holdout Accuracy = %3.4f"%(holdout_accuracy))
results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="label")
 submission = pd.concat([pd.Series(range(0,len_test),name = "id"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )