{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823,"isSourceIdPinned":false}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"ff1ede16","cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:23.313652Z","iopub.execute_input":"2026-04-18T12:03:23.313907Z","iopub.status.idle":"2026-04-18T12:03:23.321266Z","shell.execute_reply.started":"2026-04-18T12:03:23.313884Z","shell.execute_reply":"2026-04-18T12:03:23.320426Z"}},"outputs":[],"execution_count":null},{"id":"9570d039","cell_type":"code","source":"import pandas as pd \nimport pandas as pd\n\ntrain = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/test.csv\")\n\ntrain.head()\n# train.head()\ntest.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:23.323263Z","iopub.execute_input":"2026-04-18T12:03:23.323877Z","iopub.status.idle":"2026-04-18T12:03:25.217550Z","shell.execute_reply.started":"2026-04-18T12:03:23.323830Z","shell.execute_reply":"2026-04-18T12:03:25.216766Z"}},"outputs":[],"execution_count":null},{"id":"013dc062","cell_type":"code","source":"import numpy as np \nimport matplotlib.pyplot as plt \nimport seaborn as sns \nprint(train.shape)\nprint(test.shape)\ntrain.info()\ntrain.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:25.218444Z","iopub.execute_input":"2026-04-18T12:03:25.218623Z","iopub.status.idle":"2026-04-18T12:03:27.247419Z","shell.execute_reply.started":"2026-04-18T12:03:25.218604Z","shell.execute_reply":"2026-04-18T12:03:27.246776Z"}},"outputs":[],"execution_count":null},{"id":"4a17575e","cell_type":"code","source":"plt.figure(figsize=(8,4))\nsns.countplot(x=train[\"label\"])\nplt.title(\"Digit transformation\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:27.248153Z","iopub.execute_input":"2026-04-18T12:03:27.248474Z","iopub.status.idle":"2026-04-18T12:03:27.456552Z","shell.execute_reply.started":"2026-04-18T12:03:27.248454Z","shell.execute_reply":"2026-04-18T12:03:27.455835Z"}},"outputs":[],"execution_count":null},{"id":"cb889848","cell_type":"code","source":"X = train.drop(\"label\", axis=1).values\ny = train[\"label\"].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:27.458260Z","iopub.execute_input":"2026-04-18T12:03:27.458452Z","iopub.status.idle":"2026-04-18T12:03:27.498318Z","shell.execute_reply.started":"2026-04-18T12:03:27.458434Z","shell.execute_reply":"2026-04-18T12:03:27.497605Z"}},"outputs":[],"execution_count":null},{"id":"fadadcd0","cell_type":"code","source":"def show_digit(idx):\n    img = X[idx].reshape(28, 28)\n    plt.imshow(img, cmap=\"gray\")\n    plt.title(f\"Label: {y[idx]}\")\n    plt.axis(\"off\")\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:27.499118Z","iopub.execute_input":"2026-04-18T12:03:27.499588Z","iopub.status.idle":"2026-04-18T12:03:27.504079Z","shell.execute_reply.started":"2026-04-18T12:03:27.499545Z","shell.execute_reply":"2026-04-18T12:03:27.503362Z"}},"outputs":[],"execution_count":null},{"id":"a386e4e9","cell_type":"code","source":"plt.figure(figsize=(10,6))\nfor i in range(10):\n    plt.subplot(2,5,i+1)\n    show_digit(i)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:27.504870Z","iopub.execute_input":"2026-04-18T12:03:27.505307Z","iopub.status.idle":"2026-04-18T12:03:27.742978Z","shell.execute_reply.started":"2026-04-18T12:03:27.505283Z","shell.execute_reply":"2026-04-18T12:03:27.742213Z"}},"outputs":[],"execution_count":null},{"id":"85298250","cell_type":"code","source":"X = X / 255.0\ntest = test.values / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:27.743838Z","iopub.execute_input":"2026-04-18T12:03:27.744313Z","iopub.status.idle":"2026-04-18T12:03:27.841995Z","shell.execute_reply.started":"2026-04-18T12:03:27.744291Z","shell.execute_reply":"2026-04-18T12:03:27.841348Z"}},"outputs":[],"execution_count":null},{"id":"ffe3863d","cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(\n    X, y, test_size=0.1, random_state=42\n)\nprint(X_train.shape, X_val.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:27.842933Z","iopub.execute_input":"2026-04-18T12:03:27.843114Z","iopub.status.idle":"2026-04-18T12:03:28.205332Z","shell.execute_reply.started":"2026-04-18T12:03:27.843095Z","shell.execute_reply":"2026-04-18T12:03:28.204799Z"}},"outputs":[],"execution_count":null},{"id":"f9a047f2","cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score\nmodel = LogisticRegression(max_iter=1000)\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:03:28.206035Z","iopub.execute_input":"2026-04-18T12:03:28.206423Z","iopub.status.idle":"2026-04-18T12:04:05.702990Z","shell.execute_reply.started":"2026-04-18T12:03:28.206403Z","shell.execute_reply":"2026-04-18T12:04:05.702068Z"}},"outputs":[],"execution_count":null},{"id":"92da588b","cell_type":"code","source":"pred_val = model.predict(X_val)\nprint(\"Validation score:\", accuracy_score(y_val, pred_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:04:05.704686Z","iopub.execute_input":"2026-04-18T12:04:05.705921Z","iopub.status.idle":"2026-04-18T12:04:05.716791Z","shell.execute_reply.started":"2026-04-18T12:04:05.705897Z","shell.execute_reply":"2026-04-18T12:04:05.716305Z"}},"outputs":[],"execution_count":null},{"id":"8bb115ce","cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrf = RandomForestClassifier(\n    n_estimators=200,\n    n_jobs=1,\n    random_state=42\n)\n\nrf.fit(X_train, y_train)\npred_val = rf.predict(X_val)\nprint(\"Validation accuracy: \", accuracy_score(y_val, pred_val))\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:04:05.717810Z","iopub.execute_input":"2026-04-18T12:04:05.718859Z","iopub.status.idle":"2026-04-18T12:04:49.175251Z","shell.execute_reply.started":"2026-04-18T12:04:05.718834Z","shell.execute_reply":"2026-04-18T12:04:49.174505Z"}},"outputs":[],"execution_count":null},{"id":"2d805db2","cell_type":"code","source":"rf.fit(X, y)\ntest_preds = rf.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:04:49.176088Z","iopub.execute_input":"2026-04-18T12:04:49.176432Z","iopub.status.idle":"2026-04-18T12:05:42.451394Z","shell.execute_reply.started":"2026-04-18T12:04:49.176410Z","shell.execute_reply":"2026-04-18T12:05:42.450413Z"}},"outputs":[],"execution_count":null},{"id":"d50705b8","cell_type":"code","source":"submission = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(test_preds)+1),\n    \"Label\": test_preds\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:05:42.452985Z","iopub.execute_input":"2026-04-18T12:05:42.453147Z","iopub.status.idle":"2026-04-18T12:05:42.457015Z","shell.execute_reply.started":"2026-04-18T12:05:42.453130Z","shell.execute_reply":"2026-04-18T12:05:42.456353Z"}},"outputs":[],"execution_count":null},{"id":"3baf38ae","cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-18T12:05:42.457645Z","iopub.execute_input":"2026-04-18T12:05:42.457810Z","iopub.status.idle":"2026-04-18T12:05:42.488851Z","shell.execute_reply.started":"2026-04-18T12:05:42.457788Z","shell.execute_reply":"2026-04-18T12:05:42.488250Z"}},"outputs":[],"execution_count":null},{"id":"32bbe618","cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}