{"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":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, BatchNormalization\nfrom tensorflow.keras.utils import to_categorical\n#from tensorflow.keras.datasets import mnist\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-30T17:14:09.934426Z","iopub.execute_input":"2024-07-30T17:14:09.935601Z","iopub.status.idle":"2024-07-30T17:14:09.944526Z","shell.execute_reply.started":"2024-07-30T17:14:09.935559Z","shell.execute_reply":"2024-07-30T17:14:09.943271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the dataset\n#(x_train, y_train), (x_test, y_test) = mnist.load_data()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the dataset\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nsample_submission_data = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\n\ny_train = train[\"label\"]\nx_train = train.drop(labels=[\"label\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:50:29.394906Z","iopub.execute_input":"2024-07-30T17:50:29.395326Z","iopub.status.idle":"2024-07-30T17:50:34.724885Z","shell.execute_reply.started":"2024-07-30T17:50:29.395293Z","shell.execute_reply":"2024-07-30T17:50:34.723614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess the data\nx_train = x_train.values.reshape(-1, 28, 28, 1).astype('float32') / 255\ntest = test.values.reshape(-1, 28, 28, 1).astype('float32') / 255\n    \n# One-hot encode the labels\ny_train = to_categorical(y_train, 10)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:04:38.898146Z","iopub.execute_input":"2024-07-30T17:04:38.898634Z","iopub.status.idle":"2024-07-30T17:04:39.067727Z","shell.execute_reply.started":"2024-07-30T17:04:38.898586Z","shell.execute_reply":"2024-07-30T17:04:39.066540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data\nx_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:04:41.646543Z","iopub.execute_input":"2024-07-30T17:04:41.646984Z","iopub.status.idle":"2024-07-30T17:04:42.047356Z","shell.execute_reply.started":"2024-07-30T17:04:41.646949Z","shell.execute_reply":"2024-07-30T17:04:42.045895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the model\nmodel = Sequential()\n\nmodel.add(Conv2D(filters= 64, \n                 kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(28, 28, 1)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\nmodel.add(Conv2D(filters= 128, \n                 kernel_size=(3, 3), \n                 strides=2, \n                 activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:07:28.374892Z","iopub.execute_input":"2024-07-30T17:07:28.375313Z","iopub.status.idle":"2024-07-30T17:07:28.619757Z","shell.execute_reply.started":"2024-07-30T17:07:28.375280Z","shell.execute_reply":"2024-07-30T17:07:28.618153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callbacks - EarlyStopping\nearly_stopping_val = EarlyStopping(monitor='val_loss', patience=15, verbose=1, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:15:07.325243Z","iopub.execute_input":"2024-07-30T17:15:07.325701Z","iopub.status.idle":"2024-07-30T17:15:07.332353Z","shell.execute_reply.started":"2024-07-30T17:15:07.325646Z","shell.execute_reply":"2024-07-30T17:15:07.331065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nmodel.fit(x_train, y_train, validation_data=(x_test, y_test), callbacks=[early_stopping_val], epochs=5, batch_size=32, validation_split=0.2, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:15:13.049490Z","iopub.execute_input":"2024-07-30T17:15:13.050055Z","iopub.status.idle":"2024-07-30T17:24:19.805514Z","shell.execute_reply.started":"2024-07-30T17:15:13.050012Z","shell.execute_reply":"2024-07-30T17:24:19.804216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions\npredictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:48:05.957485Z","iopub.execute_input":"2024-07-30T17:48:05.958009Z","iopub.status.idle":"2024-07-30T17:48:20.786023Z","shell.execute_reply.started":"2024-07-30T17:48:05.957971Z","shell.execute_reply":"2024-07-30T17:48:20.784759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:49:12.983767Z","iopub.execute_input":"2024-07-30T17:49:12.984228Z","iopub.status.idle":"2024-07-30T17:49:12.992868Z","shell.execute_reply.started":"2024-07-30T17:49:12.984193Z","shell.execute_reply":"2024-07-30T17:49:12.991667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_res = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:48:57.033889Z","iopub.execute_input":"2024-07-30T17:48:57.034308Z","iopub.status.idle":"2024-07-30T17:48:57.041383Z","shell.execute_reply.started":"2024-07-30T17:48:57.034275Z","shell.execute_reply":"2024-07-30T17:48:57.040180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_res","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:49:02.473689Z","iopub.execute_input":"2024-07-30T17:49:02.474136Z","iopub.status.idle":"2024-07-30T17:49:02.482260Z","shell.execute_reply.started":"2024-07-30T17:49:02.474100Z","shell.execute_reply":"2024-07-30T17:49:02.480942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n        'ImageId': sample_submission_data['ImageId'],\n        'Label': predictions_res\n    })\n\nsubmission.to_csv('submission_fin.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T17:51:44.471698Z","iopub.execute_input":"2024-07-30T17:51:44.473138Z","iopub.status.idle":"2024-07-30T17:51:44.522789Z","shell.execute_reply.started":"2024-07-30T17:51:44.473099Z","shell.execute_reply":"2024-07-30T17:51:44.521625Z"},"trusted":true},"execution_count":null,"outputs":[]}]}