{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30762,"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","execution":{"iopub.status.busy":"2025-02-01T21:51:26.803216Z","iopub.execute_input":"2025-02-01T21:51:26.804034Z","iopub.status.idle":"2025-02-01T21:51:26.823299Z","shell.execute_reply.started":"2025-02-01T21:51:26.803995Z","shell.execute_reply":"2025-02-01T21:51:26.822277Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Importing Librairies","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:26.824985Z","iopub.execute_input":"2025-02-01T21:51:26.825275Z","iopub.status.idle":"2025-02-01T21:51:26.829392Z","shell.execute_reply.started":"2025-02-01T21:51:26.825248Z","shell.execute_reply":"2025-02-01T21:51:26.828437Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Importing Data","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:26.830531Z","iopub.execute_input":"2025-02-01T21:51:26.830817Z","iopub.status.idle":"2025-02-01T21:51:28.875779Z","shell.execute_reply.started":"2025-02-01T21:51:26.830789Z","shell.execute_reply":"2025-02-01T21:51:28.874686Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train=train.label\nX_train=train.drop(\"label\",axis=1).values/255","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:28.878270Z","iopub.execute_input":"2025-02-01T21:51:28.878712Z","iopub.status.idle":"2025-02-01T21:51:29.070602Z","shell.execute_reply.started":"2025-02-01T21:51:28.878674Z","shell.execute_reply":"2025-02-01T21:51:29.069736Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\").values/255","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:29.071694Z","iopub.execute_input":"2025-02-01T21:51:29.071970Z","iopub.status.idle":"2025-02-01T21:51:30.281195Z","shell.execute_reply.started":"2025-02-01T21:51:29.071946Z","shell.execute_reply":"2025-02-01T21:51:30.280079Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Selection","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Flatten,Conv2D,MaxPooling2D,Reshape,Dropout","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:30.282709Z","iopub.execute_input":"2025-02-01T21:51:30.283129Z","iopub.status.idle":"2025-02-01T21:51:30.288469Z","shell.execute_reply.started":"2025-02-01T21:51:30.283086Z","shell.execute_reply":"2025-02-01T21:51:30.287399Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Reshape((28,28,1),input_shape=X_train.shape[1:]))\nmodel.add(Conv2D(32,(3,3),activation=\"relu\",strides=1))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(16,(3,3),activation=\"relu\"))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(128,activation=\"relu\"))\nmodel.add(Flatten())\nmodel.add(Dense(10,activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:30.289901Z","iopub.execute_input":"2025-02-01T21:51:30.290833Z","iopub.status.idle":"2025-02-01T21:51:30.375016Z","shell.execute_reply.started":"2025-02-01T21:51:30.290800Z","shell.execute_reply":"2025-02-01T21:51:30.373890Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:30.377465Z","iopub.execute_input":"2025-02-01T21:51:30.377758Z","iopub.status.idle":"2025-02-01T21:51:30.398997Z","shell.execute_reply.started":"2025-02-01T21:51:30.377730Z","shell.execute_reply":"2025-02-01T21:51:30.398237Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:30.399957Z","iopub.execute_input":"2025-02-01T21:51:30.400232Z","iopub.status.idle":"2025-02-01T21:51:30.408441Z","shell.execute_reply.started":"2025-02-01T21:51:30.400205Z","shell.execute_reply":"2025-02-01T21:51:30.407717Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\nearly_stopping_monitor=EarlyStopping(patience=5)","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:30.409441Z","iopub.execute_input":"2025-02-01T21:51:30.409700Z","iopub.status.idle":"2025-02-01T21:51:30.417411Z","shell.execute_reply.started":"2025-02-01T21:51:30.409675Z","shell.execute_reply":"2025-02-01T21:51:30.416413Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train,y_train,validation_split=0.2,epochs=20,batch_size=32,callbacks=[early_stopping_monitor])","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:51:30.418454Z","iopub.execute_input":"2025-02-01T21:51:30.418757Z","iopub.status.idle":"2025-02-01T21:52:12.007893Z","shell.execute_reply.started":"2025-02-01T21:51:30.418730Z","shell.execute_reply":"2025-02-01T21:52:12.006863Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Making Predictions","metadata":{}},{"cell_type":"code","source":"proba=model.predict(X_test)\ny_pred=np.argmax(proba,axis=1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:52:12.008987Z","iopub.execute_input":"2025-02-01T21:52:12.009295Z","iopub.status.idle":"2025-02-01T21:52:13.973939Z","shell.execute_reply.started":"2025-02-01T21:52:12.009266Z","shell.execute_reply":"2025-02-01T21:52:13.972738Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission=pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:52:13.975043Z","iopub.execute_input":"2025-02-01T21:52:13.975358Z","iopub.status.idle":"2025-02-01T21:52:13.987054Z","shell.execute_reply.started":"2025-02-01T21:52:13.975330Z","shell.execute_reply":"2025-02-01T21:52:13.985932Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"Label\"]=y_pred","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:52:13.988316Z","iopub.execute_input":"2025-02-01T21:52:13.988748Z","iopub.status.idle":"2025-02-01T21:52:13.994123Z","shell.execute_reply.started":"2025-02-01T21:52:13.988705Z","shell.execute_reply":"2025-02-01T21:52:13.993330Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:52:13.995470Z","iopub.execute_input":"2025-02-01T21:52:13.995867Z","iopub.status.idle":"2025-02-01T21:52:14.025792Z","shell.execute_reply.started":"2025-02-01T21:52:13.995826Z","shell.execute_reply":"2025-02-01T21:52:14.024893Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2025-02-01T21:52:14.026920Z","iopub.execute_input":"2025-02-01T21:52:14.027209Z","iopub.status.idle":"2025-02-01T21:52:14.037779Z","shell.execute_reply.started":"2025-02-01T21:52:14.027183Z","shell.execute_reply":"2025-02-01T21:52:14.036708Z"},"trusted":true},"outputs":[],"execution_count":null}]}