{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.pyplot as plt\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:20:37.040146Z","iopub.execute_input":"2025-07-02T10:20:37.040818Z","iopub.status.idle":"2025-07-02T10:20:50.784553Z","shell.execute_reply.started":"2025-07-02T10:20:37.040790Z","shell.execute_reply":"2025-07-02T10:20:50.783784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:20:58.591594Z","iopub.execute_input":"2025-07-02T10:20:58.592529Z","iopub.status.idle":"2025-07-02T10:21:04.104545Z","shell.execute_reply.started":"2025-07-02T10:20:58.592504Z","shell.execute_reply":"2025-07-02T10:21:04.103949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:04.105749Z","iopub.execute_input":"2025-07-02T10:21:04.105953Z","iopub.status.idle":"2025-07-02T10:21:04.111344Z","shell.execute_reply.started":"2025-07-02T10:21:04.105937Z","shell.execute_reply":"2025-07-02T10:21:04.110840Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:04.111985Z","iopub.execute_input":"2025-07-02T10:21:04.112171Z","iopub.status.idle":"2025-07-02T10:21:04.127073Z","shell.execute_reply.started":"2025-07-02T10:21:04.112148Z","shell.execute_reply":"2025-07-02T10:21:04.126436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:04.128440Z","iopub.execute_input":"2025-07-02T10:21:04.128608Z","iopub.status.idle":"2025-07-02T10:21:04.161056Z","shell.execute_reply.started":"2025-07-02T10:21:04.128595Z","shell.execute_reply":"2025-07-02T10:21:04.160571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_Y = train['label']\ntrain_X = train.drop(labels=['label'], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:04.161590Z","iopub.execute_input":"2025-07-02T10:21:04.161847Z","iopub.status.idle":"2025-07-02T10:21:04.246132Z","shell.execute_reply.started":"2025-07-02T10:21:04.161831Z","shell.execute_reply":"2025-07-02T10:21:04.245571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_X.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:05.169866Z","iopub.execute_input":"2025-07-02T10:21:05.170369Z","iopub.status.idle":"2025-07-02T10:21:05.181222Z","shell.execute_reply.started":"2025-07-02T10:21:05.170348Z","shell.execute_reply":"2025-07-02T10:21:05.180616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_X.isnull().any().describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:06.990851Z","iopub.execute_input":"2025-07-02T10:21:06.991495Z","iopub.status.idle":"2025-07-02T10:21:07.013701Z","shell.execute_reply.started":"2025-07-02T10:21:06.991471Z","shell.execute_reply":"2025-07-02T10:21:07.012901Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_X = train_X/255.0\ntest = test/255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:08.043838Z","iopub.execute_input":"2025-07-02T10:21:08.044114Z","iopub.status.idle":"2025-07-02T10:21:08.136786Z","shell.execute_reply.started":"2025-07-02T10:21:08.044094Z","shell.execute_reply":"2025-07-02T10:21:08.136252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#reshape into 3d\ntrain_X = train_X.values.reshape(-1, 28, 28, 1)\ntest = test.values.reshape(-1, 28, 28, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:09.256944Z","iopub.execute_input":"2025-07-02T10:21:09.257562Z","iopub.status.idle":"2025-07-02T10:21:09.261560Z","shell.execute_reply.started":"2025-07-02T10:21:09.257534Z","shell.execute_reply":"2025-07-02T10:21:09.260665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_Y = to_categorical(train_Y, num_classes=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:10.389787Z","iopub.execute_input":"2025-07-02T10:21:10.390037Z","iopub.status.idle":"2025-07-02T10:21:10.394669Z","shell.execute_reply.started":"2025-07-02T10:21:10.390020Z","shell.execute_reply":"2025-07-02T10:21:10.394030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_Y[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:11.567992Z","iopub.execute_input":"2025-07-02T10:21:11.568571Z","iopub.status.idle":"2025-07-02T10:21:11.573208Z","shell.execute_reply.started":"2025-07-02T10:21:11.568548Z","shell.execute_reply":"2025-07-02T10:21:11.572516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(train_X, train_Y, test_size=0.1, random_state=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:12.809955Z","iopub.execute_input":"2025-07-02T10:21:12.810189Z","iopub.status.idle":"2025-07-02T10:21:13.194986Z","shell.execute_reply.started":"2025-07-02T10:21:12.810174Z","shell.execute_reply":"2025-07-02T10:21:13.194434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:13.890978Z","iopub.execute_input":"2025-07-02T10:21:13.891654Z","iopub.status.idle":"2025-07-02T10:21:13.896047Z","shell.execute_reply.started":"2025-07-02T10:21:13.891630Z","shell.execute_reply":"2025-07-02T10:21:13.895294Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:14.857920Z","iopub.execute_input":"2025-07-02T10:21:14.858607Z","iopub.status.idle":"2025-07-02T10:21:14.862820Z","shell.execute_reply.started":"2025-07-02T10:21:14.858586Z","shell.execute_reply":"2025-07-02T10:21:14.862289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:15.789907Z","iopub.execute_input":"2025-07-02T10:21:15.790507Z","iopub.status.idle":"2025-07-02T10:21:15.794819Z","shell.execute_reply.started":"2025-07-02T10:21:15.790484Z","shell.execute_reply":"2025-07-02T10:21:15.794134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"g = plt.imshow(X_train[1][:,:,0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:16.850319Z","iopub.execute_input":"2025-07-02T10:21:16.851072Z","iopub.status.idle":"2025-07-02T10:21:17.072986Z","shell.execute_reply.started":"2025-07-02T10:21:16.851046Z","shell.execute_reply":"2025-07-02T10:21:17.072343Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop, Adam\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:18.786006Z","iopub.execute_input":"2025-07-02T10:21:18.786273Z","iopub.status.idle":"2025-07-02T10:21:18.792916Z","shell.execute_reply.started":"2025-07-02T10:21:18.786254Z","shell.execute_reply":"2025-07-02T10:21:18.792110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(filters=32, kernel_size=(3, 3), padding='Same', activation='relu', input_shape=(28, 28, 1)),\n    MaxPool2D(pool_size=(2, 2)),\n    Dropout(0.25),\n\n    Conv2D(filters=64, kernel_size=(3, 3), padding='Same', activation='relu'),\n    MaxPool2D(pool_size=(2, 2), strides=(2,2)),\n    Dropout(0.25),\n\n    Conv2D(filters=128, kernel_size=(3, 3), padding='Same', activation='relu'),\n    MaxPool2D(pool_size=(2, 2), strides=(2,2)),\n    Dropout(0.25),\n\n    Conv2D(filters=256, kernel_size=(3, 3), padding='Same', activation='relu'),\n    MaxPool2D(pool_size=(2, 2), strides=(2,2)),\n    Dropout(0.25),\n\n    Flatten(),\n\n    Dense(512, activation='relu'),\n    Dropout(0.3),\n    Dense(256, activation='relu'),\n    Dropout(0.3),\n    Dense(10, activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:20.672205Z","iopub.execute_input":"2025-07-02T10:21:20.672930Z","iopub.status.idle":"2025-07-02T10:21:23.141332Z","shell.execute_reply.started":"2025-07-02T10:21:20.672893Z","shell.execute_reply":"2025-07-02T10:21:23.140369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:24.916560Z","iopub.execute_input":"2025-07-02T10:21:24.917276Z","iopub.status.idle":"2025-07-02T10:21:24.931485Z","shell.execute_reply.started":"2025-07-02T10:21:24.917251Z","shell.execute_reply":"2025-07-02T10:21:24.930799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, \n                    batch_size=256, \n                    epochs=50, \n                    validation_data=(X_val, Y_val), \n                    verbose=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:21:48.289609Z","iopub.execute_input":"2025-07-02T10:21:48.290148Z","iopub.status.idle":"2025-07-02T10:23:01.373624Z","shell.execute_reply.started":"2025-07-02T10:21:48.290126Z","shell.execute_reply":"2025-07-02T10:23:01.372817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:05.179036Z","iopub.execute_input":"2025-07-02T10:23:05.179302Z","iopub.status.idle":"2025-07-02T10:23:07.804355Z","shell.execute_reply.started":"2025-07-02T10:23:05.179282Z","shell.execute_reply":"2025-07-02T10:23:07.803759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:07.805566Z","iopub.execute_input":"2025-07-02T10:23:07.805804Z","iopub.status.idle":"2025-07-02T10:23:07.811067Z","shell.execute_reply.started":"2025-07-02T10:23:07.805787Z","shell.execute_reply":"2025-07-02T10:23:07.810450Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = np.argmax(results, axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:10.734877Z","iopub.execute_input":"2025-07-02T10:23:10.735146Z","iopub.status.idle":"2025-07-02T10:23:10.739698Z","shell.execute_reply.started":"2025-07-02T10:23:10.735127Z","shell.execute_reply":"2025-07-02T10:23:10.738971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:12.013878Z","iopub.execute_input":"2025-07-02T10:23:12.014762Z","iopub.status.idle":"2025-07-02T10:23:12.019817Z","shell.execute_reply.started":"2025-07-02T10:23:12.014737Z","shell.execute_reply":"2025-07-02T10:23:12.018983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = pd.Series(results, name='Label')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:13.095111Z","iopub.execute_input":"2025-07-02T10:23:13.095557Z","iopub.status.idle":"2025-07-02T10:23:13.099173Z","shell.execute_reply.started":"2025-07-02T10:23:13.095536Z","shell.execute_reply":"2025-07-02T10:23:13.098440Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:13.924052Z","iopub.execute_input":"2025-07-02T10:23:13.924608Z","iopub.status.idle":"2025-07-02T10:23:13.930095Z","shell.execute_reply.started":"2025-07-02T10:23:13.924585Z","shell.execute_reply":"2025-07-02T10:23:13.929326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:16.620533Z","iopub.execute_input":"2025-07-02T10:23:16.620831Z","iopub.status.idle":"2025-07-02T10:23:16.626071Z","shell.execute_reply.started":"2025-07-02T10:23:16.620811Z","shell.execute_reply":"2025-07-02T10:23:16.625331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"result_mnist.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T10:23:18.977490Z","iopub.execute_input":"2025-07-02T10:23:18.977754Z","iopub.status.idle":"2025-07-02T10:23:19.005142Z","shell.execute_reply.started":"2025-07-02T10:23:18.977737Z","shell.execute_reply":"2025-07-02T10:23:19.004321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}