{"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":30664,"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-03-20T02:27:51.735763Z","iopub.execute_input":"2024-03-20T02:27:51.736143Z","iopub.status.idle":"2024-03-20T02:27:52.102101Z","shell.execute_reply.started":"2024-03-20T02:27:51.736099Z","shell.execute_reply":"2024-03-20T02:27:52.100781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:27:53.881946Z","iopub.execute_input":"2024-03-20T02:27:53.882428Z","iopub.status.idle":"2024-03-20T02:27:56.373891Z","shell.execute_reply.started":"2024-03-20T02:27:53.882371Z","shell.execute_reply":"2024-03-20T02:27:56.372244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull()","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:27:56.375511Z","iopub.execute_input":"2024-03-20T02:27:56.375796Z","iopub.status.idle":"2024-03-20T02:27:56.410160Z","shell.execute_reply.started":"2024-03-20T02:27:56.375771Z","shell.execute_reply":"2024-03-20T02:27:56.408936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train_data.drop('label', axis=1).values\ny=train_data['label']\nx","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:40:46.589807Z","iopub.execute_input":"2024-03-20T02:40:46.590304Z","iopub.status.idle":"2024-03-20T02:40:46.665852Z","shell.execute_reply.started":"2024-03-20T02:40:46.590264Z","shell.execute_reply":"2024-03-20T02:40:46.664157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:40:48.661872Z","iopub.execute_input":"2024-03-20T02:40:48.662864Z","iopub.status.idle":"2024-03-20T02:40:48.669872Z","shell.execute_reply.started":"2024-03-20T02:40:48.662807Z","shell.execute_reply":"2024-03-20T02:40:48.668657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.layers import Dense,Flatten\n\nfrom tensorflow.keras import Sequential \n\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras import layers, models","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:40:53.717478Z","iopub.execute_input":"2024-03-20T02:40:53.718781Z","iopub.status.idle":"2024-03-20T02:40:53.727065Z","shell.execute_reply.started":"2024-03-20T02:40:53.718733Z","shell.execute_reply":"2024-03-20T02:40:53.724628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = x/ 255.0\n\n# Reshape data for CNN input\nx = x.reshape(-1, 28, 28, 1)  # Reshape to (number_of_samples, height, width, channels)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:40:54.946280Z","iopub.execute_input":"2024-03-20T02:40:54.946719Z","iopub.status.idle":"2024-03-20T02:40:55.013514Z","shell.execute_reply.started":"2024-03-20T02:40:54.946689Z","shell.execute_reply":"2024-03-20T02:40:55.011731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:41:01.565899Z","iopub.execute_input":"2024-03-20T02:41:01.566697Z","iopub.status.idle":"2024-03-20T02:41:01.622992Z","shell.execute_reply.started":"2024-03-20T02:41:01.566654Z","shell.execute_reply":"2024-03-20T02:41:01.621423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:41:03.474981Z","iopub.execute_input":"2024-03-20T02:41:03.475986Z","iopub.status.idle":"2024-03-20T02:41:03.508949Z","shell.execute_reply.started":"2024-03-20T02:41:03.475919Z","shell.execute_reply":"2024-03-20T02:41:03.507511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:41:05.871717Z","iopub.execute_input":"2024-03-20T02:41:05.872104Z","iopub.status.idle":"2024-03-20T02:41:05.888245Z","shell.execute_reply.started":"2024-03-20T02:41:05.872078Z","shell.execute_reply":"2024-03-20T02:41:05.885334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x,y, validation_split=0.2, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:41:16.071566Z","iopub.execute_input":"2024-03-20T02:41:16.072086Z","iopub.status.idle":"2024-03-20T02:45:09.840561Z","shell.execute_reply.started":"2024-03-20T02:41:16.072046Z","shell.execute_reply":"2024-03-20T02:45:09.838160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_data","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:45:36.776912Z","iopub.execute_input":"2024-03-20T02:45:36.777424Z","iopub.status.idle":"2024-03-20T02:45:38.310474Z","shell.execute_reply.started":"2024-03-20T02:45:36.777362Z","shell.execute_reply":"2024-03-20T02:45:38.309548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=test_data.values\nx_test = x_test.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:48:45.684592Z","iopub.execute_input":"2024-03-20T02:48:45.684978Z","iopub.status.idle":"2024-03-20T02:48:45.690085Z","shell.execute_reply.started":"2024-03-20T02:48:45.684950Z","shell.execute_reply":"2024-03-20T02:48:45.688943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:48:46.957844Z","iopub.execute_input":"2024-03-20T02:48:46.958327Z","iopub.status.idle":"2024-03-20T02:48:57.434063Z","shell.execute_reply.started":"2024-03-20T02:48:46.958290Z","shell.execute_reply":"2024-03-20T02:48:57.432074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:49:27.311565Z","iopub.execute_input":"2024-03-20T02:49:27.312048Z","iopub.status.idle":"2024-03-20T02:49:27.331824Z","shell.execute_reply.started":"2024-03-20T02:49:27.312011Z","shell.execute_reply":"2024-03-20T02:49:27.330151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:59:10.231530Z","iopub.execute_input":"2024-03-20T02:59:10.232046Z","iopub.status.idle":"2024-03-20T02:59:10.241403Z","shell.execute_reply.started":"2024-03-20T02:59:10.232008Z","shell.execute_reply":"2024-03-20T02:59:10.239786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_index = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:59:49.194753Z","iopub.execute_input":"2024-03-20T02:59:49.195209Z","iopub.status.idle":"2024-03-20T02:59:49.202537Z","shell.execute_reply.started":"2024-03-20T02:59:49.195174Z","shell.execute_reply":"2024-03-20T02:59:49.200614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_index","metadata":{"execution":{"iopub.status.busy":"2024-03-20T02:59:50.366898Z","iopub.execute_input":"2024-03-20T02:59:50.367372Z","iopub.status.idle":"2024-03-20T02:59:50.375341Z","shell.execute_reply.started":"2024-03-20T02:59:50.367337Z","shell.execute_reply":"2024-03-20T02:59:50.374314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.DataFrame({'ImageId': range(1, len(y_pred) + 1), 'Label':max_index})\nsub.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T03:02:25.543479Z","iopub.execute_input":"2024-03-20T03:02:25.543809Z","iopub.status.idle":"2024-03-20T03:02:25.570704Z","shell.execute_reply.started":"2024-03-20T03:02:25.543782Z","shell.execute_reply":"2024-03-20T03:02:25.569622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-03-20T03:02:25.871842Z","iopub.execute_input":"2024-03-20T03:02:25.872498Z","iopub.status.idle":"2024-03-20T03:02:25.883037Z","shell.execute_reply.started":"2024-03-20T03:02:25.872462Z","shell.execute_reply":"2024-03-20T03:02:25.881129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}