{"cells":[{"metadata":{"id":"Bzk5pJ15B1XO","outputId":"6978c28b-9b9d-498a-cdc1-9c447a16f720","trusted":true},"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\ntest=test.drop('id',axis=1)","execution_count":null,"outputs":[]},{"metadata":{"id":"VuAZf-kFhtcd","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx = train.drop('label',axis=1)\ny = train.label\nx_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size=0.15, random_state=42,shuffle=False)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"RAM3QnN-H2C-","trusted":true},"cell_type":"code","source":"from keras.utils import normalize\nfrom keras.utils.np_utils import to_categorical\n\nx_train=x_train/255\nx_valid=x_valid/255\ntest=test/255\nx_train=x_train.values.reshape(-1,28,28,1)\nx_valid=x_valid.values.reshape(-1,28,28,1)\ntest=test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"id":"vRB6W_HqAvC8","outputId":"ad194921-12e9-49c6-c935-ce1ffb4625c6","trusted":true},"cell_type":"code","source":"y_train","execution_count":null,"outputs":[]},{"metadata":{"id":"nC_h9musCQ8l","outputId":"10dd6c9e-11bf-4ea5-c241-941a6b1648ed","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(20,10))\nfor i in range(21):\n  plt.subplot(3,7,i+1)\n  plt.imshow(x_train[i][:,:,0], cmap='binary')\n  plt.title(y_train[i])","execution_count":null,"outputs":[]},{"metadata":{"id":"_Rvuci1DNC85","trusted":true},"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_valid = to_categorical(y_valid)","execution_count":null,"outputs":[]},{"metadata":{"id":"hT2eUlJpPfTV","trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=0.1,  # randomly flip images\n        vertical_flip=0.1)  # randomly flip images\ndatagen.fit(x_train)","execution_count":null,"outputs":[]},{"metadata":{"id":"HmrSHFBKGSQb","outputId":"a433b56d-e2ee-4025-fe39-1f364b0b7d90","trusted":true},"cell_type":"code","source":"x_valid[2][15]","execution_count":null,"outputs":[]},{"metadata":{"id":"4BDQUNz2C5jz","outputId":"bd88b3ab-2d52-4077-aaae-d27580226049","trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, BatchNormalization, MaxPool2D, Dropout\nfrom keras.callbacks import ReduceLROnPlateau\nimport warnings\nwarnings.filterwarnings('ignore')\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='acc', patience=3, verbose=1, factor=0.5, min_lr=0.000001)\nepochs = 10\nmodel = Sequential()\nmodel.add(Conv2D(filters = 120, kernel_size = (5,5),activation ='relu', input_shape = (28,28,1), padding='Same'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters = 120, kernel_size = (5,5),activation ='relu', padding='Same'))\nmodel.add(BatchNormalization(momentum=.15))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(100, activation = \"relu\"))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(10, activation='softmax'))\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nhistory = model.fit(x_train,y_train,epochs=epochs,callbacks=[learning_rate_reduction],validation_data=(x_valid, y_valid),batch_size=200)","execution_count":null,"outputs":[]},{"metadata":{"id":"D5X204tnGD88","outputId":"e344ce2b-711e-4e5f-ff69-2f05079524e1","trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,8))\nfig,(ax1, ax2)=plt.subplots(1,2,figsize=(20,5))\nx=range(1,1+epochs)\nax1.plot(x,history.history['loss'],color='red')\nax1.plot(x,history.history['val_loss'],color='blue')\nax1.legend(['trainng loss','validation loss'])\nax2.plot(x,history.history['accuracy'],color='red')\nax2.plot(x,history.history['val_accuracy'],color='blue')\nax2.legend(['trainng acc','validation acc'])\nax1.set_xlabel('Number of epochs')\nax1.set_ylabel('accuracy')\nax2.set_xlabel('Number of epochs')\nax2.set_ylabel('loss')","execution_count":null,"outputs":[]},{"metadata":{"id":"_8aM32FyIyh4","trusted":true},"cell_type":"code","source":"import numpy as np\npredicted = model.predict(test)\nsubmit=np.argmax(predicted,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"id":"lIM7WmjZ4oKZ","trusted":true},"cell_type":"code","source":"submit = pd.DataFrame(submit)\nsubmit['id'] = submit.index\nsubmit.columns = ['label', 'id']\nsubmit = submit[['id','label']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}