Kaggle competition: Kannada MNIST
learn.fit_one_cycle(1, np.array([1e-3, 1e-4, 1e-5]))<save_model>
import numpy as np
import pandas as pd
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout,Conv2D,MaxPool2D,Flatten,BatchNormalization
from keras.optimizers import Adam,Nadam
from sklearn.model_selection import train_test_split
from keras import regularizers
from keras.preprocessing.image import ImageDataGenerator
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
learn.save('phase-3' )<load_pretrained>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 submission = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 display(train.shape,test.shape )
learn.load('phase-3' )<feature_engineering>
Id = test['id']
learn.dbunch = dbch_test<predict_on_test>
x_train, x_test, y_train, y_test = train_test_split(train.iloc[:, 1:], train.iloc[:, 0], test_size=0.05)
 display(x_train.shape,y_train.shape,x_test.shape,y_test.shape )
preds, targs = learn.get_preds()<define_variables>
x_train, y_train = shuffle(x_train, y_train )
ids = []
labels = []
for path, pred in zip(test_paths, preds):
for i, label in enumerate(classes):
ids.append(f"{path.name.split('.')[0]}_{label}")
 predicted_probability = '{0:1.10f}'.format(pred[i].item())
 labels.append(predicted_probability )<save_to_csv>
x_train = x_train.values.reshape(-1,28,28,1)/255.0
x_test = x_test.values.reshape(-1,28,28,1)/255.0
y_train = np_utils.to_categorical(y_train,num_classes=10)
 y_test = np_utils.to_categorical(y_test,num_classes=10 )
pd.DataFrame({'ID': ids, 'Label': labels} ).to_csv(f'submission.csv', index=False )<define_variables>
imagegen = ImageDataGenerator(
 rotation_range = 10,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 20,
zoom_range = 0.2 
)
 imagegen.fit(x_train )
start_time = datetime.datetime.now()
demographic_cols = ['fecha_dato',
'ncodpers','ind_empleado','pais_residencia','sexo','age','fecha_alta','ind_nuevo','antiguedad','indrel',
'indrel_1mes','tiprel_1mes','indresi','indext','conyuemp','canal_entrada','indfall',
'tipodom','cod_prov','ind_actividad_cliente','renta','segmento']
notuse = ["ult_fec_cli_1t","nomprov"]
product_col = [
'ind_ahor_fin_ult1','ind_aval_fin_ult1','ind_cco_fin_ult1','ind_cder_fin_ult1','ind_cno_fin_ult1','ind_ctju_fin_ult1',
'ind_ctma_fin_ult1','ind_ctop_fin_ult1','ind_ctpp_fin_ult1','ind_deco_fin_ult1','ind_deme_fin_ult1',
'ind_dela_fin_ult1','ind_ecue_fin_ult1','ind_fond_fin_ult1','ind_hip_fin_ult1','ind_plan_fin_ult1',
'ind_pres_fin_ult1','ind_reca_fin_ult1','ind_tjcr_fin_ult1','ind_valo_fin_ult1','ind_viv_fin_ult1','ind_nomina_ult1',
'ind_nom_pens_ult1','ind_recibo_ult1']
train_cols = demographic_cols + product_col<load_from_csv>
model = Sequential()
model.add(Conv2D(input_shape=(28, 28, 1),filters=64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.5))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.5))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.5))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dense(10, activation = "softmax"))
model.compile(optimizer=Nadam() ,loss='categorical_crossentropy',metrics=['accuracy'])
 fit = model.fit_generator(imagegen.flow(x_train,y_train,batch_size=128),epochs=80,validation_data=(x_test,y_test),verbose = 1,steps_per_epoch=100)
 loss,accuracy = model.evaluate(x_test,y_test)
 print('test loss',loss)
 print('test accuracy',accuracy )
df_train = pd.read_csv('.. /input/makejuneextradata-fff8dd/juneExtra.csv', usecols=train_cols )<load_from_csv>
test = test.drop(['id'],axis=1)
 test = test.values.reshape(test.shape[0],28,28,1)/255.0
FINAL_PREDS = model.predict_classes(test )
df_test = pd.read_csv('.. /input/santander-product-recommendation/test_ver2.csv', usecols = demographic_cols )<filter>
submission = pd.DataFrame({ 'id': Id,
'label': FINAL_PREDS })
 submission.to_csv(path_or_buf ="submission.csv", index=False)
 submission.head()