{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\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":"2024-05-26T11:52:20.305420Z","iopub.execute_input":"2024-05-26T11:52:20.305989Z","iopub.status.idle":"2024-05-26T11:52:20.314234Z","shell.execute_reply.started":"2024-05-26T11:52:20.305947Z","shell.execute_reply":"2024-05-26T11:52:20.313056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom tensorflow.keras import layers,models,Input,preprocessing \nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\nimport keras\nimport matplotlib.pyplot as plt\nfrom tensorflow.python.keras import regularizers\nfrom keras.layers import Dropout","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:52:24.755698Z","iopub.execute_input":"2024-05-26T11:52:24.756055Z","iopub.status.idle":"2024-05-26T11:52:36.175858Z","shell.execute_reply.started":"2024-05-26T11:52:24.756026Z","shell.execute_reply":"2024-05-26T11:52:36.175091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:52:39.718017Z","iopub.execute_input":"2024-05-26T11:52:39.718638Z","iopub.status.idle":"2024-05-26T11:52:45.081021Z","shell.execute_reply.started":"2024-05-26T11:52:39.718606Z","shell.execute_reply":"2024-05-26T11:52:45.080226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train shape:\",train.shape)\nprint(\"Test shape:\", test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:52:49.426698Z","iopub.execute_input":"2024-05-26T11:52:49.427363Z","iopub.status.idle":"2024-05-26T11:52:49.432255Z","shell.execute_reply.started":"2024-05-26T11:52:49.427332Z","shell.execute_reply":"2024-05-26T11:52:49.431399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:52:50.617165Z","iopub.execute_input":"2024-05-26T11:52:50.617827Z","iopub.status.idle":"2024-05-26T11:52:50.646189Z","shell.execute_reply.started":"2024-05-26T11:52:50.617795Z","shell.execute_reply":"2024-05-26T11:52:50.645247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 10\nimage_data = train.iloc[index, 1:].values\nimage_shape = (28,28)\n\n# Reshape the 1D array to a 2D array representing the image\nimage_array = image_data.reshape(image_shape)\n\n# Plot the image\nplt.imshow(image_array, cmap='gray')\nplt.axis('off')\nplt.title(f\"Image from row {index}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:52:54.752488Z","iopub.execute_input":"2024-05-26T11:52:54.753327Z","iopub.status.idle":"2024-05-26T11:52:54.959218Z","shell.execute_reply.started":"2024-05-26T11:52:54.753281Z","shell.execute_reply":"2024-05-26T11:52:54.958123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = train.iloc[:, 1:].copy()\nY_data = train.label\nprint(\"X shape:\", X_data.shape)\nprint(\"Y_shape:\", Y_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:52:56.880680Z","iopub.execute_input":"2024-05-26T11:52:56.881006Z","iopub.status.idle":"2024-05-26T11:52:56.983343Z","shell.execute_reply.started":"2024-05-26T11:52:56.880982Z","shell.execute_reply":"2024-05-26T11:52:56.982428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:52:58.042418Z","iopub.execute_input":"2024-05-26T11:52:58.042776Z","iopub.status.idle":"2024-05-26T11:52:58.060587Z","shell.execute_reply.started":"2024-05-26T11:52:58.042748Z","shell.execute_reply":"2024-05-26T11:52:58.059609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:00.328362Z","iopub.execute_input":"2024-05-26T11:53:00.328748Z","iopub.status.idle":"2024-05-26T11:53:00.335513Z","shell.execute_reply.started":"2024-05-26T11:53:00.328719Z","shell.execute_reply":"2024-05-26T11:53:00.334606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transform the X_data\n\nX_data = X_data.values.reshape(-1,28,28,1) / 255\nX_data.shape #It's now a numpy array","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:02.336865Z","iopub.execute_input":"2024-05-26T11:53:02.337499Z","iopub.status.idle":"2024-05-26T11:53:02.441294Z","shell.execute_reply.started":"2024-05-26T11:53:02.337447Z","shell.execute_reply":"2024-05-26T11:53:02.440339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transform Y_data to a numpy array as well\nY_data = Y_data.values\nY_data.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:03.547961Z","iopub.execute_input":"2024-05-26T11:53:03.548789Z","iopub.status.idle":"2024-05-26T11:53:03.554279Z","shell.execute_reply.started":"2024-05-26T11:53:03.548756Z","shell.execute_reply":"2024-05-26T11:53:03.553422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split into train and dev\nfrom sklearn.model_selection import train_test_split\nX_train,X_dev,Y_train,Y_dev = train_test_split(X_data, Y_data, train_size = 0.8, test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:04.452009Z","iopub.execute_input":"2024-05-26T11:53:04.452740Z","iopub.status.idle":"2024-05-26T11:53:05.411464Z","shell.execute_reply.started":"2024-05-26T11:53:04.452705Z","shell.execute_reply":"2024-05-26T11:53:05.410670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"X_train shape:\", X_train.shape)\nprint(\"X_dev shape:\", X_dev.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:10.336088Z","iopub.execute_input":"2024-05-26T11:53:10.336457Z","iopub.status.idle":"2024-05-26T11:53:10.341558Z","shell.execute_reply.started":"2024-05-26T11:53:10.336429Z","shell.execute_reply":"2024-05-26T11:53:10.340524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.InputLayer(input_shape = [28,28,1]),\n    tf.keras.layers.Conv2D(filters = 6, kernel_size = 5, activation = \"tanh\", padding = \"valid\"),\n    tf.keras.layers.AveragePooling2D(pool_size = 2, strides = 2, padding = \"valid\"),\n    tf.keras.layers.Conv2D(filters = 16, kernel_size = 5, activation = \"tanh\", padding = \"valid\"),\n    tf.keras.layers.AveragePooling2D(pool_size = 2, strides = 2, padding = \"valid\"),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(120, activation = \"tanh\"),\n    tf.keras.layers.Dense(84, activation = \"tanh\"),\n    tf.keras.layers.Dense(10, activation = \"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:12.349409Z","iopub.execute_input":"2024-05-26T11:53:12.350206Z","iopub.status.idle":"2024-05-26T11:53:13.161297Z","shell.execute_reply.started":"2024-05-26T11:53:12.350172Z","shell.execute_reply":"2024-05-26T11:53:13.160435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:22.745874Z","iopub.execute_input":"2024-05-26T11:53:22.746245Z","iopub.status.idle":"2024-05-26T11:53:22.769591Z","shell.execute_reply.started":"2024-05-26T11:53:22.746215Z","shell.execute_reply":"2024-05-26T11:53:22.768791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compile the model\nmodel.compile(optimizer = 'adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:53:26.473153Z","iopub.execute_input":"2024-05-26T11:53:26.473813Z","iopub.status.idle":"2024-05-26T11:53:26.488616Z","shell.execute_reply.started":"2024-05-26T11:53:26.473778Z","shell.execute_reply":"2024-05-26T11:53:26.487712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20\nhistory = model.fit(X_train,Y_train, epochs=epochs, validation_data=(X_dev,Y_dev))","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:55:33.168492Z","iopub.execute_input":"2024-05-26T11:55:33.168899Z","iopub.status.idle":"2024-05-26T11:56:21.553426Z","shell.execute_reply.started":"2024-05-26T11:55:33.168866Z","shell.execute_reply":"2024-05-26T11:56:21.552634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(10, 6))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-26T11:56:30.545098Z","iopub.execute_input":"2024-05-26T11:56:30.545671Z","iopub.status.idle":"2024-05-26T11:56:31.027536Z","shell.execute_reply.started":"2024-05-26T11:56:30.545631Z","shell.execute_reply":"2024-05-26T11:56:31.026608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#with normalization and regularization\nmodel2 = tf.keras.Sequential([\n    tf.keras.layers.InputLayer(input_shape = [28,28,1]),\n    tf.keras.layers.Conv2D(filters = 6, kernel_size = 5, activation = \"tanh\", padding = \"valid\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.AveragePooling2D(pool_size = 2, strides = 2, padding = \"valid\"),\n    tf.keras.layers.Conv2D(filters = 16, kernel_size = 5, activation = \"tanh\", padding = \"valid\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.AveragePooling2D(pool_size = 2, strides = 2, padding = \"valid\"),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(120, activation = \"tanh\"),\n    tf.keras.layers.Dropout(0.7),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dense(84, activation = \"tanh\"),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dense(10, activation = \"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:08:19.214055Z","iopub.execute_input":"2024-05-26T12:08:19.214922Z","iopub.status.idle":"2024-05-26T12:08:19.301666Z","shell.execute_reply.started":"2024-05-26T12:08:19.214884Z","shell.execute_reply":"2024-05-26T12:08:19.300745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:08:22.645925Z","iopub.execute_input":"2024-05-26T12:08:22.646283Z","iopub.status.idle":"2024-05-26T12:08:22.674930Z","shell.execute_reply.started":"2024-05-26T12:08:22.646254Z","shell.execute_reply":"2024-05-26T12:08:22.674087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compile the model\nmodel2.compile(optimizer = 'adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:08:30.222947Z","iopub.execute_input":"2024-05-26T12:08:30.223817Z","iopub.status.idle":"2024-05-26T12:08:30.232157Z","shell.execute_reply.started":"2024-05-26T12:08:30.223781Z","shell.execute_reply":"2024-05-26T12:08:30.231314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20\nhistory = model2.fit(X_train,Y_train, epochs=epochs, validation_data=(X_dev,Y_dev))","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:08:39.584818Z","iopub.execute_input":"2024-05-26T12:08:39.585711Z","iopub.status.idle":"2024-05-26T12:09:48.521046Z","shell.execute_reply.started":"2024-05-26T12:08:39.585674Z","shell.execute_reply":"2024-05-26T12:09:48.520226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(10, 6))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:10:05.958258Z","iopub.execute_input":"2024-05-26T12:10:05.958625Z","iopub.status.idle":"2024-05-26T12:10:06.398131Z","shell.execute_reply.started":"2024-05-26T12:10:05.958596Z","shell.execute_reply":"2024-05-26T12:10:06.397256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X = test.values.reshape(-1,28,28,1) / 255","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:11:36.279753Z","iopub.execute_input":"2024-05-26T12:11:36.280707Z","iopub.status.idle":"2024-05-26T12:11:36.353400Z","shell.execute_reply.started":"2024-05-26T12:11:36.280673Z","shell.execute_reply":"2024-05-26T12:11:36.352418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model2.predict(test_X)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:12:22.162591Z","iopub.execute_input":"2024-05-26T12:12:22.162933Z","iopub.status.idle":"2024-05-26T12:12:24.602688Z","shell.execute_reply.started":"2024-05-26T12:12:22.162909Z","shell.execute_reply":"2024-05-26T12:12:24.601833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:12:34.087473Z","iopub.execute_input":"2024-05-26T12:12:34.087855Z","iopub.status.idle":"2024-05-26T12:12:34.094675Z","shell.execute_reply.started":"2024-05-26T12:12:34.087810Z","shell.execute_reply":"2024-05-26T12:12:34.093728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:12:49.521875Z","iopub.execute_input":"2024-05-26T12:12:49.522587Z","iopub.status.idle":"2024-05-26T12:12:49.528951Z","shell.execute_reply.started":"2024-05-26T12:12:49.522551Z","shell.execute_reply":"2024-05-26T12:12:49.527985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:13:23.459297Z","iopub.execute_input":"2024-05-26T12:13:23.460236Z","iopub.status.idle":"2024-05-26T12:13:23.479974Z","shell.execute_reply.started":"2024-05-26T12:13:23.460192Z","shell.execute_reply":"2024-05-26T12:13:23.479052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:13:37.539613Z","iopub.execute_input":"2024-05-26T12:13:37.540001Z","iopub.status.idle":"2024-05-26T12:13:37.550264Z","shell.execute_reply.started":"2024-05-26T12:13:37.539972Z","shell.execute_reply":"2024-05-26T12:13:37.549317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels = np.argmax(preds,axis=1)\npredicted_labels.reshape(28000,1).shape","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:16:36.964205Z","iopub.execute_input":"2024-05-26T12:16:36.965088Z","iopub.status.idle":"2024-05-26T12:16:36.972188Z","shell.execute_reply.started":"2024-05-26T12:16:36.965052Z","shell.execute_reply":"2024-05-26T12:16:36.971119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_id = np.arange(1,28001)\nimgs_id.reshape(28000,1)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:16:56.262409Z","iopub.execute_input":"2024-05-26T12:16:56.262735Z","iopub.status.idle":"2024-05-26T12:16:56.269556Z","shell.execute_reply.started":"2024-05-26T12:16:56.262710Z","shell.execute_reply":"2024-05-26T12:16:56.268649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId':imgs_id,\n    'Label':predicted_labels\n})","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:17:22.530237Z","iopub.execute_input":"2024-05-26T12:17:22.530606Z","iopub.status.idle":"2024-05-26T12:17:22.535851Z","shell.execute_reply.started":"2024-05-26T12:17:22.530577Z","shell.execute_reply":"2024-05-26T12:17:22.534834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('Submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-26T12:17:31.112080Z","iopub.execute_input":"2024-05-26T12:17:31.112480Z","iopub.status.idle":"2024-05-26T12:17:31.150401Z","shell.execute_reply.started":"2024-05-26T12:17:31.112451Z","shell.execute_reply":"2024-05-26T12:17:31.149689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}