{"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":30732,"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-07-08T21:02:59.984291Z","iopub.execute_input":"2024-07-08T21:02:59.985200Z","iopub.status.idle":"2024-07-08T21:02:59.994861Z","shell.execute_reply.started":"2024-07-08T21:02:59.985163Z","shell.execute_reply":"2024-07-08T21:02:59.993339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:02:59.997294Z","iopub.execute_input":"2024-07-08T21:02:59.997697Z","iopub.status.idle":"2024-07-08T21:03:03.790452Z","shell.execute_reply.started":"2024-07-08T21:02:59.997659Z","shell.execute_reply":"2024-07-08T21:03:03.789161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:03.792786Z","iopub.execute_input":"2024-07-08T21:03:03.793161Z","iopub.status.idle":"2024-07-08T21:03:06.008707Z","shell.execute_reply.started":"2024-07-08T21:03:03.793133Z","shell.execute_reply":"2024-07-08T21:03:06.007437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape, test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.010388Z","iopub.execute_input":"2024-07-08T21:03:06.010765Z","iopub.status.idle":"2024-07-08T21:03:06.017849Z","shell.execute_reply.started":"2024-07-08T21:03:06.010736Z","shell.execute_reply":"2024-07-08T21:03:06.016311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=train['label']\ntrain=train.drop(columns=['label'])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.021219Z","iopub.execute_input":"2024-07-08T21:03:06.022275Z","iopub.status.idle":"2024-07-08T21:03:06.145705Z","shell.execute_reply.started":"2024-07-08T21:03:06.022237Z","shell.execute_reply":"2024-07-08T21:03:06.144537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_reshaped=train.to_numpy().reshape(42000,28,28)\ntest_reshaped=test.to_numpy().reshape(28000,28,28)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.147171Z","iopub.execute_input":"2024-07-08T21:03:06.147593Z","iopub.status.idle":"2024-07-08T21:03:06.154446Z","shell.execute_reply.started":"2024-07-08T21:03:06.147555Z","shell.execute_reply":"2024-07-08T21:03:06.152863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nplt.imshow(train_reshaped[0], cmap='gray')\nplt.title('First Image in Train Dataset')\nplt.axis('off')  \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.156112Z","iopub.execute_input":"2024-07-08T21:03:06.156549Z","iopub.status.idle":"2024-07-08T21:03:06.348749Z","shell.execute_reply.started":"2024-07-08T21:03:06.156517Z","shell.execute_reply":"2024-07-08T21:03:06.347366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_reshaped=train_reshaped/255\ntest_reshaped=test_reshaped/255","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.350422Z","iopub.execute_input":"2024-07-08T21:03:06.350811Z","iopub.status.idle":"2024-07-08T21:03:06.701886Z","shell.execute_reply.started":"2024-07-08T21:03:06.350778Z","shell.execute_reply":"2024-07-08T21:03:06.700570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow import keras\nfrom keras.layers import Dense, Conv2D, BatchNormalization, Flatten, Dropout, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.703592Z","iopub.execute_input":"2024-07-08T21:03:06.703996Z","iopub.status.idle":"2024-07-08T21:03:06.711246Z","shell.execute_reply.started":"2024-07-08T21:03:06.703947Z","shell.execute_reply":"2024-07-08T21:03:06.709649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential([\n    Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)),\n    MaxPooling2D(2,2),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    BatchNormalization(),\n    Dense(64,activation='relu'),\n    Dropout(0.3),\n    BatchNormalization(),\n    Dense(10,activation='softmax')]\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.713047Z","iopub.execute_input":"2024-07-08T21:03:06.713494Z","iopub.status.idle":"2024-07-08T21:03:06.799403Z","shell.execute_reply.started":"2024-07-08T21:03:06.713445Z","shell.execute_reply":"2024-07-08T21:03:06.798042Z"},"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-07-08T21:03:06.802782Z","iopub.execute_input":"2024-07-08T21:03:06.803189Z","iopub.status.idle":"2024-07-08T21:03:06.815664Z","shell.execute_reply.started":"2024-07-08T21:03:06.803150Z","shell.execute_reply":"2024-07-08T21:03:06.814368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\nearly_stopping = EarlyStopping(monitor='val_loss', patience=15, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.817409Z","iopub.execute_input":"2024-07-08T21:03:06.817805Z","iopub.status.idle":"2024-07-08T21:03:06.826540Z","shell.execute_reply.started":"2024-07-08T21:03:06.817773Z","shell.execute_reply":"2024-07-08T21:03:06.825180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_reshaped, y, epochs= 20,callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:03:06.828324Z","iopub.execute_input":"2024-07-08T21:03:06.828741Z","iopub.status.idle":"2024-07-08T21:13:06.341616Z","shell.execute_reply.started":"2024-07-08T21:03:06.828702Z","shell.execute_reply":"2024-07-08T21:13:06.340067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(test_reshaped)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:14:46.261520Z","iopub.execute_input":"2024-07-08T21:14:46.262032Z","iopub.status.idle":"2024-07-08T21:14:53.577731Z","shell.execute_reply.started":"2024-07-08T21:14:46.261995Z","shell.execute_reply":"2024-07-08T21:14:53.576298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_labels = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:17:56.731457Z","iopub.execute_input":"2024-07-08T21:17:56.731911Z","iopub.status.idle":"2024-07-08T21:17:56.738715Z","shell.execute_reply.started":"2024-07-08T21:17:56.731877Z","shell.execute_reply":"2024-07-08T21:17:56.737376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test_reshaped[0], cmap='gray')\nplt.title('First Image in Train Dataset')\nplt.axis('off')  \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:18:31.641407Z","iopub.execute_input":"2024-07-08T21:18:31.641886Z","iopub.status.idle":"2024-07-08T21:18:31.776213Z","shell.execute_reply.started":"2024-07-08T21:18:31.641850Z","shell.execute_reply":"2024-07-08T21:18:31.774900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_pred_labels[0])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:18:56.811861Z","iopub.execute_input":"2024-07-08T21:18:56.813299Z","iopub.status.idle":"2024-07-08T21:18:56.820377Z","shell.execute_reply.started":"2024-07-08T21:18:56.813229Z","shell.execute_reply":"2024-07-08T21:18:56.818840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:21:46.701480Z","iopub.execute_input":"2024-07-08T21:21:46.701985Z","iopub.status.idle":"2024-07-08T21:21:46.726049Z","shell.execute_reply.started":"2024-07-08T21:21:46.701935Z","shell.execute_reply":"2024-07-08T21:21:46.724601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output=pd.DataFrame()\noutput['ImageId'] = range(1, len(y_pred_labels) + 1)\noutput['label']=y_pred_labels","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:37:25.672145Z","iopub.execute_input":"2024-07-08T21:37:25.672640Z","iopub.status.idle":"2024-07-08T21:37:25.684394Z","shell.execute_reply.started":"2024-07-08T21:37:25.672607Z","shell.execute_reply":"2024-07-08T21:37:25.682859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:23:20.681459Z","iopub.execute_input":"2024-07-08T21:23:20.681891Z","iopub.status.idle":"2024-07-08T21:23:20.730912Z","shell.execute_reply.started":"2024-07-08T21:23:20.681863Z","shell.execute_reply":"2024-07-08T21:23:20.729731Z"},"trusted":true},"execution_count":null,"outputs":[]}]}