{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:07:55.619715Z","iopub.execute_input":"2025-04-13T10:07:55.620157Z","iopub.status.idle":"2025-04-13T10:08:08.849131Z","shell.execute_reply.started":"2025-04-13T10:07:55.620117Z","shell.execute_reply":"2025-04-13T10:08:08.848438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:08.850049Z","iopub.execute_input":"2025-04-13T10:08:08.850424Z","iopub.status.idle":"2025-04-13T10:08:13.079597Z","shell.execute_reply.started":"2025-04-13T10:08:08.850404Z","shell.execute_reply":"2025-04-13T10:08:13.078684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:13.081595Z","iopub.execute_input":"2025-04-13T10:08:13.081942Z","iopub.status.idle":"2025-04-13T10:08:13.105795Z","shell.execute_reply.started":"2025-04-13T10:08:13.081919Z","shell.execute_reply":"2025-04-13T10:08:13.104982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:13.106952Z","iopub.execute_input":"2025-04-13T10:08:13.107200Z","iopub.status.idle":"2025-04-13T10:08:14.767008Z","shell.execute_reply.started":"2025-04-13T10:08:13.107163Z","shell.execute_reply":"2025-04-13T10:08:14.766128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df_train.drop(\"label\", axis=1)\ny = df_train[\"label\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:14.768043Z","iopub.execute_input":"2025-04-13T10:08:14.768393Z","iopub.status.idle":"2025-04-13T10:08:14.852233Z","shell.execute_reply.started":"2025-04-13T10:08:14.768359Z","shell.execute_reply":"2025-04-13T10:08:14.851511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data0 = np.matrix(X.iloc[0]).reshape(28,28)\nplt.imshow(data0, cmap=\"gray\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:14.853035Z","iopub.execute_input":"2025-04-13T10:08:14.853254Z","iopub.status.idle":"2025-04-13T10:08:15.087385Z","shell.execute_reply.started":"2025-04-13T10:08:14.853235Z","shell.execute_reply":"2025-04-13T10:08:15.086516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.isnull().sum().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:15.088261Z","iopub.execute_input":"2025-04-13T10:08:15.088484Z","iopub.status.idle":"2025-04-13T10:08:15.125722Z","shell.execute_reply.started":"2025-04-13T10:08:15.088463Z","shell.execute_reply":"2025-04-13T10:08:15.124895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X.astype('float32')/255.0\nX.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:15.126751Z","iopub.execute_input":"2025-04-13T10:08:15.127082Z","iopub.status.idle":"2025-04-13T10:08:15.253415Z","shell.execute_reply.started":"2025-04-13T10:08:15.127047Z","shell.execute_reply":"2025-04-13T10:08:15.252687Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:15.255659Z","iopub.execute_input":"2025-04-13T10:08:15.255881Z","iopub.status.idle":"2025-04-13T10:08:17.428358Z","shell.execute_reply.started":"2025-04-13T10:08:15.255862Z","shell.execute_reply":"2025-04-13T10:08:17.427603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:17.429569Z","iopub.execute_input":"2025-04-13T10:08:17.429855Z","iopub.status.idle":"2025-04-13T10:08:17.642840Z","shell.execute_reply.started":"2025-04-13T10:08:17.429834Z","shell.execute_reply":"2025-04-13T10:08:17.641974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = keras.Sequential([\n    layers.Input(shape=(784,)),\n    layers.Reshape((28, 28, 1)),\n\n    layers.RandomRotation(factor=0.04, fill_mode='nearest'),\n    layers.RandomTranslation(height_factor=0.1, width_factor=0.1, fill_mode='nearest'),\n    layers.RandomZoom(height_factor=(-0.1, 0.1), width_factor=(-0.1, 0.1), fill_mode='nearest'),\n\n    layers.Conv2D(filters=64, kernel_size=3, padding='same'),\n    layers.LeakyReLU(negative_slope=0.1),\n    layers.MaxPool2D(),\n\n    layers.Conv2D(filters=128, kernel_size=3, padding='same'),\n    layers.LeakyReLU(negative_slope=0.1),\n    layers.MaxPool2D(),\n\n    layers.Conv2D(filters=256, kernel_size=3, padding='same'),\n    layers.BatchNormalization(),\n    layers.LeakyReLU(negative_slope=0.1),\n    layers.Conv2D(filters=256, kernel_size=3, padding='same'),\n    layers.BatchNormalization(),\n    layers.LeakyReLU(negative_slope=0.1),\n    layers.MaxPool2D(),\n\n    layers.Flatten(),\n    layers.Dense(128),\n    layers.BatchNormalization(),\n    layers.LeakyReLU(negative_slope=0.15),\n    layers.Dense(64),\n    layers.BatchNormalization(),\n    layers.LeakyReLU(negative_slope=0.15),\n    layers.Dense(16),\n    layers.LeakyReLU(negative_slope=0.15),\n    layers.Dense(10, activation='softmax'),\n])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:17.643755Z","iopub.execute_input":"2025-04-13T10:08:17.644329Z","iopub.status.idle":"2025-04-13T10:08:19.867492Z","shell.execute_reply.started":"2025-04-13T10:08:17.644293Z","shell.execute_reply":"2025-04-13T10:08:19.866605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:19.868375Z","iopub.execute_input":"2025-04-13T10:08:19.868586Z","iopub.status.idle":"2025-04-13T10:08:19.879780Z","shell.execute_reply.started":"2025-04-13T10:08:19.868569Z","shell.execute_reply":"2025-04-13T10:08:19.879062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:19.880585Z","iopub.execute_input":"2025-04-13T10:08:19.880910Z","iopub.status.idle":"2025-04-13T10:08:19.914097Z","shell.execute_reply.started":"2025-04-13T10:08:19.880881Z","shell.execute_reply":"2025-04-13T10:08:19.913335Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"early_stop = EarlyStopping(\n    monitor='val_accuracy',\n    patience=20,\n    restore_best_weights=True,\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:19.914803Z","iopub.execute_input":"2025-04-13T10:08:19.915011Z","iopub.status.idle":"2025-04-13T10:08:19.918520Z","shell.execute_reply.started":"2025-04-13T10:08:19.914993Z","shell.execute_reply":"2025-04-13T10:08:19.917634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x=X_train,\n    y=y_train,\n    validation_data=(X_val, y_val),\n    epochs=100,\n    batch_size=64,\n    callbacks=[early_stop]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:08:19.919436Z","iopub.execute_input":"2025-04-13T10:08:19.919673Z","iopub.status.idle":"2025-04-13T10:13:21.391334Z","shell.execute_reply.started":"2025-04-13T10:08:19.919653Z","shell.execute_reply":"2025-04-13T10:13:21.390556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['accuracy', 'val_accuracy']].plot();","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:21.392373Z","iopub.execute_input":"2025-04-13T10:13:21.392675Z","iopub.status.idle":"2025-04-13T10:13:21.835016Z","shell.execute_reply.started":"2025-04-13T10:13:21.392629Z","shell.execute_reply":"2025-04-13T10:13:21.834027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_val, y_val, verbose=1)\nprint(f\"Validation Loss: {loss}\")\nprint(f\"Validation Accuracy: {accuracy}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:21.835993Z","iopub.execute_input":"2025-04-13T10:13:21.836315Z","iopub.status.idle":"2025-04-13T10:13:22.453656Z","shell.execute_reply.started":"2025-04-13T10:13:21.836291Z","shell.execute_reply":"2025-04-13T10:13:22.452995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_probs = model.predict(X_val)\ny_pred = np.argmax(y_pred_probs, axis=1)\ny_true = y_val.to_numpy().astype(int) if hasattr(y_val, 'to_numpy') else y_val","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:22.454457Z","iopub.execute_input":"2025-04-13T10:13:22.454706Z","iopub.status.idle":"2025-04-13T10:13:23.086904Z","shell.execute_reply.started":"2025-04-13T10:13:22.454674Z","shell.execute_reply":"2025-04-13T10:13:23.086166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cm = confusion_matrix(y_true, y_pred)\nprint(\"Confusion Matrix:\")\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=np.arange(10), yticklabels=np.arange(10))\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.show()\n\nprint(\"\\nClassification Report:\")\nreport = classification_report(y_true, y_pred, digits=4, output_dict=True)\npd.DataFrame(report).transpose()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:23.087711Z","iopub.execute_input":"2025-04-13T10:13:23.088018Z","iopub.status.idle":"2025-04-13T10:13:23.509816Z","shell.execute_reply.started":"2025-04-13T10:13:23.087988Z","shell.execute_reply":"2025-04-13T10:13:23.509094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Test = df_test.astype('float32')/255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:23.510644Z","iopub.execute_input":"2025-04-13T10:13:23.510950Z","iopub.status.idle":"2025-04-13T10:13:23.570322Z","shell.execute_reply.started":"2025-04-13T10:13:23.510916Z","shell.execute_reply":"2025-04-13T10:13:23.569495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_probs = model.predict(Test)\npred = np.argmax(pred_probs, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:23.571183Z","iopub.execute_input":"2025-04-13T10:13:23.571398Z","iopub.status.idle":"2025-04-13T10:13:25.717207Z","shell.execute_reply.started":"2025-04-13T10:13:23.571378Z","shell.execute_reply":"2025-04-13T10:13:25.716237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsubmission_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:25.718121Z","iopub.execute_input":"2025-04-13T10:13:25.718363Z","iopub.status.idle":"2025-04-13T10:13:25.738057Z","shell.execute_reply.started":"2025-04-13T10:13:25.718337Z","shell.execute_reply":"2025-04-13T10:13:25.737342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.DataFrame(pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:25.738838Z","iopub.execute_input":"2025-04-13T10:13:25.739148Z","iopub.status.idle":"2025-04-13T10:13:25.746371Z","shell.execute_reply.started":"2025-04-13T10:13:25.739118Z","shell.execute_reply":"2025-04-13T10:13:25.745678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df[\"Label\"] = pred\nsubmission_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:25.747313Z","iopub.execute_input":"2025-04-13T10:13:25.747558Z","iopub.status.idle":"2025-04-13T10:13:25.765586Z","shell.execute_reply.started":"2025-04-13T10:13:25.747538Z","shell.execute_reply":"2025-04-13T10:13:25.764923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T10:13:25.766475Z","iopub.execute_input":"2025-04-13T10:13:25.766802Z","iopub.status.idle":"2025-04-13T10:13:25.800884Z","shell.execute_reply.started":"2025-04-13T10:13:25.766771Z","shell.execute_reply":"2025-04-13T10:13:25.800166Z"}},"outputs":[],"execution_count":null}]}