{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Objective**\nIn this project we have to build nural network which can pridict / classify digits from imgs in this MNIST data set we have given img with 784/(28,28) pixals in grayscale","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport seaborn as sns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:11.146122Z","iopub.execute_input":"2025-07-22T12:12:11.146910Z","iopub.status.idle":"2025-07-22T12:12:29.990107Z","shell.execute_reply.started":"2025-07-22T12:12:11.146878Z","shell.execute_reply":"2025-07-22T12:12:29.988878Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# LOADING AND ANALYSING DATA ","metadata":{}},{"cell_type":"code","source":"Data=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nData.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:29.991613Z","iopub.execute_input":"2025-07-22T12:12:29.992399Z","iopub.status.idle":"2025-07-22T12:12:33.447505Z","shell.execute_reply.started":"2025-07-22T12:12:29.992372Z","shell.execute_reply":"2025-07-22T12:12:33.446616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:33.448557Z","iopub.execute_input":"2025-07-22T12:12:33.449066Z","iopub.status.idle":"2025-07-22T12:12:33.454941Z","shell.execute_reply.started":"2025-07-22T12:12:33.449034Z","shell.execute_reply":"2025-07-22T12:12:33.453980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Saperating pixels and lebels\nLabel=Data.pop('label')\nLabel.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:33.456040Z","iopub.execute_input":"2025-07-22T12:12:33.456365Z","iopub.status.idle":"2025-07-22T12:12:33.514029Z","shell.execute_reply.started":"2025-07-22T12:12:33.456335Z","shell.execute_reply":"2025-07-22T12:12:33.513024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Label.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:33.516285Z","iopub.execute_input":"2025-07-22T12:12:33.516541Z","iopub.status.idle":"2025-07-22T12:12:33.522943Z","shell.execute_reply.started":"2025-07-22T12:12:33.516519Z","shell.execute_reply":"2025-07-22T12:12:33.522077Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"> no missing values","metadata":{}},{"cell_type":"code","source":"Data=np.array(Data)\nData.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:33.523811Z","iopub.execute_input":"2025-07-22T12:12:33.524043Z","iopub.status.idle":"2025-07-22T12:12:33.785520Z","shell.execute_reply.started":"2025-07-22T12:12:33.524022Z","shell.execute_reply":"2025-07-22T12:12:33.784702Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualising data","metadata":{}},{"cell_type":"code","source":"Data=Data.reshape(42000,28,28)\nData.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:33.786705Z","iopub.execute_input":"2025-07-22T12:12:33.787020Z","iopub.status.idle":"2025-07-22T12:12:33.793331Z","shell.execute_reply.started":"2025-07-22T12:12:33.786996Z","shell.execute_reply":"2025-07-22T12:12:33.792097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(Data[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:33.794304Z","iopub.execute_input":"2025-07-22T12:12:33.794572Z","iopub.status.idle":"2025-07-22T12:12:34.140618Z","shell.execute_reply.started":"2025-07-22T12:12:33.794545Z","shell.execute_reply":"2025-07-22T12:12:34.139907Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Spiltting data for Validation","metadata":{}},{"cell_type":"code","source":"split_idx = int(Data.shape[0] * 0.8)\n\nX_train = Data[:split_idx]\ny_train = Label[:split_idx]\nX_val = Data[split_idx:]\ny_val = Label[split_idx:]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.141576Z","iopub.execute_input":"2025-07-22T12:12:34.142055Z","iopub.status.idle":"2025-07-22T12:12:34.147251Z","shell.execute_reply.started":"2025-07-22T12:12:34.142021Z","shell.execute_reply":"2025-07-22T12:12:34.146290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.148107Z","iopub.execute_input":"2025-07-22T12:12:34.148399Z","iopub.status.idle":"2025-07-22T12:12:34.167248Z","shell.execute_reply.started":"2025-07-22T12:12:34.148376Z","shell.execute_reply":"2025-07-22T12:12:34.166238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.168073Z","iopub.execute_input":"2025-07-22T12:12:34.168290Z","iopub.status.idle":"2025-07-22T12:12:34.186624Z","shell.execute_reply.started":"2025-07-22T12:12:34.168271Z","shell.execute_reply":"2025-07-22T12:12:34.185698Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"*Chack Balence*","metadata":{}},{"cell_type":"code","source":"sns.countplot(\n    x=y_train,\n    palette=\"Set1\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.187497Z","iopub.execute_input":"2025-07-22T12:12:34.188183Z","iopub.status.idle":"2025-07-22T12:12:34.387004Z","shell.execute_reply.started":"2025-07-22T12:12:34.188155Z","shell.execute_reply":"2025-07-22T12:12:34.386104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(\n    x=y_val,\n    palette=\"Set1\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.387864Z","iopub.execute_input":"2025-07-22T12:12:34.388161Z","iopub.status.idle":"2025-07-22T12:12:34.552762Z","shell.execute_reply.started":"2025-07-22T12:12:34.388140Z","shell.execute_reply":"2025-07-22T12:12:34.551992Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Observation:**\n*data is well distributed and balanced*","metadata":{}},{"cell_type":"markdown","source":"# Preprocessing data","metadata":{}},{"cell_type":"markdown","source":"> 1.Adding channel dimention","metadata":{}},{"cell_type":"code","source":"X_train.shape\nX_train=X_train.reshape(X_train.shape[0],X_train.shape[1],X_train.shape[2],1)\nX_val=X_val.reshape(X_val.shape[0],X_val.shape[1],X_val.shape[2],1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.555723Z","iopub.execute_input":"2025-07-22T12:12:34.555989Z","iopub.status.idle":"2025-07-22T12:12:34.560462Z","shell.execute_reply.started":"2025-07-22T12:12:34.555970Z","shell.execute_reply":"2025-07-22T12:12:34.559561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.561395Z","iopub.execute_input":"2025-07-22T12:12:34.561989Z","iopub.status.idle":"2025-07-22T12:12:34.578651Z","shell.execute_reply.started":"2025-07-22T12:12:34.561960Z","shell.execute_reply":"2025-07-22T12:12:34.577766Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"> Scal Pixel values to [0,1] ","metadata":{}},{"cell_type":"code","source":"X_train=X_train.astype('float32')/255.0\nX_val=X_val.astype('float32')/255.0\n\nprint(f'[{X_train.min()},{X_train.max()}],[{X_val.min()},{X_val.max()}]')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.579680Z","iopub.execute_input":"2025-07-22T12:12:34.580025Z","iopub.status.idle":"2025-07-22T12:12:34.747703Z","shell.execute_reply.started":"2025-07-22T12:12:34.579994Z","shell.execute_reply":"2025-07-22T12:12:34.746764Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"> One Hot encode lables","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n# Apply one-hot encoding to y_train and y_test\ny_train = to_categorical(y_train)\ny_val = to_categorical(y_val)\n\n# Verify one-hot encoding\nprint(np.unique(y_train))\nprint(np.unique(y_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.748530Z","iopub.execute_input":"2025-07-22T12:12:34.748818Z","iopub.status.idle":"2025-07-22T12:12:34.874554Z","shell.execute_reply.started":"2025-07-22T12:12:34.748796Z","shell.execute_reply":"2025-07-22T12:12:34.873509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.876084Z","iopub.execute_input":"2025-07-22T12:12:34.877910Z","iopub.status.idle":"2025-07-22T12:12:34.883418Z","shell.execute_reply.started":"2025-07-22T12:12:34.877876Z","shell.execute_reply":"2025-07-22T12:12:34.882528Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"> Visualising","metadata":{}},{"cell_type":"code","source":"print(y_train[0])\nplt.imshow(X_train[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:34.884188Z","iopub.execute_input":"2025-07-22T12:12:34.884481Z","iopub.status.idle":"2025-07-22T12:12:35.092526Z","shell.execute_reply.started":"2025-07-22T12:12:34.884454Z","shell.execute_reply":"2025-07-22T12:12:35.091615Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Buindling CNN for img recognision\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import Input\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\nnum_classes=10\n\ncnn=Sequential([\n    Input(shape=X_val[0].shape),\n    #Block1\n    Conv2D(64,(3,3),padding='valid',activation='relu'),\n    Conv2D(64,(3,3),padding='valid',activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.3),\n\n    #Block2\n    Conv2D(64, (3,3), padding='valid', activation='relu'),\n    Conv2D(64, (3,3), padding='valid', activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.3),\n\n    #Output layer\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(num_classes, activation='softmax')\n    \n])\n\ncnn.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:35.093558Z","iopub.execute_input":"2025-07-22T12:12:35.093918Z","iopub.status.idle":"2025-07-22T12:12:35.260784Z","shell.execute_reply.started":"2025-07-22T12:12:35.093888Z","shell.execute_reply":"2025-07-22T12:12:35.259949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\n# CallBack\nearly_stop=EarlyStopping(\n    monitor='val_loss',\n    patience=7,\n    restore_best_weights=True\n)\n\ncheckpoint= ModelCheckpoint(\n    'best_cnn.keras',\n    monitor='val_loss',\n    save_best_only=True\n)\n\nreduce_lr = ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.2, # Reduce LR by 20%\n    patience=3, # Wait 3 epochs before reducing\n    min_lr=1e-6 # Minimum LR\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:35.261647Z","iopub.execute_input":"2025-07-22T12:12:35.261905Z","iopub.status.idle":"2025-07-22T12:12:35.269540Z","shell.execute_reply.started":"2025-07-22T12:12:35.261886Z","shell.execute_reply":"2025-07-22T12:12:35.268781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnn.compile(\n    optimizer=tf.keras.optimizers.RMSprop(epsilon=1e-06),\n    loss='categorical_crossentropy',\n    metrics=['acc']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:12:35.270408Z","iopub.execute_input":"2025-07-22T12:12:35.270707Z","iopub.status.idle":"2025-07-22T12:12:35.297196Z","shell.execute_reply.started":"2025-07-22T12:12:35.270680Z","shell.execute_reply":"2025-07-22T12:12:35.296158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = cnn.fit(\n    X_train, y_train,\n    validation_data=(X_val,y_val),\n    epochs=20,\n    batch_size=64,\n    callbacks=[early_stop, checkpoint, reduce_lr]\n)\n\nplt.figure(figsize=(12, 5))\n\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Training Loss', color='blue')\nplt.plot(history.history['val_loss'], label='Validation Loss', color='orange')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training vs Validation Loss')\nplt.legend()\nplt.grid(True)\n\n# Plotting Accuracy\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='Training Accuracy', color='green')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy', color='red')\nplt.xlabel('Epochs')\nplt.ylabel('Acc')\nplt.title('Training vs Validation Accuracy')\nplt.legend()\nplt.grid(True)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:13:24.701561Z","iopub.execute_input":"2025-07-22T12:13:24.701911Z","iopub.status.idle":"2025-07-22T12:38:28.016383Z","shell.execute_reply.started":"2025-07-22T12:13:24.701886Z","shell.execute_reply":"2025-07-22T12:38:28.015144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.subplot(1, 2, 2)\nplt.plot(history.history['acc'], label='Training Accuracy', color='green')\nplt.plot(history.history['val_acc'], label='Validation Accuracy', color='red')\nplt.xlabel('Epochs')\nplt.ylabel('Acc')\nplt.title('Training vs Validation Accuracy')\nplt.legend()\nplt.grid(True)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:39:29.270440Z","iopub.execute_input":"2025-07-22T12:39:29.270768Z","iopub.status.idle":"2025-07-22T12:39:29.463999Z","shell.execute_reply.started":"2025-07-22T12:39:29.270709Z","shell.execute_reply":"2025-07-22T12:39:29.463108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=cnn.predict(X_val)\ny_pred_class=np.argmax(y_pred,axis=1)\ny_true=np.argmax(y_val,axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:42:33.060862Z","iopub.execute_input":"2025-07-22T12:42:33.061478Z","iopub.status.idle":"2025-07-22T12:42:38.108473Z","shell.execute_reply.started":"2025-07-22T12:42:33.061449Z","shell.execute_reply":"2025-07-22T12:42:38.107474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=y_pred_class","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:46:16.200021Z","iopub.execute_input":"2025-07-22T12:46:16.200340Z","iopub.status.idle":"2025-07-22T12:46:16.204752Z","shell.execute_reply.started":"2025-07-22T12:46:16.200315Z","shell.execute_reply":"2025-07-22T12:46:16.203926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_true","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:43:03.120397Z","iopub.execute_input":"2025-07-22T12:43:03.120778Z","iopub.status.idle":"2025-07-22T12:43:03.126672Z","shell.execute_reply.started":"2025-07-22T12:43:03.120724Z","shell.execute_reply":"2025-07-22T12:43:03.125865Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluating models performance","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\n# Compute metrics\nacc = accuracy_score(y_true, y_pred) * 100\nprecision = precision_score(y_true, y_pred, average=\"weighted\")\nrecall = recall_score(y_true, y_pred, average=\"weighted\")\nf1 = f1_score(y_true, y_pred, average=\"weighted\")\n\n# Save to results\nresults = {\n    \"CNN\": {\n        \"Accuracy(%)\": acc,\n        \"Precision\": precision,\n        \"Recall\": recall,\n        \"F1-Score\": f1\n    }\n}\n\n# Print metrics\nprint(f\"Model: CNN\")\nprint(f\"Accuracy: {acc:.2f}%\")\nprint(f\"Precision: {precision:.4f}\")\nprint(f\"Recall: {recall:.4f}\")\nprint(f\"F1-Score: {f1:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:46:18.384967Z","iopub.execute_input":"2025-07-22T12:46:18.385272Z","iopub.status.idle":"2025-07-22T12:46:18.411603Z","shell.execute_reply.started":"2025-07-22T12:46:18.385248Z","shell.execute_reply":"2025-07-22T12:46:18.410666Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"> trying to see where our model lags","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\nprint(\"CNN Classification Report\")\nprint(classification_report(y_true, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:48:37.980559Z","iopub.execute_input":"2025-07-22T12:48:37.981269Z","iopub.status.idle":"2025-07-22T12:48:38.010603Z","shell.execute_reply.started":"2025-07-22T12:48:37.981236Z","shell.execute_reply":"2025-07-22T12:48:38.009418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\ncnn_cm = confusion_matrix(y_true, y_pred)\nplt.figure(figsize=(8,6))\nsns.heatmap(cnn_cm, annot=True, fmt=\"d\", cmap=\"Blues\")\nplt.title(\"CNN Confusion Matrix\")\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"Actual\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:49:44.045399Z","iopub.execute_input":"2025-07-22T12:49:44.045705Z","iopub.status.idle":"2025-07-22T12:49:44.438756Z","shell.execute_reply.started":"2025-07-22T12:49:44.045681Z","shell.execute_reply":"2025-07-22T12:49:44.437799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get indices where predictions don't match true labels\nmisclassified_idx = np.where(y_pred != y_true)[0]\nprint(f\"Total misclassified samples: {len(misclassified_idx)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:50:38.421076Z","iopub.execute_input":"2025-07-22T12:50:38.421400Z","iopub.status.idle":"2025-07-22T12:50:38.428203Z","shell.execute_reply.started":"2025-07-22T12:50:38.421374Z","shell.execute_reply":"2025-07-22T12:50:38.426761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot misclassified examples\nplt.figure(figsize=(10,10))\nfor i, idx in enumerate(misclassified_idx[:9]):  # Show first 9\n    plt.subplot(3,3,i+1)\n    plt.imshow(X_val[idx].reshape(28,28), cmap='gray')\n    plt.title(f\"True: {y_true[idx]}, Pred: {y_pred[idx]}\")\n    plt.axis('off')\nplt.suptitle(\"Misclassified Examples\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:51:59.925457Z","iopub.execute_input":"2025-07-22T12:51:59.926178Z","iopub.status.idle":"2025-07-22T12:52:00.486504Z","shell.execute_reply.started":"2025-07-22T12:51:59.926146Z","shell.execute_reply":"2025-07-22T12:52:00.485562Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Creating Submission ","metadata":{}},{"cell_type":"code","source":"test_data=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:54:24.799678Z","iopub.execute_input":"2025-07-22T12:54:24.800068Z","iopub.status.idle":"2025-07-22T12:54:26.728570Z","shell.execute_reply.started":"2025-07-22T12:54:24.800044Z","shell.execute_reply":"2025-07-22T12:54:26.727787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data=np.array(test_data)\ntest_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T12:59:36.479706Z","iopub.execute_input":"2025-07-22T12:59:36.480378Z","iopub.status.idle":"2025-07-22T12:59:36.582556Z","shell.execute_reply.started":"2025-07-22T12:59:36.480353Z","shell.execute_reply":"2025-07-22T12:59:36.581696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data=test_data.reshape(test_data.shape[0],28,28)\ntest_data[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T13:00:51.080764Z","iopub.execute_input":"2025-07-22T13:00:51.081085Z","iopub.status.idle":"2025-07-22T13:00:51.088561Z","shell.execute_reply.started":"2025-07-22T13:00:51.081059Z","shell.execute_reply":"2025-07-22T13:00:51.087784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction=cnn.predict(test_data)\nLabel=np.argmax(prediction,axis=1)\nImageId=np.arange(1,test_data.shape[0]+1)\nsubmission=pd.DataFrame({'ImageId':ImageId,'Label':Label})\nsubmission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T13:01:05.852432Z","iopub.execute_input":"2025-07-22T13:01:05.852774Z","iopub.status.idle":"2025-07-22T13:01:23.210367Z","shell.execute_reply.started":"2025-07-22T13:01:05.852723Z","shell.execute_reply":"2025-07-22T13:01:23.209229Z"}},"outputs":[],"execution_count":null}]}