{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<div class=\"alert alert-success\" style = \"border-radius: 20px;text-align: center;\" role=\"alert\">\n    Don't forget to upvote if you liked the notebook\n</div>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix , classification_report\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport tensorflow.keras.layers as tfl\nimport tensorflow.keras.models as tfm ","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:07:26.673700Z","iopub.execute_input":"2023-12-01T14:07:26.674215Z","iopub.status.idle":"2023-12-01T14:07:26.700983Z","shell.execute_reply.started":"2023-12-01T14:07:26.674176Z","shell.execute_reply":"2023-12-01T14:07:26.699378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:07:30.135145Z","iopub.execute_input":"2023-12-01T14:07:30.135664Z","iopub.status.idle":"2023-12-01T14:07:33.457059Z","shell.execute_reply.started":"2023-12-01T14:07:30.135625Z","shell.execute_reply":"2023-12-01T14:07:33.455731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['label']","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:07:33.459015Z","iopub.execute_input":"2023-12-01T14:07:33.459366Z","iopub.status.idle":"2023-12-01T14:07:33.465232Z","shell.execute_reply.started":"2023-12-01T14:07:33.459337Z","shell.execute_reply":"2023-12-01T14:07:33.464038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(columns='label' , inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:07:33.717199Z","iopub.execute_input":"2023-12-01T14:07:33.717645Z","iopub.status.idle":"2023-12-01T14:07:34.029105Z","shell.execute_reply.started":"2023-12-01T14:07:33.717608Z","shell.execute_reply":"2023-12-01T14:07:34.027760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(df, y, test_size=0.33, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:13:10.856345Z","iopub.execute_input":"2023-12-01T14:13:10.856830Z","iopub.status.idle":"2023-12-01T14:13:11.117764Z","shell.execute_reply.started":"2023-12-01T14:13:10.856797Z","shell.execute_reply":"2023-12-01T14:13:11.116544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.values\nx_test = x_test.values\ny_train = y_train.values\ny_test = y_test.values\n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:13:11.197649Z","iopub.execute_input":"2023-12-01T14:13:11.198133Z","iopub.status.idle":"2023-12-01T14:13:11.204108Z","shell.execute_reply.started":"2023-12-01T14:13:11.198091Z","shell.execute_reply":"2023-12-01T14:13:11.202926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\nx_test = x_test.reshape(x_test.shape[0], 28, 28, 1)\nx_train = x_train.astype('float')\nx_test = x_test.astype('float')\nx_train /= 255\nx_test /= 255","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:13:15.115538Z","iopub.execute_input":"2023-12-01T14:13:15.115973Z","iopub.status.idle":"2023-12-01T14:13:15.246526Z","shell.execute_reply.started":"2023-12-01T14:13:15.115941Z","shell.execute_reply":"2023-12-01T14:13:15.244414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:13:17.779763Z","iopub.execute_input":"2023-12-01T14:13:17.780203Z","iopub.status.idle":"2023-12-01T14:13:17.789824Z","shell.execute_reply.started":"2023-12-01T14:13:17.780169Z","shell.execute_reply":"2023-12-01T14:13:17.788402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def leNet5(input_shape , num_of_labels):\n    model = tfm.Sequential()\n    \n    model.add(tfl.Conv2D(6, (5, 5), activation='tanh', input_shape=input_shape))\n    model.add(tfl.AvgPool2D((2,2) , strides=2))\n    \n    model.add(tfl.Conv2D(16 , (5,5) , activation='tanh'))          \n    model.add(tfl.AvgPool2D((2,2) , strides=2))\n    \n    model.add(tfl.Flatten())\n    \n    model.add(tfl.Dense(120 , activation='tanh'))\n    model.add(tfl.Dense(84 , activation='tanh'))\n    \n    model.add(tfl.Dense(num_of_labels , activation='softmax'))            \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:13:18.300658Z","iopub.execute_input":"2023-12-01T14:13:18.301253Z","iopub.status.idle":"2023-12-01T14:13:18.311068Z","shell.execute_reply.started":"2023-12-01T14:13:18.301214Z","shell.execute_reply":"2023-12-01T14:13:18.310064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_model = leNet5(input_shape=x_train[0].shape , num_of_labels=len(np.unique(y_train)))\nmnist_model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n              metrics=['accuracy'])\n\nmnist_model.fit(x_train, y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:13:23.293224Z","iopub.execute_input":"2023-12-01T14:13:23.293648Z","iopub.status.idle":"2023-12-01T14:14:55.085261Z","shell.execute_reply.started":"2023-12-01T14:13:23.293616Z","shell.execute_reply":"2023-12-01T14:14:55.083685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:14:55.087564Z","iopub.execute_input":"2023-12-01T14:14:55.088017Z","iopub.status.idle":"2023-12-01T14:14:55.121217Z","shell.execute_reply.started":"2023-12-01T14:14:55.087974Z","shell.execute_reply":"2023-12-01T14:14:55.120062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred = np.argmax(mnist_model.predict(x_test) , axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:14:55.122980Z","iopub.execute_input":"2023-12-01T14:14:55.123440Z","iopub.status.idle":"2023-12-01T14:14:57.931411Z","shell.execute_reply.started":"2023-12-01T14:14:55.123398Z","shell.execute_reply":"2023-12-01T14:14:57.930366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convmat = confusion_matrix(y_test , y_val_pred)\nsns.heatmap(convmat , annot=True , fmt='d',cmap = 'Greens')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:14:57.934042Z","iopub.execute_input":"2023-12-01T14:14:57.934432Z","iopub.status.idle":"2023-12-01T14:14:59.177452Z","shell.execute_reply.started":"2023-12-01T14:14:57.934400Z","shell.execute_reply":"2023-12-01T14:14:59.176110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test , y_val_pred))","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:15:07.938705Z","iopub.execute_input":"2023-12-01T14:15:07.939915Z","iopub.status.idle":"2023-12-01T14:15:07.989665Z","shell.execute_reply.started":"2023-12-01T14:15:07.939866Z","shell.execute_reply":"2023-12-01T14:15:07.988081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train2, y_train2), (x_test2, y_test2) = tf.keras.datasets.fashion_mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:21:35.215769Z","iopub.execute_input":"2023-11-30T12:21:35.216265Z","iopub.status.idle":"2023-11-30T12:21:38.192921Z","shell.execute_reply.started":"2023-11-30T12:21:35.216235Z","shell.execute_reply":"2023-11-30T12:21:38.192095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train2.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:21:38.193880Z","iopub.execute_input":"2023-11-30T12:21:38.194217Z","iopub.status.idle":"2023-11-30T12:21:38.202011Z","shell.execute_reply.started":"2023-11-30T12:21:38.194181Z","shell.execute_reply":"2023-11-30T12:21:38.200789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train2 = x_train2.reshape(x_train2.shape[0], 28, 28, 1)\nx_test2 = x_test2.reshape(x_test2.shape[0], 28, 28, 1)\nx_train2 = x_train2.astype('float')\nx_test2 = x_test2.astype('float')\nx_train2 /= 255\nx_test2 /= 255","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:21:38.203305Z","iopub.execute_input":"2023-11-30T12:21:38.203606Z","iopub.status.idle":"2023-11-30T12:21:38.428951Z","shell.execute_reply.started":"2023-11-30T12:21:38.203578Z","shell.execute_reply":"2023-11-30T12:21:38.427713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train2","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:21:38.430476Z","iopub.execute_input":"2023-11-30T12:21:38.430855Z","iopub.status.idle":"2023-11-30T12:21:38.438924Z","shell.execute_reply.started":"2023-11-30T12:21:38.430823Z","shell.execute_reply":"2023-11-30T12:21:38.437501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fashion_model = leNet5(input_shape=x_train2[0].shape , num_of_labels=len(np.unique(y_train2)))\nfashion_model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n              metrics=['accuracy'])\n\nfashion_model.fit(x_train2, y_train2, epochs=10 )","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:21:38.440536Z","iopub.execute_input":"2023-11-30T12:21:38.440937Z","iopub.status.idle":"2023-11-30T12:24:40.474796Z","shell.execute_reply.started":"2023-11-30T12:21:38.440894Z","shell.execute_reply":"2023-11-30T12:24:40.473571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test2_pred = np.argmax(fashion_model.predict(x_test2) , axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:24:40.476367Z","iopub.execute_input":"2023-11-30T12:24:40.476964Z","iopub.status.idle":"2023-11-30T12:24:43.221056Z","shell.execute_reply.started":"2023-11-30T12:24:40.476930Z","shell.execute_reply":"2023-11-30T12:24:43.219772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convmat2 = confusion_matrix(y_test2 , y_test2_pred)\nsns.heatmap(convmat2 , annot=True , fmt='d',cmap = 'Greens')","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:24:43.222598Z","iopub.execute_input":"2023-11-30T12:24:43.222985Z","iopub.status.idle":"2023-11-30T12:24:43.851083Z","shell.execute_reply.started":"2023-11-30T12:24:43.222944Z","shell.execute_reply":"2023-11-30T12:24:43.849824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test2 , y_test2_pred))","metadata":{"execution":{"iopub.status.busy":"2023-11-30T12:24:43.852838Z","iopub.execute_input":"2023-11-30T12:24:43.853341Z","iopub.status.idle":"2023-11-30T12:24:43.891152Z","shell.execute_reply.started":"2023-11-30T12:24:43.853288Z","shell.execute_reply":"2023-11-30T12:24:43.890009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:18:30.677335Z","iopub.execute_input":"2023-12-01T14:18:30.677809Z","iopub.status.idle":"2023-12-01T14:18:32.773677Z","shell.execute_reply.started":"2023-12-01T14:18:30.677772Z","shell.execute_reply":"2023-12-01T14:18:32.772557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.values","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:19:13.350104Z","iopub.execute_input":"2023-12-01T14:19:13.350626Z","iopub.status.idle":"2023-12-01T14:19:13.357071Z","shell.execute_reply.started":"2023-12-01T14:19:13.350587Z","shell.execute_reply":"2023-12-01T14:19:13.355880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.reshape(test.shape[0], 28, 28, 1)\ntest = test / 255","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:19:47.564891Z","iopub.execute_input":"2023-12-01T14:19:47.565336Z","iopub.status.idle":"2023-12-01T14:19:47.644431Z","shell.execute_reply.started":"2023-12-01T14:19:47.565301Z","shell.execute_reply":"2023-12-01T14:19:47.643188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred = np.argmax(mnist_model.predict(test) , axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:20:33.973914Z","iopub.execute_input":"2023-12-01T14:20:33.974357Z","iopub.status.idle":"2023-12-01T14:20:38.929125Z","shell.execute_reply.started":"2023-12-01T14:20:33.974321Z","shell.execute_reply":"2023-12-01T14:20:38.927867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(y_test_pred)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:20:40.255982Z","iopub.execute_input":"2023-12-01T14:20:40.256421Z","iopub.status.idle":"2023-12-01T14:20:40.261608Z","shell.execute_reply.started":"2023-12-01T14:20:40.256387Z","shell.execute_reply":"2023-12-01T14:20:40.260638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_root = \"/kaggle/working/\"\n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:20:59.463289Z","iopub.execute_input":"2023-12-01T14:20:59.463757Z","iopub.status.idle":"2023-12-01T14:20:59.468978Z","shell.execute_reply.started":"2023-12-01T14:20:59.463722Z","shell.execute_reply":"2023-12-01T14:20:59.467770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"ImageId\": test_df.index + 1, \"Label\": y_test_pred})\nsubmission.to_csv(f\"{output_root}submission.csv\", index=False)\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T14:21:22.987859Z","iopub.execute_input":"2023-12-01T14:21:22.988380Z","iopub.status.idle":"2023-12-01T14:21:23.071868Z","shell.execute_reply.started":"2023-12-01T14:21:22.988344Z","shell.execute_reply":"2023-12-01T14:21:23.070604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}