{ "cells": [ { "cell_type": "markdown", "id": "df15487e", "metadata": { "papermill": { "duration": 0.003655, "end_time": "2026-05-12T18:12:35.316317+00:00", "exception": false, "start_time": "2026-05-12T18:12:35.312662+00:00", "status": "completed" }, "tags": [] }, "source": [ "# Digit Recognizer" ] }, { "cell_type": "markdown", "id": "fe874a2f", "metadata": { "papermill": { "duration": 0.002338, "end_time": "2026-05-12T18:12:35.321466+00:00", "exception": false, "start_time": "2026-05-12T18:12:35.319128+00:00", "status": "completed" }, "tags": [] }, "source": [ "" ] }, { "cell_type": "markdown", "id": "7ced5fba", "metadata": { "papermill": { "duration": 0.002482, "end_time": "2026-05-12T18:12:35.326178+00:00", "exception": false, "start_time": "2026-05-12T18:12:35.323696+00:00", "status": "completed" }, "tags": [] }, "source": [ "## Dataset Description\n", "\n", "The data files train.csv and test.csv contain gray-scale images of hand-drawn digits, from zero through nine.\n", "\n", "Each image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255, inclusive.\n", "\n", "The training data set, (train.csv), has 785 columns. The first column, called \"label\", is the digit that was drawn by the user. The rest of the columns contain the pixel-values of the associated image.\n", "\n", "The test data set, (test.csv), is the same as the training set, except that it does not contain the \"label\" column.\n" ] }, { "cell_type": "markdown", "id": "ec4b165d", "metadata": { "papermill": { "duration": 0.002622, "end_time": "2026-05-12T18:12:35.331393+00:00", "exception": false, "start_time": "2026-05-12T18:12:35.328771+00:00", "status": "completed" }, "tags": [] }, "source": [ "## Aim & Target \n", "\n", "**Aim**\n", "\n", "The primary aim of this project is to develop a robust machine learning model capable of accurately identifying and classifying hand-drawn digits from the MNIST dataset. By leveraging a Convolutional Neural Network (CNN), the goal is to map high-dimensional pixel data into ten distinct numerical categories. \n", "\n", "**Target**\n", "\n", "The target is to achieve a categorization accuracy exceeding 98% on the 28,000 test images while maintaining a computationally efficient pipeline. Successfully meeting this benchmark will result in a submission file that correctly maps each ImageId to its predicted Label. This project serves as a foundational milestone in mastering image recognition and deep learning deployment workflows." ] }, { "cell_type": "markdown", "id": "394fd234", "metadata": { "papermill": { "duration": 0.003652, "end_time": "2026-05-12T18:12:35.339320+00:00", "exception": false, "start_time": "2026-05-12T18:12:35.335668+00:00", "status": "completed" }, "tags": [] }, "source": [ "## Importing Libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "9f5737cf", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:35.349925Z", "iopub.status.busy": "2026-05-12T18:12:35.349460Z", "iopub.status.idle": "2026-05-12T18:12:38.936432Z", "shell.execute_reply": "2026-05-12T18:12:38.935526Z" }, "papermill": { "duration": 3.595043, "end_time": "2026-05-12T18:12:38.938258+00:00", "exception": false, "start_time": "2026-05-12T18:12:35.343215+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "from sklearn.model_selection import train_test_split\n" ] }, { "cell_type": "markdown", "id": "eab327cf", "metadata": { "papermill": { "duration": 0.002335, "end_time": "2026-05-12T18:12:38.943048+00:00", "exception": false, "start_time": "2026-05-12T18:12:38.940713+00:00", "status": "completed" }, "tags": [] }, "source": [ "## Dataframe Generation" ] }, { "cell_type": "code", "execution_count": 2, "id": "f28ea190", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:38.949488Z", "iopub.status.busy": "2026-05-12T18:12:38.948546Z", "iopub.status.idle": "2026-05-12T18:12:42.619012Z", "shell.execute_reply": "2026-05-12T18:12:42.618094Z" }, "papermill": { "duration": 3.675462, "end_time": "2026-05-12T18:12:42.620795+00:00", "exception": false, "start_time": "2026-05-12T18:12:38.945333+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "train = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\") \n", "\n", "test = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/test.csv\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "da8e3239", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:42.627192Z", "iopub.status.busy": "2026-05-12T18:12:42.626825Z", "iopub.status.idle": "2026-05-12T18:12:42.659630Z", "shell.execute_reply": "2026-05-12T18:12:42.658951Z" }, "papermill": { "duration": 0.03771, "end_time": "2026-05-12T18:12:42.661103+00:00", "exception": false, "start_time": "2026-05-12T18:12:42.623393+00:00", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
labelpixel0pixel1pixel2pixel3pixel4pixel5pixel6pixel7pixel8...pixel774pixel775pixel776pixel777pixel778pixel779pixel780pixel781pixel782pixel783
01000000000...0000000000
10000000000...0000000000
21000000000...0000000000
34000000000...0000000000
40000000000...0000000000
\n", "

5 rows × 785 columns

\n", "
" ], "text/plain": [ " label pixel0 pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 \\\n", "0 1 0 0 0 0 0 0 0 0 \n", "1 0 0 0 0 0 0 0 0 0 \n", "2 1 0 0 0 0 0 0 0 0 \n", "3 4 0 0 0 0 0 0 0 0 \n", "4 0 0 0 0 0 0 0 0 0 \n", "\n", " pixel8 ... pixel774 pixel775 pixel776 pixel777 pixel778 pixel779 \\\n", "0 0 ... 0 0 0 0 0 0 \n", "1 0 ... 0 0 0 0 0 0 \n", "2 0 ... 0 0 0 0 0 0 \n", "3 0 ... 0 0 0 0 0 0 \n", "4 0 ... 0 0 0 0 0 0 \n", "\n", " pixel780 pixel781 pixel782 pixel783 \n", "0 0 0 0 0 \n", "1 0 0 0 0 \n", "2 0 0 0 0 \n", "3 0 0 0 0 \n", "4 0 0 0 0 \n", "\n", "[5 rows x 785 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "7fc853c8", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:42.668007Z", "iopub.status.busy": "2026-05-12T18:12:42.667674Z", "iopub.status.idle": "2026-05-12T18:12:42.671984Z", "shell.execute_reply": "2026-05-12T18:12:42.671380Z" }, "papermill": { "duration": 0.009409, "end_time": "2026-05-12T18:12:42.673530+00:00", "exception": false, "start_time": "2026-05-12T18:12:42.664121+00:00", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(42000, 785)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train.shape" ] }, { "cell_type": "code", "execution_count": 5, "id": "97fe8462", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:42.679934Z", "iopub.status.busy": "2026-05-12T18:12:42.679623Z", "iopub.status.idle": "2026-05-12T18:12:42.683765Z", "shell.execute_reply": "2026-05-12T18:12:42.683129Z" }, "papermill": { "duration": 0.009328, "end_time": "2026-05-12T18:12:42.685472+00:00", "exception": false, "start_time": "2026-05-12T18:12:42.676144+00:00", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(28000, 784)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test.shape" ] }, { "cell_type": "code", "execution_count": 6, "id": "aa1558d3", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:42.692679Z", "iopub.status.busy": "2026-05-12T18:12:42.692055Z", "iopub.status.idle": "2026-05-12T18:12:42.777113Z", "shell.execute_reply": "2026-05-12T18:12:42.776531Z" }, "papermill": { "duration": 0.090785, "end_time": "2026-05-12T18:12:42.778985+00:00", "exception": false, "start_time": "2026-05-12T18:12:42.688200+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "X = train.drop(\"label\", axis=1)\n", "y = train[\"label\"]" ] }, { "cell_type": "code", "execution_count": 7, "id": "f464ac02", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:42.786245Z", "iopub.status.busy": "2026-05-12T18:12:42.785890Z", "iopub.status.idle": "2026-05-12T18:12:43.003954Z", "shell.execute_reply": "2026-05-12T18:12:43.003253Z" }, "papermill": { "duration": 0.223632, "end_time": "2026-05-12T18:12:43.005670+00:00", "exception": false, "start_time": "2026-05-12T18:12:42.782038+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_val, y_train, y_val = train_test_split(\n", " X,y,test_size = 0.2, random_state=42\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "id": "0d524046", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:43.013753Z", "iopub.status.busy": "2026-05-12T18:12:43.013400Z", "iopub.status.idle": "2026-05-12T18:12:43.537378Z", "shell.execute_reply": "2026-05-12T18:12:43.536753Z" }, "papermill": { "duration": 0.530267, "end_time": "2026-05-12T18:12:43.539063+00:00", "exception": false, "start_time": "2026-05-12T18:12:43.008796+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "from sklearn.ensemble import RandomForestClassifier" ] }, { "cell_type": "code", "execution_count": 9, "id": "a0c58228", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:43.547178Z", "iopub.status.busy": "2026-05-12T18:12:43.546719Z", "iopub.status.idle": "2026-05-12T18:12:43.550180Z", "shell.execute_reply": "2026-05-12T18:12:43.549608Z" }, "papermill": { "duration": 0.009009, "end_time": "2026-05-12T18:12:43.551425+00:00", "exception": false, "start_time": "2026-05-12T18:12:43.542416+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "model = RandomForestClassifier()" ] }, { "cell_type": "code", "execution_count": 10, "id": "19f928ea", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:12:43.558489Z", "iopub.status.busy": "2026-05-12T18:12:43.558154Z", "iopub.status.idle": "2026-05-12T18:13:07.413519Z", "shell.execute_reply": "2026-05-12T18:13:07.412701Z" }, "papermill": { "duration": 23.860858, "end_time": "2026-05-12T18:13:07.415128+00:00", "exception": false, "start_time": "2026-05-12T18:12:43.554270+00:00", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
RandomForestClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "RandomForestClassifier()" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 11, "id": "a0e7b784", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:13:07.423313Z", "iopub.status.busy": "2026-05-12T18:13:07.422932Z", "iopub.status.idle": "2026-05-12T18:13:07.640592Z", "shell.execute_reply": "2026-05-12T18:13:07.639619Z" }, "papermill": { "duration": 0.223409, "end_time": "2026-05-12T18:13:07.642164+00:00", "exception": false, "start_time": "2026-05-12T18:13:07.418755+00:00", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 96.42 %\n" ] } ], "source": [ "accuracy = model.score(X_val, y_val)\n", "print(\"Accuracy:\", np.round(accuracy*100,2),\"%\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "48cc6d59", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:13:07.650894Z", "iopub.status.busy": "2026-05-12T18:13:07.650498Z", "iopub.status.idle": "2026-05-12T18:13:08.379387Z", "shell.execute_reply": "2026-05-12T18:13:08.378783Z" }, "papermill": { "duration": 0.735532, "end_time": "2026-05-12T18:13:08.381324+00:00", "exception": false, "start_time": "2026-05-12T18:13:07.645792+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "predictions = model.predict(test)" ] }, { "cell_type": "code", "execution_count": 13, "id": "b57a7e97", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:13:08.389773Z", "iopub.status.busy": "2026-05-12T18:13:08.389539Z", "iopub.status.idle": "2026-05-12T18:13:08.393628Z", "shell.execute_reply": "2026-05-12T18:13:08.392934Z" }, "papermill": { "duration": 0.009935, "end_time": "2026-05-12T18:13:08.394996+00:00", "exception": false, "start_time": "2026-05-12T18:13:08.385061+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Create submission file\n", "submission = pd.DataFrame(\n", " {\n", " \"ImageId\": range(1,len(predictions)+1),\n", " \"Label\": predictions\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": 14, "id": "cb3fddc6", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:13:08.402838Z", "iopub.status.busy": "2026-05-12T18:13:08.402631Z", "iopub.status.idle": "2026-05-12T18:13:08.428700Z", "shell.execute_reply": "2026-05-12T18:13:08.428131Z" }, "papermill": { "duration": 0.032122, "end_time": "2026-05-12T18:13:08.430429+00:00", "exception": false, "start_time": "2026-05-12T18:13:08.398307+00:00", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Save as CSV file\n", "submission.to_csv(\"submission.csv\", index=False)" ] }, { "cell_type": "code", "execution_count": 15, "id": "042b6c9d", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:13:08.439042Z", "iopub.status.busy": "2026-05-12T18:13:08.438820Z", "iopub.status.idle": "2026-05-12T18:13:08.536023Z", "shell.execute_reply": "2026-05-12T18:13:08.535310Z" }, "papermill": { "duration": 0.103325, "end_time": "2026-05-12T18:13:08.537640+00:00", "exception": false, "start_time": "2026-05-12T18:13:08.434315+00:00", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['digit_recognizer_rf.joblib']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import joblib\n", "\n", "# This creates the only file you need for the model itself\n", "joblib.dump(model, 'digit_recognizer_rf.joblib')" ] }, { "cell_type": "code", "execution_count": 16, "id": "e1882c65", "metadata": { "execution": { "iopub.execute_input": "2026-05-12T18:13:08.546298Z", "iopub.status.busy": "2026-05-12T18:13:08.546022Z", "iopub.status.idle": "2026-05-12T18:13:08.551992Z", "shell.execute_reply": "2026-05-12T18:13:08.551468Z" }, "papermill": { "duration": 0.011764, "end_time": "2026-05-12T18:13:08.553416+00:00", "exception": false, "start_time": "2026-05-12T18:13:08.541652+00:00", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "digit_recognizer_rf.joblib
" ], "text/plain": [ "/kaggle/working/digit_recognizer_rf.joblib" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "submission.csv
" ], "text/plain": [ "/kaggle/working/submission.csv" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import FileLink\n", "\n", "display(FileLink('digit_recognizer_rf.joblib'))\n", "display(FileLink('submission.csv'))" ] }, { "cell_type": "markdown", "id": "b02a1ef8", "metadata": { "papermill": { "duration": 0.003586, "end_time": "2026-05-12T18:13:08.560971+00:00", "exception": false, "start_time": "2026-05-12T18:13:08.557385+00:00", "status": "completed" }, "tags": [] }, "source": [ "## Conclusion \n", "\n", "In conclusion, this project successfully demonstrates the application of a Random Forest Classifier to the classic problem of handwritten digit recognition. By processing the 784-pixel grayscale images and optimizing the ensemble model, we established a reliable pipeline for mapping visual data to numerical labels with high accuracy. The resulting model was successfully serialized into a .joblib format, ensuring it is ready for deployment to professional portfolios like Hugging Face. This achievement marks a significant step forward in mastering tabular image data and fulfilling the requirements for professional status in the field." ] } ], "metadata": { "kaggle": { "accelerator": "gpu", "dataSources": [ { "databundleVersionId": 861823, "sourceId": 3004, "sourceType": "competition" } ], "dockerImageVersionId": 31329, "isGpuEnabled": true, "isInternetEnabled": true, "language": "python", "sourceType": "notebook" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.12" }, "papermill": { "default_parameters": {}, "duration": 36.968193, "end_time": "2026-05-12T18:13:09.082285+00:00", "environment_variables": {}, "exception": null, "input_path": "__notebook__.ipynb", "output_path": "__notebook__.ipynb", "parameters": {}, "start_time": "2026-05-12T18:12:32.114092+00:00", "version": "2.7.0" } }, "nbformat": 4, "nbformat_minor": 5 }