{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31329,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Digit Recognizer","metadata":{}},{"cell_type":"markdown","source":"<img src='https://www.kaggle.com/competitions/3004/images/header'>","metadata":{}},{"cell_type":"markdown","source":"## Dataset Description\n\nThe data files train.csv and test.csv contain gray-scale images of hand-drawn digits, from zero through nine.\n\nEach 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\nThe 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\nThe test data set, (test.csv), is the same as the training set, except that it does not contain the \"label\" column.\n","metadata":{}},{"cell_type":"markdown","source":"## Aim & Target \n\n**Aim**\n\nThe 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\nThe 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.","metadata":{}},{"cell_type":"markdown","source":"## Importing Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nfrom sklearn.model_selection import train_test_split\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Dataframe Generation","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\") \n\ntest = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/test.csv\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(\"label\", axis=1)\ny = train[\"label\"]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(\n    X,y,test_size = 0.2, random_state=42\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = RandomForestClassifier()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = model.score(X_val, y_val)\nprint(\"Accuracy:\", np.round(accuracy*100,2),\"%\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(test)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create submission file\nsubmission = pd.DataFrame(\n    {\n        \"ImageId\": range(1,len(predictions)+1),\n        \"Label\": predictions\n    }\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save as CSV file\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import joblib\n\n# This creates the only file you need for the model itself\njoblib.dump(model, 'digit_recognizer_rf.joblib')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import FileLink\n\ndisplay(FileLink('digit_recognizer_rf.joblib'))\ndisplay(FileLink('submission.csv'))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Conclusion \n\nIn 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":{}}]}