{"cells":[{"metadata":{"id":"3ODX9F153Vhi","colab_type":"code","outputId":"13aca76f-c1ff-4d5f-ffbd-c1d5d6905455","colab":{"base_uri":"https://localhost:8080/","height":34},"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","execution_count":null,"outputs":[]},{"metadata":{"id":"zkoL-ckM-Ku_","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import pandas as pd\n\ntrain_sample = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"sH7e2qjyWDhS","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"first_img = train_sample.iloc[0, 1:].values","execution_count":null,"outputs":[]},{"metadata":{"id":"-Jrv7YChSdmC","colab_type":"code","outputId":"f1bba1b8-783e-4943-e337-e833687cdb57","colab":{"base_uri":"https://localhost:8080/","height":265},"trusted":true},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\nw, h = 28, 28\ndata = np.array(first_img).reshape((w, h))\nplt.imshow(data, interpolation='nearest')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"q3-W1ozHPiAn","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":419},"outputId":"18dcce40-5fe5-4662-bff0-45cd9fd04416","trusted":true},"cell_type":"code","source":"Y_train = train_sample.iloc[:,  :1]\nX_train = train_sample.iloc[:, 1:]\n\nY_train","execution_count":null,"outputs":[]},{"metadata":{"id":"9-t3p7DsQNH4","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\n\nscaler = MinMaxScaler(feature_range=(-1, 1))\nX_train_scaled = scaler.fit_transform(X_train)","execution_count":null,"outputs":[]},{"metadata":{"id":"W1wZT0a4YfUc","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"12e8b047-7b44-4c09-a924-997935f31f72","trusted":true},"cell_type":"code","source":"import tensorflow.keras as keras\nfrom keras import layers\nfrom keras.layers import *\nfrom keras.models import Model\nfrom keras.callbacks import CSVLogger, ModelCheckpoint","execution_count":null,"outputs":[]},{"metadata":{"id":"sFeuLv7PYmu6","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":323},"outputId":"632a3474-4148-4dd5-abca-438c855ee3eb","trusted":true},"cell_type":"code","source":"x = Input(shape=(784,))\ny = Dense(20, activation=None)(x)\ny = Activation('elu')(y)\ny = Dropout(rate=0.3)(y)\nprediction = Dense(10, activation='softmax')(y)\n\nmodel = Model(inputs=[x], output=[prediction])\n\nmodel.compile(optimizer=keras.optimizers.SGD(),\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"id":"neLXrIDrakuh","colab_type":"code","outputId":"81bb0ee4-6dcd-4f04-8380-2a8642f076a5","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"Y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"id":"jCY6XpjdZIkG","colab_type":"code","outputId":"44998867-57e8-4178-d0fa-8550869d8f24","colab":{"base_uri":"https://localhost:8080/","height":615},"trusted":true},"cell_type":"code","source":"model.fit(\n    X_train_scaled,\n    Y_train, \n    epochs=15,\n    batch_size=20,\n    callbacks=[\n        CSVLogger('log.csv'),\n        ModelCheckpoint('model.h1', save_best_only=True)\n    ]\n)","execution_count":null,"outputs":[]},{"metadata":{"id":"6SIY7SFXaKO1","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"417e419c-3025-45d3-9837-02041284cb36","trusted":true},"cell_type":"code","source":"test_sample = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\n\nX_test = test_sample.iloc[:, 1:]\nX_test = scaler.transform(X_test)\n\nX_test.shape","execution_count":null,"outputs":[]},{"metadata":{"id":"htT0drEDDvUG","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":204},"outputId":"15a47167-3104-44f0-d0c1-010ba856b467","trusted":true},"cell_type":"code","source":"pred_probas = model.predict(X_test, batch_size=16)\n\nprediction = pred_probas.argmax(axis=1)\n\nresult = pd.DataFrame()\nresult['id'] = list(range(0, len(prediction)))\nresult['label'] = prediction\n\nprint(result.shape)\nresult.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"aVFsqmffG9YJ","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"result.to_csv(\"submission.csv\", index = False)","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"NS-1hw.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}