{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"papermill":{"default_parameters":{},"duration":213.891624,"end_time":"2023-07-14T11:17:59.095826","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-07-14T11:14:25.204202","version":"2.4.0"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Setup","metadata":{"id":"0FUpZZ4qgNWD","papermill":{"duration":0.006746,"end_time":"2023-07-14T11:14:36.643733","exception":false,"start_time":"2023-07-14T11:14:36.636987","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Read data","metadata":{"id":"a-EZn9WTgPYm","papermill":{"duration":0.005715,"end_time":"2023-07-14T11:14:36.655704","exception":false,"start_time":"2023-07-14T11:14:36.649989","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndf = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf","metadata":{"id":"ArEfHyebfXEQ","outputId":"81501bfd-07c1-4a97-f357-003bbf388ef4","papermill":{"duration":4.335768,"end_time":"2023-07-14T11:14:40.997554","exception":false,"start_time":"2023-07-14T11:14:36.661786","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:03:56.967612Z","iopub.execute_input":"2024-01-18T15:03:56.968047Z","iopub.status.idle":"2024-01-18T15:04:01.758884Z","shell.execute_reply.started":"2024-01-18T15:03:56.968011Z","shell.execute_reply":"2024-01-18T15:04:01.757772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.reshape(df.loc[:, df.columns != 'label'].to_numpy(), (len(df), 28, 28))\ny = df['label'].to_numpy()","metadata":{"id":"BMP7Hp7vgLaz","papermill":{"duration":0.133103,"end_time":"2023-07-14T11:14:41.137816","exception":false,"start_time":"2023-07-14T11:14:41.004713","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:04:01.761453Z","iopub.execute_input":"2024-01-18T15:04:01.762152Z","iopub.status.idle":"2024-01-18T15:04:01.897089Z","shell.execute_reply.started":"2024-01-18T15:04:01.762106Z","shell.execute_reply":"2024-01-18T15:04:01.895587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=2, ncols=5)\n\nfor i in range(2):\n    for j in range(5):\n        num = i*5+j\n        idx = np.where(y == num)[0][0]\n        axes[i, j].imshow(X[idx], cmap='gray')\n        axes[i, j].set_title(num)\nplt.show()","metadata":{"id":"XEATVESDt_QU","outputId":"f1937901-5555-488d-f45c-95b4aea37caa","papermill":{"duration":1.575009,"end_time":"2023-07-14T11:14:42.719987","exception":false,"start_time":"2023-07-14T11:14:41.144978","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:04:01.899058Z","iopub.execute_input":"2024-01-18T15:04:01.899516Z","iopub.status.idle":"2024-01-18T15:04:03.412308Z","shell.execute_reply.started":"2024-01-18T15:04:01.899476Z","shell.execute_reply":"2024-01-18T15:04:03.411108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train model","metadata":{"id":"9Ur81ubX-u9p","papermill":{"duration":0.008645,"end_time":"2023-07-14T11:14:42.736171","exception":false,"start_time":"2023-07-14T11:14:42.727526","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nprint(\"Training data shape:\", X_train.shape)\nprint(\"Training labels shape:\", y_train.shape)\nprint(\"Test data shape:\", X_test.shape)\nprint(\"Test labels shape:\", y_test.shape)","metadata":{"id":"k8AfLK8mxJNK","outputId":"a79d56c0-5d4a-4d2a-f1f8-1645caf70c4e","papermill":{"duration":1.734521,"end_time":"2023-07-14T11:14:44.478086","exception":false,"start_time":"2023-07-14T11:14:42.743565","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:04:03.415764Z","iopub.execute_input":"2024-01-18T15:04:03.416238Z","iopub.status.idle":"2024-01-18T15:04:04.523869Z","shell.execute_reply.started":"2024-01-18T15:04:03.416196Z","shell.execute_reply":"2024-01-18T15:04:04.522732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# Normalize pixel values between 0 and 1\nX_train = X_train / 255.0\nX_test = X_test / 255.0\n\n# Reshape data to add a channel dimension\nX_train = np.expand_dims(X_train, axis=-1)\nX_test = np.expand_dims(X_test, axis=-1)\n\n# Convert labels to one-hot encoding\ny_train = tf.keras.utils.to_categorical(y_train, num_classes=10)\ny_test = tf.keras.utils.to_categorical(y_test, num_classes=10)","metadata":{"id":"J1QXaWJ99pIo","papermill":{"duration":10.21823,"end_time":"2023-07-14T11:14:54.704358","exception":false,"start_time":"2023-07-14T11:14:44.486128","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:04:04.525206Z","iopub.execute_input":"2024-01-18T15:04:04.525615Z","iopub.status.idle":"2024-01-18T15:04:17.503047Z","shell.execute_reply.started":"2024-01-18T15:04:04.525586Z","shell.execute_reply":"2024-01-18T15:04:17.502064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"id":"IW37E8nL95FL","papermill":{"duration":0.267493,"end_time":"2023-07-14T11:14:54.979799","exception":false,"start_time":"2023-07-14T11:14:54.712306","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:04:17.504726Z","iopub.execute_input":"2024-01-18T15:04:17.505650Z","iopub.status.idle":"2024-01-18T15:04:17.719878Z","shell.execute_reply.started":"2024-01-18T15:04:17.505617Z","shell.execute_reply":"2024-01-18T15:04:17.719034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"id":"PfLQ4y7E-QUg","papermill":{"duration":0.031854,"end_time":"2023-07-14T11:14:55.019675","exception":false,"start_time":"2023-07-14T11:14:54.987821","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:04:17.720980Z","iopub.execute_input":"2024-01-18T15:04:17.721820Z","iopub.status.idle":"2024-01-18T15:04:17.739957Z","shell.execute_reply.started":"2024-01-18T15:04:17.721789Z","shell.execute_reply":"2024-01-18T15:04:17.738938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=8, batch_size=32, validation_data=(X_test, y_test))","metadata":{"id":"9pSAiR2U-RBk","outputId":"3bb3393b-3a62-49c0-cecc-efc76d6ed795","papermill":{"duration":164.771654,"end_time":"2023-07-14T11:17:39.799440","exception":false,"start_time":"2023-07-14T11:14:55.027786","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:04:17.741132Z","iopub.execute_input":"2024-01-18T15:04:17.741421Z","iopub.status.idle":"2024-01-18T15:07:41.000584Z","shell.execute_reply.started":"2024-01-18T15:04:17.741396Z","shell.execute_reply":"2024-01-18T15:07:40.999306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\nplt.show()","metadata":{"id":"Ra7Qgu-4COsQ","outputId":"b4b803ee-93fb-46b7-912a-1b77e9260538","papermill":{"duration":0.604617,"end_time":"2023-07-14T11:17:40.661657","exception":false,"start_time":"2023-07-14T11:17:40.057040","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:41.002408Z","iopub.execute_input":"2024-01-18T15:07:41.002871Z","iopub.status.idle":"2024-01-18T15:07:41.344593Z","shell.execute_reply.started":"2024-01-18T15:07:41.002839Z","shell.execute_reply":"2024-01-18T15:07:41.343328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate","metadata":{"id":"9hCwp4a6_qrS","papermill":{"duration":0.257603,"end_time":"2023-07-14T11:17:41.178777","exception":false,"start_time":"2023-07-14T11:17:40.921174","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(X_test, y_test)\nprint(f'Test Loss: {test_loss}, Test Accuracy: {test_accuracy}')","metadata":{"id":"GfPw3W2V-VTi","outputId":"c120f501-0c51-4cef-e9b1-25c2774a09e6","papermill":{"duration":1.879781,"end_time":"2023-07-14T11:17:43.384578","exception":false,"start_time":"2023-07-14T11:17:41.504797","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:41.348025Z","iopub.execute_input":"2024-01-18T15:07:41.348468Z","iopub.status.idle":"2024-01-18T15:07:42.884225Z","shell.execute_reply.started":"2024-01-18T15:07:41.348439Z","shell.execute_reply":"2024-01-18T15:07:42.883124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create submission","metadata":{"id":"tgL9T21Q-wgy","papermill":{"duration":0.26329,"end_time":"2023-07-14T11:17:43.913709","exception":false,"start_time":"2023-07-14T11:17:43.650419","status":"completed"},"tags":[]}},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsubmission","metadata":{"id":"QUgbqpJc_z8l","outputId":"a69b56aa-a0cd-40a5-a2b0-68a09023ccd0","papermill":{"duration":2.931216,"end_time":"2023-07-14T11:17:47.108890","exception":false,"start_time":"2023-07-14T11:17:44.177674","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:42.888072Z","iopub.execute_input":"2024-01-18T15:07:42.888453Z","iopub.status.idle":"2024-01-18T15:07:45.378078Z","shell.execute_reply.started":"2024-01-18T15:07:42.888422Z","shell.execute_reply":"2024-01-18T15:07:45.376965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data = np.reshape(submission.to_numpy(), (len(submission), 28, 28))\nsubmission_data = submission_data / 255.0\nsubmission_data = np.expand_dims(submission_data, axis=-1)","metadata":{"id":"FooFHpZYALwa","papermill":{"duration":0.370351,"end_time":"2023-07-14T11:17:47.744140","exception":false,"start_time":"2023-07-14T11:17:47.373789","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:45.379187Z","iopub.execute_input":"2024-01-18T15:07:45.379489Z","iopub.status.idle":"2024-01-18T15:07:45.471718Z","shell.execute_reply.started":"2024-01-18T15:07:45.379464Z","shell.execute_reply":"2024-01-18T15:07:45.470797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(submission_data)\npredictions","metadata":{"id":"DRKHxwS_-WOz","outputId":"0387af9c-25a8-4637-9ccc-4710a187bac2","papermill":{"duration":5.668119,"end_time":"2023-07-14T11:17:53.678268","exception":false,"start_time":"2023-07-14T11:17:48.010149","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:45.472969Z","iopub.execute_input":"2024-01-18T15:07:45.473303Z","iopub.status.idle":"2024-01-18T15:07:50.422677Z","shell.execute_reply.started":"2024-01-18T15:07:45.473274Z","shell.execute_reply":"2024-01-18T15:07:50.421463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(predictions, axis=1)","metadata":{"id":"ukyLxu_hAZva","outputId":"27de9c10-f3da-4477-fe90-3bfb46b06cc5","papermill":{"duration":0.285296,"end_time":"2023-07-14T11:17:54.235022","exception":false,"start_time":"2023-07-14T11:17:53.949726","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:50.424033Z","iopub.execute_input":"2024-01-18T15:07:50.424457Z","iopub.status.idle":"2024-01-18T15:07:50.431319Z","shell.execute_reply.started":"2024-01-18T15:07:50.424412Z","shell.execute_reply":"2024-01-18T15:07:50.430424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"id":"H9CTNAikA7hQ","outputId":"abaf8255-5cdd-4e11-a5ba-9a2754b8550d","papermill":{"duration":0.284482,"end_time":"2023-07-14T11:17:54.852678","exception":false,"start_time":"2023-07-14T11:17:54.568196","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:50.432464Z","iopub.execute_input":"2024-01-18T15:07:50.433127Z","iopub.status.idle":"2024-01-18T15:07:50.442849Z","shell.execute_reply.started":"2024-01-18T15:07:50.433096Z","shell.execute_reply":"2024-01-18T15:07:50.441845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.DataFrame(data={\"ImageId\": range(1,28001),\"Label\":np.argmax(predictions, axis=1)})\nsubmit.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"id":"3ZvYzdzBA9V9","papermill":{"duration":0.345594,"end_time":"2023-07-14T11:17:55.485783","exception":false,"start_time":"2023-07-14T11:17:55.140189","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-18T15:07:50.444429Z","iopub.execute_input":"2024-01-18T15:07:50.444755Z","iopub.status.idle":"2024-01-18T15:07:50.518579Z","shell.execute_reply.started":"2024-01-18T15:07:50.444727Z","shell.execute_reply":"2024-01-18T15:07:50.517410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"Sk-GDa79BO_H","papermill":{"duration":0.274129,"end_time":"2023-07-14T11:17:56.028699","exception":false,"start_time":"2023-07-14T11:17:55.754570","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}