{"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 numpy as np\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-22T17:46:38.088778Z","iopub.execute_input":"2024-05-22T17:46:38.089245Z","iopub.status.idle":"2024-05-22T17:46:38.094556Z","shell.execute_reply.started":"2024-05-22T17:46:38.089213Z","shell.execute_reply":"2024-05-22T17:46:38.093340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n# data","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:46:38.097304Z","iopub.execute_input":"2024-05-22T17:46:38.097674Z","iopub.status.idle":"2024-05-22T17:46:41.247194Z","shell.execute_reply.started":"2024-05-22T17:46:38.097606Z","shell.execute_reply":"2024-05-22T17:46:41.246171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Count the occurrences of each label\n# label_counts = data['label'].value_counts()\n\n# # Create a list of counts in the original order of labels\n# ordered_label_counts = [label_counts[label] for label in label_counts]\n\n# # Plot the label counts in the original order\n# plt.figure(figsize=(10, 6))\n# plt.bar(unique_labels, ordered_label_counts)\n# plt.xlabel('Label')\n# plt.ylabel('Count')\n# plt.title('Number of occurrences of each label')\n# plt.xticks(unique_labels)  # Ensure the x-ticks correspond to the labels\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:46:41.248662Z","iopub.execute_input":"2024-05-22T17:46:41.249069Z","iopub.status.idle":"2024-05-22T17:46:41.254806Z","shell.execute_reply.started":"2024-05-22T17:46:41.249033Z","shell.execute_reply":"2024-05-22T17:46:41.253563Z"},"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:46:41.256323Z","iopub.execute_input":"2024-05-22T17:46:41.256694Z","iopub.status.idle":"2024-05-22T17:46:41.948234Z","shell.execute_reply.started":"2024-05-22T17:46:41.256664Z","shell.execute_reply":"2024-05-22T17:46:41.947062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ### Select a single image (for example, the first one) to display\n# image_index = 4541 # Change this index to display a different image\n# image = x.iloc[image_index].values\n# label = y.iloc[image_index]\n\n# # Reshape the image to 28x28 pixels\n# image = image.reshape(28, 28)\n\n# # Display the image using matplotlib\n# plt.imshow(image, cmap='gray')\n# plt.title(f'Label: {label}')\n# plt.axis('off')  # Hide axis\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:46:41.951529Z","iopub.execute_input":"2024-05-22T17:46:41.952021Z","iopub.status.idle":"2024-05-22T17:46:41.957121Z","shell.execute_reply.started":"2024-05-22T17:46:41.951982Z","shell.execute_reply":"2024-05-22T17:46:41.955874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the model\nmodel = tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.Dense(64, activation='relu'))\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(x_train, y_train, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T17:46:41.958642Z","iopub.execute_input":"2024-05-22T17:46:41.959098Z","iopub.status.idle":"2024-05-22T17:46:55.127715Z","shell.execute_reply.started":"2024-05-22T17:46:41.959060Z","shell.execute_reply":"2024-05-22T17:46:55.126678Z"},"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:47:23.543223Z","iopub.execute_input":"2024-05-22T17:47:23.543651Z","iopub.status.idle":"2024-05-22T17:47:23.892704Z","shell.execute_reply.started":"2024-05-22T17:47:23.543594Z","shell.execute_reply":"2024-05-22T17:47:23.891541Z"},"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-22T17:46:55.634406Z","iopub.execute_input":"2024-05-22T17:46:55.634778Z","iopub.status.idle":"2024-05-22T17:46:59.514984Z","shell.execute_reply.started":"2024-05-22T17:46:55.634745Z","shell.execute_reply":"2024-05-22T17:46:59.513993Z"},"trusted":true},"execution_count":null,"outputs":[]}]}