{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.utils import to_categorical","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-18T12:31:40.469607Z","iopub.execute_input":"2025-06-18T12:31:40.469987Z","iopub.status.idle":"2025-06-18T12:31:40.485592Z","shell.execute_reply.started":"2025-06-18T12:31:40.469963Z","shell.execute_reply":"2025-06-18T12:31:40.484535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_data=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(all_data.shape)\nall_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T12:49:59.014970Z","iopub.execute_input":"2025-06-18T12:49:59.015319Z","iopub.status.idle":"2025-06-18T12:50:01.498948Z","shell.execute_reply.started":"2025-06-18T12:49:59.015298Z","shell.execute_reply":"2025-06-18T12:50:01.498109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = all_data.iloc[:, 1:]\ny = all_data.iloc[:, 0]\n\nprint(X.shape)\nprint(y.shape)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:23:04.714593Z","iopub.execute_input":"2025-06-18T13:23:04.714923Z","iopub.status.idle":"2025-06-18T13:23:04.721107Z","shell.execute_reply.started":"2025-06-18T13:23:04.714898Z","shell.execute_reply":"2025-06-18T13:23:04.720072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if not isinstance(X, pd.DataFrame):\n    X = pd.DataFrame(X)\nX = X.apply(pd.to_numeric, errors='coerce')\nX = X.fillna(0)  \nX = X.values / 255.0\nX = X.reshape(-1, 28, 28, 1)\nprint(X.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:23:07.351666Z","iopub.execute_input":"2025-06-18T13:23:07.352478Z","iopub.status.idle":"2025-06-18T13:23:07.834280Z","shell.execute_reply.started":"2025-06-18T13:23:07.352447Z","shell.execute_reply":"2025-06-18T13:23:07.832919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_one_hot = to_categorical(y, num_classes=10)\nprint(\"X shape:\", X.shape)\nprint(\"y_one_hot shape:\", y_one_hot.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:23:14.212010Z","iopub.execute_input":"2025-06-18T13:23:14.212711Z","iopub.status.idle":"2025-06-18T13:23:14.247942Z","shell.execute_reply.started":"2025-06-18T13:23:14.212663Z","shell.execute_reply":"2025-06-18T13:23:14.246807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(\n    X, y_one_hot,         \n    test_size=0.2,         \n    random_state=42,       \n    shuffle=True           \n)\nprint(\"X_train shape:\", X_train.shape)\nprint(\"y_train shape:\", y_train.shape)\nprint(\"X_val shape:\", X_val.shape)\nprint(\"y_val shape:\", y_val.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:24:06.316495Z","iopub.execute_input":"2025-06-18T13:24:06.316809Z","iopub.status.idle":"2025-06-18T13:24:06.758497Z","shell.execute_reply.started":"2025-06-18T13:24:06.316785Z","shell.execute_reply":"2025-06-18T13:24:06.756941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Flatten(input_shape=(28, 28, 1)), \n    Dense(128, activation='relu'),     \n    Dense(64, activation='relu'),      \n    Dense(10, activation='softmax')    \n])\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:24:53.756618Z","iopub.execute_input":"2025-06-18T13:24:53.756956Z","iopub.status.idle":"2025-06-18T13:24:53.947084Z","shell.execute_reply.started":"2025-06-18T13:24:53.756931Z","shell.execute_reply":"2025-06-18T13:24:53.946048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:28:00.745416Z","iopub.execute_input":"2025-06-18T13:28:00.745738Z","iopub.status.idle":"2025-06-18T13:28:52.795341Z","shell.execute_reply.started":"2025-06-18T13:28:00.745717Z","shell.execute_reply":"2025-06-18T13:28:52.794346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_loss, val_accuracy = model.evaluate(X_val, y_val)\nprint(f\"Validation Accuracy: {val_accuracy * 100:.2f}%\")\nprint(f\"Validation Loss: {val_loss:.4f}\")\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:38:51.164510Z","iopub.execute_input":"2025-06-18T13:38:51.164825Z","iopub.status.idle":"2025-06-18T13:38:52.156794Z","shell.execute_reply.started":"2025-06-18T13:38:51.164802Z","shell.execute_reply":"2025-06-18T13:38:52.155971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test = test_data.values / 255.0\nX_test = X_test.reshape(-1, 28, 28, 1)\npredictions = model.predict(X_test)\npredicted_labels = np.argmax(predictions, axis=1)\nfor i in range(5):\n    plt.imshow(X_test[i].reshape(28, 28), cmap='gray')\n    plt.title(f\"Predicted: {predicted_labels[i]}\")\n    plt.axis('off')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-18T13:30:16.140697Z","iopub.execute_input":"2025-06-18T13:30:16.141047Z","iopub.status.idle":"2025-06-18T13:30:22.080507Z","shell.execute_reply.started":"2025-06-18T13:30:16.141023Z","shell.execute_reply":"2025-06-18T13:30:22.078796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}