{"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-27T15:26:59.028070Z","iopub.execute_input":"2022-08-27T15:26:59.029121Z","iopub.status.idle":"2022-08-27T15:26:59.058986Z","shell.execute_reply.started":"2022-08-27T15:26:59.029027Z","shell.execute_reply":"2022-08-27T15:26:59.058089Z"},"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\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten,Conv2D , MaxPool2D \nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers.experimental.preprocessing import Normalization\nfrom tensorflow.keras.optimizers import Adam\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:26:59.061012Z","iopub.execute_input":"2022-08-27T15:26:59.061681Z","iopub.status.idle":"2022-08-27T15:27:05.324434Z","shell.execute_reply.started":"2022-08-27T15:26:59.061644Z","shell.execute_reply":"2022-08-27T15:27:05.323425Z"},"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-27T15:27:05.325718Z","iopub.execute_input":"2022-08-27T15:27:05.326748Z","iopub.status.idle":"2022-08-27T15:27:09.421082Z","shell.execute_reply.started":"2022-08-27T15:27:05.326709Z","shell.execute_reply":"2022-08-27T15:27:09.420050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:09.423698Z","iopub.execute_input":"2022-08-27T15:27:09.424186Z","iopub.status.idle":"2022-08-27T15:27:09.456091Z","shell.execute_reply.started":"2022-08-27T15:27:09.424150Z","shell.execute_reply":"2022-08-27T15:27:09.455056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:09.457645Z","iopub.execute_input":"2022-08-27T15:27:09.458108Z","iopub.status.idle":"2022-08-27T15:27:09.482560Z","shell.execute_reply.started":"2022-08-27T15:27:09.458071Z","shell.execute_reply":"2022-08-27T15:27:09.481685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing = train.isna().sum()>0\nmissing.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:09.484913Z","iopub.execute_input":"2022-08-27T15:27:09.485256Z","iopub.status.idle":"2022-08-27T15:27:09.551052Z","shell.execute_reply.started":"2022-08-27T15:27:09.485219Z","shell.execute_reply":"2022-08-27T15:27:09.550046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing1 = test.isna().sum()>0\nmissing1.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:09.552454Z","iopub.execute_input":"2022-08-27T15:27:09.552834Z","iopub.status.idle":"2022-08-27T15:27:09.568748Z","shell.execute_reply.started":"2022-08-27T15:27:09.552782Z","shell.execute_reply":"2022-08-27T15:27:09.567825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train.drop(\"label\", axis=1)\ny_train = train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:09.570439Z","iopub.execute_input":"2022-08-27T15:27:09.570870Z","iopub.status.idle":"2022-08-27T15:27:09.688467Z","shell.execute_reply.started":"2022-08-27T15:27:09.570836Z","shell.execute_reply":"2022-08-27T15:27:09.686821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:09.690229Z","iopub.execute_input":"2022-08-27T15:27:09.691084Z","iopub.status.idle":"2022-08-27T15:27:09.701733Z","shell.execute_reply.started":"2022-08-27T15:27:09.691049Z","shell.execute_reply":"2022-08-27T15:27:09.700718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Preparation**","metadata":{}},{"cell_type":"code","source":"x_train = train.values[:, 1:].reshape((-1, 28, 28, 1)) / 255.\ny_train = tf.keras.utils.to_categorical(train.values[:, 0], 10)\nx_test = test.values[:, 1:].reshape((-1, 28, 28, 1)) / 255.","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:29.876169Z","iopub.execute_input":"2022-08-27T15:27:29.876544Z","iopub.status.idle":"2022-08-27T15:27:30.039889Z","shell.execute_reply.started":"2022-08-27T15:27:29.876513Z","shell.execute_reply":"2022-08-27T15:27:30.038910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(9):\n    plt.subplot(330+1+i)\n    plt.imshow(x_train[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:30.666992Z","iopub.execute_input":"2022-08-27T15:27:30.667966Z","iopub.status.idle":"2022-08-27T15:27:31.332876Z","shell.execute_reply.started":"2022-08-27T15:27:30.667920Z","shell.execute_reply":"2022-08-27T15:27:31.330933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:31.334694Z","iopub.execute_input":"2022-08-27T15:27:31.335264Z","iopub.status.idle":"2022-08-27T15:27:31.871432Z","shell.execute_reply.started":"2022-08-27T15:27:31.335226Z","shell.execute_reply":"2022-08-27T15:27:31.870337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model evaluation and accuracy**","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n                            tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(28,28,1)),\n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'),\n                            tf.keras.layers.MaxPooling2D(2,2),\n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.Dropout(0.2),\n                            tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'), \n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'),\n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.MaxPooling2D(2,2),\n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.Dropout(0.2),\n                            tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'),\n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'),\n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.MaxPooling2D(2,2),\n                            tf.keras.layers.Dropout(0.2), \n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.Flatten(),\n                            tf.keras.layers.Dense(64, activation = 'relu', kernel_initializer='he_uniform'),\n                            tf.keras.layers.BatchNormalization(),\n                            tf.keras.layers.Dropout(0.2),\n                            tf.keras.layers.Dense(10, activation = 'softmax'),])\n                           \n\nmodel.compile(optimizer ='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nhistory = model.fit(x_train, y_train, epochs=10,validation_data=(x_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:27:33.032001Z","iopub.execute_input":"2022-08-27T15:27:33.032515Z","iopub.status.idle":"2022-08-27T15:29:59.669301Z","shell.execute_reply.started":"2022-08-27T15:27:33.032481Z","shell.execute_reply":"2022-08-27T15:29:59.668243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:30:01.884879Z","iopub.execute_input":"2022-08-27T15:30:01.885284Z","iopub.status.idle":"2022-08-27T15:30:02.096496Z","shell.execute_reply.started":"2022-08-27T15:30:01.885242Z","shell.execute_reply":"2022-08-27T15:30:02.095542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prediction and submission**","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:30:22.006530Z","iopub.execute_input":"2022-08-27T15:30:22.006917Z","iopub.status.idle":"2022-08-27T15:30:22.030547Z","shell.execute_reply.started":"2022-08-27T15:30:22.006885Z","shell.execute_reply":"2022-08-27T15:30:22.029621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(sub.label)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:30:52.233672Z","iopub.execute_input":"2022-08-27T15:30:52.234175Z","iopub.status.idle":"2022-08-27T15:30:52.242253Z","shell.execute_reply.started":"2022-08-27T15:30:52.234134Z","shell.execute_reply":"2022-08-27T15:30:52.241144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(x_test)\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"label\")","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:30:30.055219Z","iopub.execute_input":"2022-08-27T15:30:30.055601Z","iopub.status.idle":"2022-08-27T15:30:30.616707Z","shell.execute_reply.started":"2022-08-27T15:30:30.055569Z","shell.execute_reply":"2022-08-27T15:30:30.615666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,5001),name = \"id\"),results],axis = 1)\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T15:31:06.723610Z","iopub.execute_input":"2022-08-27T15:31:06.723997Z","iopub.status.idle":"2022-08-27T15:31:06.738544Z","shell.execute_reply.started":"2022-08-27T15:31:06.723966Z","shell.execute_reply":"2022-08-27T15:31:06.737584Z"},"trusted":true},"execution_count":null,"outputs":[]}]}