{"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":"# 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-05-29T22:52:42.401269Z","iopub.execute_input":"2024-05-29T22:52:42.402138Z","iopub.status.idle":"2024-05-29T22:52:43.310300Z","shell.execute_reply.started":"2024-05-29T22:52:42.402095Z","shell.execute_reply":"2024-05-29T22:52:43.309285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Load the dataset\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n\ndef visualize_image(image_data):\n    \"\"\"Visualizes a single MNIST image with annotations.\"\"\"\n    image_matrix = image_data.reshape(28, 28)  # Reshape the image\n    \n    fig, ax = plt.subplots(figsize=(8, 8))  # Adjust size as needed\n    cax = ax.matshow(image_matrix, cmap='binary')  # Use binary colormap for black and white visual\n    \n    # Annotate each pixel with its corresponding value\n    for i in range(image_matrix.shape[0]):\n        for j in range(image_matrix.shape[1]):\n            text_color = 'white' if image_matrix[i, j] > 128 else 'black'\n            ax.text(j, i, str(image_matrix[i, j]), va='center', ha='center', color=text_color, fontsize=5)\n    \n    plt.title(\"MNIST Image with Pixel Values\")\n    plt.colorbar(cax)\n    plt.show()\n\n# How many images to display\nnum_images = 5\n\nfor index in range(num_images):\n    image_pixels = data.iloc[index, 1:].values  # Skip the label column\n    visualize_image(image_pixels)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-29T23:45:26.439741Z","iopub.execute_input":"2024-05-29T23:45:26.440654Z","iopub.status.idle":"2024-05-29T23:45:37.114434Z","shell.execute_reply.started":"2024-05-29T23:45:26.440619Z","shell.execute_reply":"2024-05-29T23:45:37.113337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\n\n# Load the dataset\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Specify how many images to display in total and per row\ntotal_images = 15\nimages_per_row = 5\n\n# Calculate the number of rows needed\nnum_rows = (total_images + images_per_row - 1) // images_per_row\n\n# Plotting the images in a matrix\nfig, axes = plt.subplots(num_rows, images_per_row, figsize=(10, num_rows * 2))  # Adjust height dynamically\naxes = axes.ravel()\n\nfor i in range(total_images):\n    img = test_df.iloc[i].values.reshape(28, 28)  # Reshape 784 pixels to 28x28 image\n    axes[i].imshow(img, cmap='gray')\n    axes[i].set_title(f\"Id: {i+1}\")  # Set title to image index\n    axes[i].axis('off')\n\n# If there are fewer images than placeholders in the matrix, turn off remaining axes\nfor j in range(i + 1, num_rows * images_per_row):\n    axes[j].axis('off')\n\nplt.tight_layout()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T01:04:23.797516Z","iopub.execute_input":"2024-05-30T01:04:23.797892Z","iopub.status.idle":"2024-05-30T01:04:26.497079Z","shell.execute_reply.started":"2024-05-30T01:04:23.797862Z","shell.execute_reply":"2024-05-30T01:04:26.495742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.metrics import accuracy_score, confusion_matrix\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T01:59:14.461791Z","iopub.execute_input":"2024-05-30T01:59:14.462246Z","iopub.status.idle":"2024-05-30T01:59:14.468176Z","shell.execute_reply.started":"2024-05-30T01:59:14.462208Z","shell.execute_reply":"2024-05-30T01:59:14.467302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the dataset\ntrain_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T01:59:25.061521Z","iopub.execute_input":"2024-05-30T01:59:25.061933Z","iopub.status.idle":"2024-05-30T01:59:29.983840Z","shell.execute_reply.started":"2024-05-30T01:59:25.061900Z","shell.execute_reply":"2024-05-30T01:59:29.982741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting a few images from the training dataset\nfig, axes = plt.subplots(2, 5, figsize=(10, 5))\naxes = axes.ravel()\nfor i in range(10):\n    img = train_df.iloc[i, 1:].values.reshape(28, 28)  # Reshape 784 pixels to 28x28 image\n    axes[i].imshow(img, cmap='gray')\n    axes[i].set_title(f\"Label: {train_df.iloc[i, 0]}\")\n    axes[i].axis('off')\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T01:59:41.067525Z","iopub.execute_input":"2024-05-30T01:59:41.068224Z","iopub.status.idle":"2024-05-30T01:59:41.941428Z","shell.execute_reply.started":"2024-05-30T01:59:41.068179Z","shell.execute_reply":"2024-05-30T01:59:41.940298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot distribution of labels\nsns.countplot(x='label', data=train_df)\nplt.title('Distribution of Digit Labels')\nplt.xlabel('Digit Label')\nplt.ylabel('Frequency')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T01:59:59.218421Z","iopub.execute_input":"2024-05-30T01:59:59.218834Z","iopub.status.idle":"2024-05-30T01:59:59.482500Z","shell.execute_reply.started":"2024-05-30T01:59:59.218801Z","shell.execute_reply":"2024-05-30T01:59:59.481148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize pixel values from 0-255 to 0-1 for both training and test data\nscaler = MinMaxScaler()\nX_train = scaler.fit_transform(train_df.drop('label', axis=1))\ny_train = train_df['label']\nX_test = scaler.transform(test_df)\n\n# Split data into training and validation sets\nX_train_part, X_val, y_train_part, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)\n\n# Initialize and train the Random Forest Classifier\nrf_classifier = RandomForestClassifier(random_state=42)\nrf_classifier.fit(X_train_part, y_train_part)\n\n# Validate the model\ny_pred_train = rf_classifier.predict(X_train_part)\ny_pred_val = rf_classifier.predict(X_val)\ntrain_accuracy = accuracy_score(y_train_part, y_pred_train)\nval_accuracy = accuracy_score(y_val, y_pred_val)\n\n# Output accuracy results\nprint(f\"Training Accuracy: {train_accuracy}\")\nprint(f\"Validation Accuracy: {val_accuracy}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T02:03:41.922243Z","iopub.execute_input":"2024-05-30T02:03:41.923544Z","iopub.status.idle":"2024-05-30T02:04:16.285075Z","shell.execute_reply.started":"2024-05-30T02:03:41.923500Z","shell.execute_reply":"2024-05-30T02:04:16.283992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compute and plot the confusion matrix\ncm = confusion_matrix(y_val, y_pred_val)\nplt.figure(figsize=(10, 7))\nsns.heatmap(cm, annot=True, fmt='d')\nplt.title('Confusion Matrix')\nplt.ylabel('True Label')\nplt.xlabel('Predicted Label')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T02:01:14.853673Z","iopub.execute_input":"2024-05-30T02:01:14.854068Z","iopub.status.idle":"2024-05-30T02:01:15.384337Z","shell.execute_reply.started":"2024-05-30T02:01:14.854037Z","shell.execute_reply":"2024-05-30T02:01:15.383276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate predictions for the test set\ntest_predictions = rf_classifier.predict(X_test)\n\n# Prepare the submission file\nsubmission_df = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(test_predictions) + 1),\n    \"Label\": test_predictions\n})\nsubmission_df.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T02:01:57.911832Z","iopub.execute_input":"2024-05-30T02:01:57.912382Z","iopub.status.idle":"2024-05-30T02:01:59.352417Z","shell.execute_reply.started":"2024-05-30T02:01:57.912334Z","shell.execute_reply":"2024-05-30T02:01:59.351151Z"},"trusted":true},"execution_count":null,"outputs":[]}]}