{"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":31260,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:51.974406Z","iopub.execute_input":"2026-02-26T22:10:51.974701Z","iopub.status.idle":"2026-02-26T22:10:51.982532Z","shell.execute_reply.started":"2026-02-26T22:10:51.974679Z","shell.execute_reply":"2026-02-26T22:10:51.981917Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Digit Recognizer – Exploratory Data Analysis (EDA)\n\n## Competition: Digit Recognizer\n\n**Goal:**  \nAnalyze handwritten digit images and understand pixel patterns that help classify digits from 0 to 9.\n\n**Problem type:** Classification  \n**Data type:** Image (28x28 grayscale pixels, flattened)  \n**Target variable:** label (digit 0–9)  \n**Evaluation metric:** Accuracy  \n\nAccuracy measures the proportion of correctly classified digits out of all predictions.\n\nThis notebook focuses on Exploratory Data Analysis (EDA), including data structure inspection, visualization of digit samples, pixel intensity distributions, and class balance analysis to prepare for modeling.\n","metadata":{}},{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport joblib\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.linear_model import LogisticRegression","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:11:27.199184Z","iopub.execute_input":"2026-02-26T22:11:27.199816Z","iopub.status.idle":"2026-02-26T22:11:27.204076Z","shell.execute_reply.started":"2026-02-26T22:11:27.199790Z","shell.execute_reply":"2026-02-26T22:11:27.203237Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Load Data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:51.996838Z","iopub.execute_input":"2026-02-26T22:10:51.997085Z","iopub.status.idle":"2026-02-26T22:10:53.765113Z","shell.execute_reply.started":"2026-02-26T22:10:51.997066Z","shell.execute_reply":"2026-02-26T22:10:53.764367Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Dataset Overview\nWe start by inspecting the size and structure of the datasets.","metadata":{}},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:53.766671Z","iopub.execute_input":"2026-02-26T22:10:53.766968Z","iopub.status.idle":"2026-02-26T22:10:53.771311Z","shell.execute_reply.started":"2026-02-26T22:10:53.766937Z","shell.execute_reply":"2026-02-26T22:10:53.770787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:53.772016Z","iopub.execute_input":"2026-02-26T22:10:53.772276Z","iopub.status.idle":"2026-02-26T22:10:53.887441Z","shell.execute_reply.started":"2026-02-26T22:10:53.772244Z","shell.execute_reply":"2026-02-26T22:10:53.886719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:53.888613Z","iopub.execute_input":"2026-02-26T22:10:53.889144Z","iopub.status.idle":"2026-02-26T22:10:55.342508Z","shell.execute_reply.started":"2026-02-26T22:10:53.889120Z","shell.execute_reply":"2026-02-26T22:10:55.341708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum().sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:55.343518Z","iopub.execute_input":"2026-02-26T22:10:55.343899Z","iopub.status.idle":"2026-02-26T22:10:55.373743Z","shell.execute_reply.started":"2026-02-26T22:10:55.343866Z","shell.execute_reply":"2026-02-26T22:10:55.373129Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Target View\nDistribution of numbers (0-9)","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=df[\"label\"])\nplt.title(\"Distribution of Digits\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:55.374678Z","iopub.execute_input":"2026-02-26T22:10:55.374949Z","iopub.status.idle":"2026-02-26T22:10:55.551911Z","shell.execute_reply.started":"2026-02-26T22:10:55.374928Z","shell.execute_reply":"2026-02-26T22:10:55.551301Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The target classes (digits 0–9) are fairly balanced, which is good for classification using accuracy.","metadata":{}},{"cell_type":"markdown","source":"## Visualize images","metadata":{}},{"cell_type":"code","source":"image = df.drop(\"label\", axis=1).iloc[0].values.reshape(28,28)\nplt.imshow(image, cmap=\"gray\")\nplt.title(f\"Label: {df['label'].iloc[0]}\")\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:55.552851Z","iopub.execute_input":"2026-02-26T22:10:55.553159Z","iopub.status.idle":"2026-02-26T22:10:55.685051Z","shell.execute_reply.started":"2026-02-26T22:10:55.553128Z","shell.execute_reply":"2026-02-26T22:10:55.684433Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axes = plt.subplots(1,5, figsize=(10,3))\n\nfor i, ax in enumerate(axes):\n    img = df.drop(\"label\", axis=1).iloc[i].values.reshape(28,28)\n    ax.imshow(img, cmap=\"gray\")\n    ax.set_title(df[\"label\"].iloc[i])\n    ax.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:55.686930Z","iopub.execute_input":"2026-02-26T22:10:55.687198Z","iopub.status.idle":"2026-02-26T22:10:56.215706Z","shell.execute_reply.started":"2026-02-26T22:10:55.687178Z","shell.execute_reply":"2026-02-26T22:10:56.215116Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Preprocessing","metadata":{}},{"cell_type":"code","source":"X = df.drop(\"label\", axis=1)\ny = df[\"label\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:56.216615Z","iopub.execute_input":"2026-02-26T22:10:56.216855Z","iopub.status.idle":"2026-02-26T22:10:56.298425Z","shell.execute_reply.started":"2026-02-26T22:10:56.216833Z","shell.execute_reply":"2026-02-26T22:10:56.297847Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Split","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:10:56.299304Z","iopub.execute_input":"2026-02-26T22:10:56.299602Z","iopub.status.idle":"2026-02-26T22:10:56.505107Z","shell.execute_reply.started":"2026-02-26T22:10:56.299568Z","shell.execute_reply":"2026-02-26T22:10:56.504548Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"model = LogisticRegression(max_iter=1000)\n\nmodel.fit(X_train, y_train)\n\npreds = model.predict(X_val)\n\nprint(\"Accuracy:\", accuracy_score(y_val, preds))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:11:30.823372Z","iopub.execute_input":"2026-02-26T22:11:30.824097Z","iopub.status.idle":"2026-02-26T22:14:08.931823Z","shell.execute_reply.started":"2026-02-26T22:11:30.824065Z","shell.execute_reply":"2026-02-26T22:14:08.931215Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Save Model","metadata":{}},{"cell_type":"code","source":"joblib.dump(model, \"digit_model.pkl\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T22:14:20.615489Z","iopub.execute_input":"2026-02-26T22:14:20.616029Z","iopub.status.idle":"2026-02-26T22:14:20.645222Z","shell.execute_reply.started":"2026-02-26T22:14:20.615985Z","shell.execute_reply":"2026-02-26T22:14:20.644686Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Conclusion\n\nA Logistic Regression was trained on the MNIST dataset to recognize handwritten digits.\n\nThe model achieved an accuracy of **0.90** on the validation set, showing strong performance without using deep learning.\n\nThe trained model was exported for deployment in a Streamlit web application that allows users to upload an image and receive a real-time prediction.","metadata":{}}]}