{"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":"import tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-09T16:30:23.628746Z","iopub.execute_input":"2023-01-09T16:30:23.629720Z","iopub.status.idle":"2023-01-09T16:30:23.634944Z","shell.execute_reply.started":"2023-01-09T16:30:23.629680Z","shell.execute_reply":"2023-01-09T16:30:23.634024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:30:29.555962Z","iopub.execute_input":"2023-01-09T16:30:29.556347Z","iopub.status.idle":"2023-01-09T16:30:29.570946Z","shell.execute_reply.started":"2023-01-09T16:30:29.556315Z","shell.execute_reply":"2023-01-09T16:30:29.569711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:30:50.356416Z","iopub.execute_input":"2023-01-09T16:30:50.356821Z","iopub.status.idle":"2023-01-09T16:30:53.803659Z","shell.execute_reply.started":"2023-01-09T16:30:50.356788Z","shell.execute_reply":"2023-01-09T16:30:53.802647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:31:00.509146Z","iopub.execute_input":"2023-01-09T16:31:00.509639Z","iopub.status.idle":"2023-01-09T16:31:00.522742Z","shell.execute_reply.started":"2023-01-09T16:31:00.509594Z","shell.execute_reply":"2023-01-09T16:31:00.521264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:31:11.756503Z","iopub.execute_input":"2023-01-09T16:31:11.756892Z","iopub.status.idle":"2023-01-09T16:31:11.780300Z","shell.execute_reply.started":"2023-01-09T16:31:11.756861Z","shell.execute_reply":"2023-01-09T16:31:11.779134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Separating features\nX = df.iloc[:,1:]\ny = df.iloc[:, 0]","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:31:32.058606Z","iopub.execute_input":"2023-01-09T16:31:32.059021Z","iopub.status.idle":"2023-01-09T16:31:32.064541Z","shell.execute_reply.started":"2023-01-09T16:31:32.058988Z","shell.execute_reply":"2023-01-09T16:31:32.063496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:31:37.584999Z","iopub.execute_input":"2023-01-09T16:31:37.585396Z","iopub.status.idle":"2023-01-09T16:31:37.605664Z","shell.execute_reply.started":"2023-01-09T16:31:37.585365Z","shell.execute_reply":"2023-01-09T16:31:37.604697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:31:43.266920Z","iopub.execute_input":"2023-01-09T16:31:43.267325Z","iopub.status.idle":"2023-01-09T16:31:43.276117Z","shell.execute_reply.started":"2023-01-09T16:31:43.267291Z","shell.execute_reply":"2023-01-09T16:31:43.274693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check a handwritten digit\npic1 = np.array(X.loc[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:34:25.086470Z","iopub.execute_input":"2023-01-09T16:34:25.086907Z","iopub.status.idle":"2023-01-09T16:34:25.093080Z","shell.execute_reply.started":"2023-01-09T16:34:25.086836Z","shell.execute_reply":"2023-01-09T16:34:25.091958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pic1 = pic1.reshape(28,28)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:34:26.362835Z","iopub.execute_input":"2023-01-09T16:34:26.363228Z","iopub.status.idle":"2023-01-09T16:34:26.368203Z","shell.execute_reply.started":"2023-01-09T16:34:26.363196Z","shell.execute_reply":"2023-01-09T16:34:26.366999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pic1, cmap = 'gray')\nprint(\"The image label is \", y[13])","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:35:48.332588Z","iopub.execute_input":"2023-01-09T16:35:48.333043Z","iopub.status.idle":"2023-01-09T16:35:48.377538Z","shell.execute_reply.started":"2023-01-09T16:35:48.333007Z","shell.execute_reply":"2023-01-09T16:35:48.376687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\"there should be an image of that digit here but somehow it doesn't appear..\"","metadata":{}},{"cell_type":"code","source":"#Data mapping\nX = X / 255","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:36:29.199566Z","iopub.execute_input":"2023-01-09T16:36:29.200744Z","iopub.status.idle":"2023-01-09T16:36:29.316282Z","shell.execute_reply.started":"2023-01-09T16:36:29.200701Z","shell.execute_reply":"2023-01-09T16:36:29.315299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Split the data into train and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state= 8)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:39:07.791899Z","iopub.execute_input":"2023-01-09T16:39:07.792314Z","iopub.status.idle":"2023-01-09T16:39:08.241496Z","shell.execute_reply.started":"2023-01-09T16:39:07.792280Z","shell.execute_reply":"2023-01-09T16:39:08.240009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Shapes of these train and test data.\nprint(\"shape of X_train: \", X_train.shape)\nprint(\"shape of y_train: \", y_train.shape)\nprint(\"shape of X_test: \", X_test.shape)\nprint(\"shape of y_test: \", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:39:23.597669Z","iopub.execute_input":"2023-01-09T16:39:23.598106Z","iopub.status.idle":"2023-01-09T16:39:23.605092Z","shell.execute_reply.started":"2023-01-09T16:39:23.598071Z","shell.execute_reply":"2023-01-09T16:39:23.603607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Building\nmodel = Sequential()\nmodel.add(Dense(128, activation = 'relu', input_dim = 784))\nmodel.add(Dense(32, activation = 'relu'))\nmodel.add(Dense(10, activation = 'softmax')) ","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:41:57.830391Z","iopub.execute_input":"2023-01-09T16:41:57.830830Z","iopub.status.idle":"2023-01-09T16:41:57.876029Z","shell.execute_reply.started":"2023-01-09T16:41:57.830795Z","shell.execute_reply":"2023-01-09T16:41:57.874833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:42:11.144946Z","iopub.execute_input":"2023-01-09T16:42:11.145351Z","iopub.status.idle":"2023-01-09T16:42:11.151988Z","shell.execute_reply.started":"2023-01-09T16:42:11.145315Z","shell.execute_reply":"2023-01-09T16:42:11.150681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = 'sparse_categorical_crossentropy', \n              optimizer = 'Adam', metrics = 'accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:42:44.602657Z","iopub.execute_input":"2023-01-09T16:42:44.603063Z","iopub.status.idle":"2023-01-09T16:42:44.617660Z","shell.execute_reply.started":"2023-01-09T16:42:44.603030Z","shell.execute_reply":"2023-01-09T16:42:44.616528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Training\nhs = model.fit(X_train, y_train, \n                    epochs = 20, validation_split= 0.25)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:43:26.745297Z","iopub.execute_input":"2023-01-09T16:43:26.746045Z","iopub.status.idle":"2023-01-09T16:44:49.671049Z","shell.execute_reply.started":"2023-01-09T16:43:26.746005Z","shell.execute_reply":"2023-01-09T16:44:49.669804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the change in loss\nplt.figure(figsize=(8,5))\nplt.xlabel('epochs')\nplt.plot(hs.history['loss'])\nplt.plot(hs.history['val_loss'])\n\n#Somehow both of the diagrams doesn't appear","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:46:05.354576Z","iopub.execute_input":"2023-01-09T16:46:05.354965Z","iopub.status.idle":"2023-01-09T16:46:05.487994Z","shell.execute_reply.started":"2023-01-09T16:46:05.354935Z","shell.execute_reply":"2023-01-09T16:46:05.486660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the change in accuracy\nplt.figure(figsize=(8,5))\nplt.xlabel('epochs')\nplt.plot(hs.history['accuracy'])\nplt.plot(hs.history['val_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:46:52.892302Z","iopub.execute_input":"2023-01-09T16:46:52.892701Z","iopub.status.idle":"2023-01-09T16:46:53.009356Z","shell.execute_reply.started":"2023-01-09T16:46:52.892669Z","shell.execute_reply":"2023-01-09T16:46:53.007867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction\ny_prob = model.predict(X_test)\ny_prob","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:47:51.308143Z","iopub.execute_input":"2023-01-09T16:47:51.308574Z","iopub.status.idle":"2023-01-09T16:47:51.953261Z","shell.execute_reply.started":"2023-01-09T16:47:51.308535Z","shell.execute_reply":"2023-01-09T16:47:51.951280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = y_prob.argmax(axis = 1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:48:05.953485Z","iopub.execute_input":"2023-01-09T16:48:05.953903Z","iopub.status.idle":"2023-01-09T16:48:05.961748Z","shell.execute_reply.started":"2023-01-09T16:48:05.953868Z","shell.execute_reply":"2023-01-09T16:48:05.960398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pic2 = np.array(X_test.iloc[0])\npic2 = pic2.reshape(28,28)\nplt.imshow(pic2, cmap = 'gray')\nprint(\"The image label is \", y_pred[13])","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:52:14.268121Z","iopub.execute_input":"2023-01-09T16:52:14.268517Z","iopub.status.idle":"2023-01-09T16:52:14.307625Z","shell.execute_reply.started":"2023-01-09T16:52:14.268484Z","shell.execute_reply":"2023-01-09T16:52:14.306487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:54:16.067607Z","iopub.execute_input":"2023-01-09T16:54:16.068036Z","iopub.status.idle":"2023-01-09T16:54:16.077671Z","shell.execute_reply.started":"2023-01-09T16:54:16.067998Z","shell.execute_reply":"2023-01-09T16:54:16.076199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Submission \ntest_set = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:54:52.088700Z","iopub.execute_input":"2023-01-09T16:54:52.089117Z","iopub.status.idle":"2023-01-09T16:54:52.392073Z","shell.execute_reply.started":"2023-01-09T16:54:52.089083Z","shell.execute_reply":"2023-01-09T16:54:52.390921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:54:54.656831Z","iopub.execute_input":"2023-01-09T16:54:54.657509Z","iopub.status.idle":"2023-01-09T16:54:54.663314Z","shell.execute_reply.started":"2023-01-09T16:54:54.657462Z","shell.execute_reply":"2023-01-09T16:54:54.662196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:55:00.353992Z","iopub.execute_input":"2023-01-09T16:55:00.354363Z","iopub.status.idle":"2023-01-09T16:55:00.376844Z","shell.execute_reply.started":"2023-01-09T16:55:00.354333Z","shell.execute_reply":"2023-01-09T16:55:00.375533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.array(test_set.iloc[:,:])","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:55:26.235710Z","iopub.execute_input":"2023-01-09T16:55:26.236147Z","iopub.status.idle":"2023-01-09T16:55:26.247860Z","shell.execute_reply.started":"2023-01-09T16:55:26.236110Z","shell.execute_reply":"2023-01-09T16:55:26.246617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test / 255\ntest","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:56:55.447107Z","iopub.execute_input":"2023-01-09T16:56:55.447524Z","iopub.status.idle":"2023-01-09T16:56:55.460709Z","shell.execute_reply.started":"2023-01-09T16:56:55.447489Z","shell.execute_reply":"2023-01-09T16:56:55.459660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prob = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T16:56:35.024554Z","iopub.execute_input":"2023-01-09T16:56:35.025502Z","iopub.status.idle":"2023-01-09T16:56:35.134489Z","shell.execute_reply.started":"2023-01-09T16:56:35.025457Z","shell.execute_reply":"2023-01-09T16:56:35.132699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}