{"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-23T06:14:15.605592Z","iopub.execute_input":"2024-02-23T06:14:15.606070Z","iopub.status.idle":"2024-02-23T06:14:15.617986Z","shell.execute_reply.started":"2024-02-23T06:14:15.606033Z","shell.execute_reply":"2024-02-23T06:14:15.616699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\ntrain = 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-02-23T06:14:16.075183Z","iopub.execute_input":"2024-02-23T06:14:16.075604Z","iopub.status.idle":"2024-02-23T06:14:21.914288Z","shell.execute_reply.started":"2024-02-23T06:14:16.075572Z","shell.execute_reply":"2024-02-23T06:14:21.912981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.drop(\"label\",axis = 1)\ny = train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-02-23T06:14:21.916320Z","iopub.execute_input":"2024-02-23T06:14:21.916643Z","iopub.status.idle":"2024-02-23T06:14:22.064306Z","shell.execute_reply.started":"2024-02-23T06:14:21.916615Z","shell.execute_reply":"2024-02-23T06:14:22.062922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfor i in range(5):\n    plt.figure()\n    plt.imshow(x.iloc[i,:].values.reshape(28,28),cmap='gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-23T06:14:22.066081Z","iopub.execute_input":"2024-02-23T06:14:22.066467Z","iopub.status.idle":"2024-02-23T06:14:23.026239Z","shell.execute_reply.started":"2024-02-23T06:14:22.066423Z","shell.execute_reply":"2024-02-23T06:14:23.024305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(x)\nlabels = np.array(y)\nprint(images.shape, labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-23T06:14:23.029704Z","iopub.execute_input":"2024-02-23T06:14:23.030142Z","iopub.status.idle":"2024-02-23T06:14:23.248882Z","shell.execute_reply.started":"2024-02-23T06:14:23.030105Z","shell.execute_reply":"2024-02-23T06:14:23.247627Z"},"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-23T06:14:23.250299Z","iopub.execute_input":"2024-02-23T06:14:23.251627Z","iopub.status.idle":"2024-02-23T06:14:23.454005Z","shell.execute_reply.started":"2024-02-23T06:14:23.251579Z","shell.execute_reply":"2024-02-23T06:14:23.452814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import models, layers \nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten\n\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-23T06:14:23.455372Z","iopub.execute_input":"2024-02-23T06:14:23.455731Z","iopub.status.idle":"2024-02-23T06:14:40.437313Z","shell.execute_reply.started":"2024-02-23T06:14:23.455699Z","shell.execute_reply":"2024-02-23T06:14:40.436198Z"},"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-23T06:14:40.438997Z","iopub.execute_input":"2024-02-23T06:14:40.439444Z","iopub.status.idle":"2024-02-23T06:14:40.474978Z","shell.execute_reply.started":"2024-02-23T06:14:40.439411Z","shell.execute_reply":"2024-02-23T06:14:40.473937Z"},"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-23T06:14:40.476188Z","iopub.execute_input":"2024-02-23T06:14:40.476746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop([])\ntest_np = np.array(test)\ntest_np = test_np.reshape(-1,28,28,1)\ntest_np = test_np/255.0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_np)\n\npredictions = np.argmax(predictions,axis = 1)\npredictions = pd.Series(predictions,name = \"Label\")","metadata":{"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(\"s.csv\", index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}