{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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":31260,"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# 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","trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:48.640220Z","iopub.execute_input":"2026-02-08T18:27:48.640487Z","iopub.status.idle":"2026-02-08T18:27:48.648706Z","shell.execute_reply.started":"2026-02-08T18:27:48.640457Z","shell.execute_reply":"2026-02-08T18:27:48.647850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:48.650368Z","iopub.execute_input":"2026-02-08T18:27:48.651072Z","iopub.status.idle":"2026-02-08T18:27:51.662506Z","shell.execute_reply.started":"2026-02-08T18:27:48.650997Z","shell.execute_reply":"2026-02-08T18:27:51.661870Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:51.663346Z","iopub.execute_input":"2026-02-08T18:27:51.663601Z","iopub.status.idle":"2026-02-08T18:27:51.669779Z","shell.execute_reply.started":"2026-02-08T18:27:51.663571Z","shell.execute_reply":"2026-02-08T18:27:51.668908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = train_df.drop('label', axis=1).values\ny_train = train_df['label'].values\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:51.670665Z","iopub.execute_input":"2026-02-08T18:27:51.670928Z","iopub.status.idle":"2026-02-08T18:27:51.760581Z","shell.execute_reply.started":"2026-02-08T18:27:51.670907Z","shell.execute_reply":"2026-02-08T18:27:51.759948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X_train / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:51.762560Z","iopub.execute_input":"2026-02-08T18:27:51.762770Z","iopub.status.idle":"2026-02-08T18:27:51.862382Z","shell.execute_reply.started":"2026-02-08T18:27:51.762751Z","shell.execute_reply":"2026-02-08T18:27:51.861731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X_train.reshape(-1, 28, 28, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:51.863215Z","iopub.execute_input":"2026-02-08T18:27:51.863522Z","iopub.status.idle":"2026-02-08T18:27:51.867776Z","shell.execute_reply.started":"2026-02-08T18:27:51.863499Z","shell.execute_reply":"2026-02-08T18:27:51.867101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test_final=test_df.values/255.0\nx_test_final=x_test_final.reshape(-1,28,28,1)\n\nx_test_final.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:51.869382Z","iopub.execute_input":"2026-02-08T18:27:51.870116Z","iopub.status.idle":"2026-02-08T18:27:51.947139Z","shell.execute_reply.started":"2026-02-08T18:27:51.870023Z","shell.execute_reply":"2026-02-08T18:27:51.946424Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Flatten,Dense,Input,MaxPooling2D,Conv2D\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32,kernel_size=(3,3),padding='valid', activation='relu', input_shape=(28,28,1)))\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))\nmodel.add(Conv2D(32,kernel_size=(3,3),padding='valid', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))\n\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(10,activation='softmax'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:51.948022Z","iopub.execute_input":"2026-02-08T18:27:51.948426Z","iopub.status.idle":"2026-02-08T18:27:52.010555Z","shell.execute_reply.started":"2026-02-08T18:27:51.948403Z","shell.execute_reply":"2026-02-08T18:27:52.009704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.fit(X_train, y_train, epochs=30,batch_size=64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:27:52.011568Z","iopub.execute_input":"2026-02-08T18:27:52.012327Z","iopub.status.idle":"2026-02-08T18:28:55.830846Z","shell.execute_reply.started":"2026-02-08T18:27:52.012296Z","shell.execute_reply":"2026-02-08T18:28:55.830256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\ntest_prediction=model.predict(x_test_final)\ntest_labels=np.argmax(test_prediction,axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:29:14.687748Z","iopub.execute_input":"2026-02-08T18:29:14.688444Z","iopub.status.idle":"2026-02-08T18:29:16.533536Z","shell.execute_reply.started":"2026-02-08T18:29:14.688414Z","shell.execute_reply":"2026-02-08T18:29:16.532908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission=pd.DataFrame({\n    \"ImageId\":np.arange(1,len(test_labels)+1),\n    \"Label\":test_labels\n})\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:29:31.158513Z","iopub.execute_input":"2026-02-08T18:29:31.159263Z","iopub.status.idle":"2026-02-08T18:29:31.177213Z","shell.execute_reply.started":"2026-02-08T18:29:31.159235Z","shell.execute_reply":"2026-02-08T18:29:31.176575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-08T18:29:42.620702Z","iopub.execute_input":"2026-02-08T18:29:42.621115Z","iopub.status.idle":"2026-02-08T18:29:42.648116Z","shell.execute_reply.started":"2026-02-08T18:29:42.621087Z","shell.execute_reply":"2026-02-08T18:29:42.647555Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}