{"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_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-29T12:39:43.203704Z","iopub.execute_input":"2022-08-29T12:39:43.204584Z","iopub.status.idle":"2022-08-29T12:39:43.230234Z","shell.execute_reply.started":"2022-08-29T12:39:43.204486Z","shell.execute_reply":"2022-08-29T12:39:43.229258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:39:43.232179Z","iopub.execute_input":"2022-08-29T12:39:43.232855Z","iopub.status.idle":"2022-08-29T12:39:49.590472Z","shell.execute_reply.started":"2022-08-29T12:39:43.232819Z","shell.execute_reply":"2022-08-29T12:39:49.589216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest  = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:44:19.097493Z","iopub.execute_input":"2022-08-29T12:44:19.098432Z","iopub.status.idle":"2022-08-29T12:44:22.064992Z","shell.execute_reply.started":"2022-08-29T12:44:19.098390Z","shell.execute_reply":"2022-08-29T12:44:22.063934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:44:22.067180Z","iopub.execute_input":"2022-08-29T12:44:22.067590Z","iopub.status.idle":"2022-08-29T12:44:22.084350Z","shell.execute_reply.started":"2022-08-29T12:44:22.067551Z","shell.execute_reply":"2022-08-29T12:44:22.083039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:44:22.086727Z","iopub.execute_input":"2022-08-29T12:44:22.087086Z","iopub.status.idle":"2022-08-29T12:44:22.106861Z","shell.execute_reply.started":"2022-08-29T12:44:22.087049Z","shell.execute_reply":"2022-08-29T12:44:22.105979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.values[:, 1:].reshape((-1,28,28,1)) / 255.\ny = tf.keras.utils.to_categorical(train.values[:, 0], 10)\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:45:17.373572Z","iopub.execute_input":"2022-08-29T12:45:17.374156Z","iopub.status.idle":"2022-08-29T12:45:17.530098Z","shell.execute_reply.started":"2022-08-29T12:45:17.374121Z","shell.execute_reply":"2022-08-29T12:45:17.529004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training and Prediction","metadata":{}},{"cell_type":"code","source":"for i in range(9):\n    plt.subplot(331+i)\n    plt.imshow(X[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:46:40.448278Z","iopub.execute_input":"2022-08-29T12:46:40.449193Z","iopub.status.idle":"2022-08-29T12:46:41.096271Z","shell.execute_reply.started":"2022-08-29T12:46:40.449145Z","shell.execute_reply":"2022-08-29T12:46:41.095373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=tf.keras.Sequential([\n                           tf.keras.layers.Conv2D(32,(3,3),activation=\"relu\",input_shape=(28,28,1)),\n                           tf.keras.layers.BatchNormalization(),\n                           tf.keras.layers.Conv2D(64,(3,3),activation=\"relu\"),\n                           tf.keras.layers.MaxPool2D(2,2),\n                           tf.keras.layers.BatchNormalization(),\n                           tf.keras.layers.Flatten(),\n                           tf.keras.layers.Dense(100,activation=\"relu\"),\n                           tf.keras.layers.BatchNormalization(),\n                           tf.keras.layers.Dense(10,activation=\"softmax\")]) \n\nmodel.compile(optimizer=tf.keras.optimizers.SGD(lr=0.01,momentum=0.9),loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])\nhistory=model.fit(X, y, validation_split=0.2 ,batch_size=32,epochs=5,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:48:55.858067Z","iopub.execute_input":"2022-08-29T12:48:55.858471Z","iopub.status.idle":"2022-08-29T12:51:18.813259Z","shell.execute_reply.started":"2022-08-29T12:48:55.858440Z","shell.execute_reply":"2022-08-29T12:51:18.812201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline \nplt.plot(history.history[\"accuracy\"])\nplt.plot(history.history[\"val_accuracy\"])\nplt.title(\"Modelaccuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.legend([\"Train\",\"Test\"],loc=\"upper left\")\nplt.show()\n\nplt.plot(history.history[\"loss\"])\nplt.plot(history.history[\"val_loss\"])\nplt.title(\"Model loss\")\nplt.ylabel(\"loss\")\nplt.xlabel(\"Epoch\")\nplt.legend([\"Train\",\"Test\"],loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:51:18.815828Z","iopub.execute_input":"2022-08-29T12:51:18.816215Z","iopub.status.idle":"2022-08-29T12:51:19.262353Z","shell.execute_reply.started":"2022-08-29T12:51:18.816177Z","shell.execute_reply":"2022-08-29T12:51:19.261276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"pred=model.predict(test.values[:, 1:].reshape((-1,28,28,1)) / 255.)\nclasses = pred.argmax(axis=-1)\nclasses","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:52:35.715651Z","iopub.execute_input":"2022-08-29T12:52:35.716017Z","iopub.status.idle":"2022-08-29T12:52:36.104174Z","shell.execute_reply.started":"2022-08-29T12:52:35.715985Z","shell.execute_reply":"2022-08-29T12:52:36.103232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission['label']=classes\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T12:53:44.213563Z","iopub.execute_input":"2022-08-29T12:53:44.214329Z","iopub.status.idle":"2022-08-29T12:53:44.246142Z","shell.execute_reply.started":"2022-08-29T12:53:44.214277Z","shell.execute_reply":"2022-08-29T12:53:44.245017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}