{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-07T19:52:36.963152Z","iopub.execute_input":"2024-10-07T19:52:36.963714Z","iopub.status.idle":"2024-10-07T19:52:38.307159Z","shell.execute_reply.started":"2024-10-07T19:52:36.963648Z","shell.execute_reply":"2024-10-07T19:52:38.305891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib as plt","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:52:40.640746Z","iopub.execute_input":"2024-10-07T19:52:40.641378Z","iopub.status.idle":"2024-10-07T19:52:57.470242Z","shell.execute_reply.started":"2024-10-07T19:52:40.641332Z","shell.execute_reply":"2024-10-07T19:52:57.468987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\") \ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:53:00.752436Z","iopub.execute_input":"2024-10-07T19:53:00.753208Z","iopub.status.idle":"2024-10-07T19:53:08.740551Z","shell.execute_reply.started":"2024-10-07T19:53:00.753159Z","shell.execute_reply":"2024-10-07T19:53:08.738902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:53:11.568226Z","iopub.execute_input":"2024-10-07T19:53:11.568681Z","iopub.status.idle":"2024-10-07T19:53:11.611277Z","shell.execute_reply.started":"2024-10-07T19:53:11.568639Z","shell.execute_reply":"2024-10-07T19:53:11.609999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"x_train = train.drop(columns = ['label'],axis = 0)\ny_train = train['label']","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:53:16.690584Z","iopub.execute_input":"2024-10-07T19:53:16.691104Z","iopub.status.idle":"2024-10-07T19:53:16.810732Z","shell.execute_reply.started":"2024-10-07T19:53:16.691056Z","shell.execute_reply":"2024-10-07T19:53:16.809453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:53:21.152412Z","iopub.execute_input":"2024-10-07T19:53:21.152973Z","iopub.status.idle":"2024-10-07T19:53:21.181583Z","shell.execute_reply.started":"2024-10-07T19:53:21.152896Z","shell.execute_reply":"2024-10-07T19:53:21.180194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\n\nmodel = Sequential([\n    Dense(784, activation='relu', input_shape=(784,)),\n    Dense(128, activation='relu'),\n    Dense(64, activation='relu'),\n    Dense(32, activation='relu'),\n    Dense(16, activation='relu'),\n    Dense(10, activation='softmax'),  # Output layer for multi-class classification (10 classes)\n])\n\n# Compile the model with categorical crossentropy for multi-class classification\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:53:25.918166Z","iopub.execute_input":"2024-10-07T19:53:25.918613Z","iopub.status.idle":"2024-10-07T19:53:26.139148Z","shell.execute_reply.started":"2024-10-07T19:53:25.918563Z","shell.execute_reply":"2024-10-07T19:53:26.137784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:53:29.386302Z","iopub.execute_input":"2024-10-07T19:53:29.386770Z","iopub.status.idle":"2024-10-07T19:53:29.418001Z","shell.execute_reply.started":"2024-10-07T19:53:29.386727Z","shell.execute_reply":"2024-10-07T19:53:29.416657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n# Convert the labels to one-hot encoded form\ny_train = to_categorical(y_train, num_classes=10)  \nmodel.fit(x_train, y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:53:31.872299Z","iopub.execute_input":"2024-10-07T19:53:31.872752Z","iopub.status.idle":"2024-10-07T19:55:49.339416Z","shell.execute_reply.started":"2024-10-07T19:53:31.872711Z","shell.execute_reply":"2024-10-07T19:55:49.338145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Assuming your DataFrame is named 'df'\n# Convert the DataFrame to NumPy array\ndata = test.values\n\n# Reshape each row (1x784) to (28x28)\nimages = data.reshape(-1, 28, 28)  # -1 ensures all rows are reshaped\n\n# Display the first image\nplt.imshow(images[0], cmap='gray')\nplt.title('Image 0')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:56:43.001707Z","iopub.execute_input":"2024-10-07T19:56:43.003159Z","iopub.status.idle":"2024-10-07T19:56:43.361096Z","shell.execute_reply.started":"2024-10-07T19:56:43.003103Z","shell.execute_reply":"2024-10-07T19:56:43.359788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:57:01.641884Z","iopub.execute_input":"2024-10-07T19:57:01.645217Z","iopub.status.idle":"2024-10-07T19:57:01.654140Z","shell.execute_reply.started":"2024-10-07T19:57:01.645140Z","shell.execute_reply":"2024-10-07T19:57:01.652887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test)  # Predicts probability distributions for each class\n\n# Get the predicted class (the one with the highest probability)\npredicted_classes = np.argmax(predictions, axis=1)\n\n# Print the predicted classes\nprint(predicted_classes[:10])","metadata":{"execution":{"iopub.status.busy":"2024-10-07T19:58:20.270566Z","iopub.execute_input":"2024-10-07T19:58:20.271100Z","iopub.status.idle":"2024-10-07T19:58:23.945385Z","shell.execute_reply.started":"2024-10-07T19:58:20.271040Z","shell.execute_reply":"2024-10-07T19:58:23.943831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Set the number of rows and columns for the grid\nrows, cols = 2, 5  # 2 rows, 5 columns\n\n# Create subplots\nfig, axes = plt.subplots(rows, cols, figsize=(10, 5))\n\n# Loop through the first 10 images\nfor i in range(10):\n    # Find the correct subplot location\n    ax = axes[i // cols, i % cols]  # Determine row and column\n    \n    # Display the image\n    ax.imshow(images[i], cmap='gray')\n    \n    # Set the title for each subplot\n    ax.set_title(f'predict {predicted_classes[i]}')\n    \n    # Hide the axis (optional)\n    ax.axis('off')\n\n# Adjust layout so the titles don't overlap\nplt.tight_layout()\n\n# Show the grid of images\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T20:00:06.753172Z","iopub.execute_input":"2024-10-07T20:00:06.753795Z","iopub.status.idle":"2024-10-07T20:00:08.135652Z","shell.execute_reply.started":"2024-10-07T20:00:06.753745Z","shell.execute_reply":"2024-10-07T20:00:08.134033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': np.arange(1, len(predicted_classes) + 1),  # Create ImageId column starting from 1\n    'label': predicted_classes  # Your predicted classes\n})\n\n# Save the DataFrame to a CSV file\nsubmission.to_csv(\"/kaggle/working/submission2.csv\", index=False)  # index=False to avoid saving the index\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T20:08:21.401341Z","iopub.execute_input":"2024-10-07T20:08:21.401787Z","iopub.status.idle":"2024-10-07T20:08:21.448641Z","shell.execute_reply.started":"2024-10-07T20:08:21.401747Z","shell.execute_reply":"2024-10-07T20:08:21.447249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-10-07T20:07:48.254038Z","iopub.execute_input":"2024-10-07T20:07:48.254595Z","iopub.status.idle":"2024-10-07T20:07:48.269193Z","shell.execute_reply.started":"2024-10-07T20:07:48.254545Z","shell.execute_reply":"2024-10-07T20:07:48.267488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}