{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.layers import Flatten\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.optimizers import Adam\nfrom keras.utils import np_utils\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import classification_report,confusion_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Read and seperate data and labels/IDs\ntrain_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntrain_Y = train_data['label']\ntrain_data = train_data.drop(columns=['label'])\ntest_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ntest_id = test_data['id']\ntest_data = test_data.drop(columns=['id'])\ntrain_X = train_data.values\ntest_X = test_data.values\nprint(train_X.shape, train_Y.shape, test_X.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"qw = 5\nplt.imshow(train_X[qw,:].reshape(28,28),cmap='gray')\nplt.title(train_Y[qw])\nplt.xticks([])\nplt.yticks([])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Number of each class\ntrain_Y.value_counts(sort=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Restructure data\nnum = 28\ntrain_X_re = train_X.reshape((-1, num, num, 1)).astype('float32')  \ntest_X_re = test_X.reshape((-1, num, num, 1)).astype('float32')\nprint(train_X_re.shape, test_X_re.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#One hot encode\nnumber_of_classes = 10\ntrain_Y = np_utils.to_categorical(train_Y, number_of_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# normalize inputs from 0-255 to 0-1\ntrain_X_re/=255\ntest_X_re/=255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create model\nmodel = Sequential()\nmodel.add(Conv2D(32, (5, 5), input_shape=(train_X_re.shape[1], train_X_re.shape[2], 1), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(32, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(number_of_classes, activation='softmax'))\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Compile model\nmodel.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Train Test split\nfrom sklearn.model_selection import train_test_split\ntrain_X_t,train_X_ts,train_Y_t,train_Y_ts = train_test_split(train_X_re, train_Y,test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_X_t.shape)\nprint(train_Y_t.shape)\nprint(train_X_ts.shape)\nprint(train_Y_ts.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fit the model\nmodel_result=model.fit(train_X_t, train_Y_t, validation_data=(train_X_ts, train_Y_ts), epochs=15, batch_size=200)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot and visualise the training and validation losses\nplt.figure(figsize=(20, 10))\n\nplt.subplot(1, 2, 1)\nplt.plot(model_result.history[\"loss\"], label=\"training\")\nplt.plot(model_result.history[\"val_loss\"], label=\"validation\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(model_result.history[\"acc\"], label=\"training\")\nplt.plot(model_result.history[\"val_acc\"], label=\"validation\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Create Classification_report and Confusion Matrix\nY_pred = model.predict_classes(train_X_re)\ntarget_names = ['0','1','2','3','4','5','6','7','8','9']\nprint(classification_report(np.argmax(train_Y,axis=1), Y_pred, target_names=target_names))\ncm = confusion_matrix(np.argmax(train_Y,axis=1), Y_pred)\n\n\nfig, ax = plt.subplots(figsize=(8,8))\nim = ax.imshow(cm)\n\n# We want to show all ticks...\nax.set_xticks(np.arange(len(target_names)))\nax.set_yticks(np.arange(len(target_names)))\n# ... and label them with the respective list entries\nax.set_xticklabels(target_names)\nax.set_yticklabels(target_names)\n\n# Rotate the tick labels and set their alignment.\nplt.setp(ax.get_xticklabels(), ha=\"center\",va=\"center\", color=\"w\")\n\n# Loop over data dimensions and create text annotations.\nfor i in range(len(target_names)):\n    for j in range(len(target_names)):\n        text = ax.text(j, i, cm[i, j],\n                       ha=\"center\", va=\"center\", color=\"w\")\n\nax.set_title(\"Confusion Matrix\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# predict on test set\npredictions = model.predict_classes(test_X_re)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create submission file\nsubmission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission['label'] = predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# generate submission file in csv format\nsubmission.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":1}