{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-02-02T20:30:45.402150Z","iopub.execute_input":"2024-02-02T20:30:45.403116Z","iopub.status.idle":"2024-02-02T20:30:45.414376Z","shell.execute_reply.started":"2024-02-02T20:30:45.403045Z","shell.execute_reply":"2024-02-02T20:30:45.413334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras import models, layers \nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten\n","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:45.417322Z","iopub.execute_input":"2024-02-02T20:30:45.418183Z","iopub.status.idle":"2024-02-02T20:30:45.429562Z","shell.execute_reply.started":"2024-02-02T20:30:45.418143Z","shell.execute_reply":"2024-02-02T20:30:45.428236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_images = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:45.431419Z","iopub.execute_input":"2024-02-02T20:30:45.432279Z","iopub.status.idle":"2024-02-02T20:30:49.397318Z","shell.execute_reply.started":"2024-02-02T20:30:45.432238Z","shell.execute_reply":"2024-02-02T20:30:49.395898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:49.398727Z","iopub.execute_input":"2024-02-02T20:30:49.399104Z","iopub.status.idle":"2024-02-02T20:30:49.419615Z","shell.execute_reply.started":"2024-02-02T20:30:49.399052Z","shell.execute_reply":"2024-02-02T20:30:49.418326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train.drop(\"label\",axis = 1)\ntrain_labels = train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:49.422892Z","iopub.execute_input":"2024-02-02T20:30:49.423241Z","iopub.status.idle":"2024-02-02T20:30:49.562761Z","shell.execute_reply.started":"2024-02-02T20:30:49.423213Z","shell.execute_reply":"2024-02-02T20:30:49.561320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255\n# test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255\n# train_images = train_images.values.reshape((len(train_images), 28, 28, 1)).astype('float32') / 255\n# test_images = test_images.values.reshape((len(test_images), 28, 28, 1)).astype('float32') / 255\n\nimages = np.array(train_images)\nlabels = np.array(train_labels)\nprint(images.shape, labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:49.564515Z","iopub.execute_input":"2024-02-02T20:30:49.565627Z","iopub.status.idle":"2024-02-02T20:30:49.708198Z","shell.execute_reply.started":"2024-02-02T20:30:49.565586Z","shell.execute_reply":"2024-02-02T20:30:49.706752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = images.reshape(-1,28,28,1)\nimages = images / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:49.709774Z","iopub.execute_input":"2024-02-02T20:30:49.710378Z","iopub.status.idle":"2024-02-02T20:30:49.865015Z","shell.execute_reply.started":"2024-02-02T20:30:49.710349Z","shell.execute_reply":"2024-02-02T20:30:49.863641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = tf.keras.Sequential([\n    Conv2D(filters = 64, kernel_size=(5,5),strides=(1,1),padding='same',activation='relu' ),\n    MaxPooling2D(pool_size=(2,2), strides=(2,2)),\n    Conv2D(filters = 32, kernel_size=(5,5),strides=(1,1),activation='relu'),\n    MaxPooling2D(pool_size=(2,2), strides=(2,2)),\n    Flatten(),\n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:49.866368Z","iopub.execute_input":"2024-02-02T20:30:49.866705Z","iopub.status.idle":"2024-02-02T20:30:49.917743Z","shell.execute_reply.started":"2024-02-02T20:30:49.866679Z","shell.execute_reply":"2024-02-02T20:30:49.916453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:49.919225Z","iopub.execute_input":"2024-02-02T20:30:49.919852Z","iopub.status.idle":"2024-02-02T20:30:49.937466Z","shell.execute_reply.started":"2024-02-02T20:30:49.919801Z","shell.execute_reply":"2024-02-02T20:30:49.936461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x=images, y= labels, batch_size = 32, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:30:49.938728Z","iopub.execute_input":"2024-02-02T20:30:49.939275Z","iopub.status.idle":"2024-02-02T20:36:52.939909Z","shell.execute_reply.started":"2024-02-02T20:30:49.939247Z","shell.execute_reply":"2024-02-02T20:36:52.938606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images.drop([])\ntest_images = np.array(test_images)\ntest_images = test_images.reshape(-1,28,28,1)\ntest_images = test_images/255.0","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:36:52.941343Z","iopub.execute_input":"2024-02-02T20:36:52.941678Z","iopub.status.idle":"2024-02-02T20:36:53.171019Z","shell.execute_reply.started":"2024-02-02T20:36:52.941651Z","shell.execute_reply":"2024-02-02T20:36:53.170089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_images)\n\npredictions = np.argmax(predictions,axis = 1)\npredictions = pd.Series(predictions,name = \"Label\")","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:38:44.134698Z","iopub.execute_input":"2024-02-02T20:38:44.135838Z","iopub.status.idle":"2024-02-02T20:38:54.743261Z","shell.execute_reply.started":"2024-02-02T20:38:44.135782Z","shell.execute_reply":"2024-02-02T20:38:54.741909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001), name = \"ImageId\"),predictions],axis = 1)\nsubmission.to_csv(\"submissions.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2024-02-02T20:38:56.107231Z","iopub.execute_input":"2024-02-02T20:38:56.107650Z","iopub.status.idle":"2024-02-02T20:38:56.137435Z","shell.execute_reply.started":"2024-02-02T20:38:56.107618Z","shell.execute_reply":"2024-02-02T20:38:56.136237Z"},"trusted":true},"execution_count":null,"outputs":[]}]}