{"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":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-05-22T18:00:03.412899Z","iopub.execute_input":"2024-05-22T18:00:03.413511Z","iopub.status.idle":"2024-05-22T18:00:03.420110Z","shell.execute_reply.started":"2024-05-22T18:00:03.413472Z","shell.execute_reply":"2024-05-22T18:00:03.418372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:58:46.329107Z","iopub.execute_input":"2024-05-22T17:58:46.329529Z","iopub.status.idle":"2024-05-22T17:58:50.052370Z","shell.execute_reply.started":"2024-05-22T17:58:46.329496Z","shell.execute_reply":"2024-05-22T17:58:50.051052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate the features and labels\nx_train = data.drop('label', axis=1)\ny_train = data['label']\n\n# Normalize the pixel data\nx_train = tf.keras.utils.normalize(x_train, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:58:50.054076Z","iopub.execute_input":"2024-05-22T17:58:50.054591Z","iopub.status.idle":"2024-05-22T17:58:50.848017Z","shell.execute_reply.started":"2024-05-22T17:58:50.054550Z","shell.execute_reply":"2024-05-22T17:58:50.844960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the model\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Dense(128, activation='relu'),  # Hidden layer with 128 neurons\n    tf.keras.layers.Dropout(0.2),  # Dropout layer to reduce overfitting\n    tf.keras.layers.Dense(64, activation='relu'),  # Additional hidden layer\n    tf.keras.layers.Dense(10, activation='softmax')  # Output layer\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(x_train, y_train, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:58:50.851727Z","iopub.execute_input":"2024-05-22T17:58:50.852495Z","iopub.status.idle":"2024-05-22T17:59:47.602128Z","shell.execute_reply.started":"2024-05-22T17:58:50.852448Z","shell.execute_reply":"2024-05-22T17:59:47.600563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the accuracy and loss\nplt.figure(figsize=(8, 5))\n\nplt.plot(history.history['accuracy'], label='Accuracy')\nplt.plot(history.history['loss'], label='Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\nplt.title('Model Accuracy and Loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:59:47.605418Z","iopub.execute_input":"2024-05-22T17:59:47.605766Z","iopub.status.idle":"2024-05-22T17:59:47.928204Z","shell.execute_reply.started":"2024-05-22T17:59:47.605737Z","shell.execute_reply":"2024-05-22T17:59:47.926767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\ntest = test_data.iloc[:,:].values\ntest = tf.keras.utils.normalize(test, axis=1)\n\npredictions = model.predict(test)\npredictions = np.argmax(predictions, axis=1)\n\nsubmission = pd.DataFrame({'ImageId': range(1, len(predictions) + 1), 'Label': predictions})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T18:00:06.560490Z","iopub.execute_input":"2024-05-22T18:00:06.560988Z","iopub.status.idle":"2024-05-22T18:00:11.888636Z","shell.execute_reply.started":"2024-05-22T18:00:06.560953Z","shell.execute_reply":"2024-05-22T18:00:11.887411Z"},"trusted":true},"execution_count":null,"outputs":[]}]}