{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # Numerical Operation on Arrays\nimport pandas as pd # Read CSV file\n\n# Build and train Neural Networks\nimport tensorflow as tf \nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Preprocessing \nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-20T09:33:32.961243Z","iopub.execute_input":"2023-11-20T09:33:32.961860Z","iopub.status.idle":"2023-11-20T09:33:49.461962Z","shell.execute_reply.started":"2023-11-20T09:33:32.961815Z","shell.execute_reply":"2023-11-20T09:33:49.460472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read the data\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Pre-Process the data\nX_train = train_data.drop('label', axis=1)/255.0\ny_train = train_data['label']\nX_train = X_train.values.reshape(-1, 28,28,1)\n\n# Split a part of training data to create a validation set\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T09:34:18.679709Z","iopub.execute_input":"2023-11-20T09:34:18.680702Z","iopub.status.idle":"2023-11-20T09:34:25.822266Z","shell.execute_reply.started":"2023-11-20T09:34:18.680665Z","shell.execute_reply":"2023-11-20T09:34:25.820980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))\n\n# Evaluate the model\nval_loss, val_accuracy = model.evaluate(X_val, y_val)\nprint(f\"Validation accuracy: {val_accuracy}\")\n\n# Prediction\ntest_data = test_data / 255.0\ntest_data = test_data.values.reshape(-1, 28, 28, 1)\npredictions = model.predict(test_data)\n\nresults = np.argmax(predictions, axis=1)\nsubmission = pd.DataFrame({\"ImageId\": list(range(1, len(results)+1)), \"Label\": results})\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T09:34:46.310384Z","iopub.execute_input":"2023-11-20T09:34:46.310861Z"},"trusted":true},"execution_count":null,"outputs":[]}]}