{"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":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31286,"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\nfrom sklearn.model_selection import train_test_split\nfrom xgboost import XGBClassifier\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\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-23T10:17:24.225023Z","iopub.execute_input":"2026-02-23T10:17:24.225350Z","iopub.status.idle":"2026-02-23T10:17:24.233420Z","shell.execute_reply.started":"2026-02-23T10:17:24.225326Z","shell.execute_reply":"2026-02-23T10:17:24.231936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Read data\ntrain_eval_data = pd.read_csv(os.path.join(dirname, \"train.csv\")).to_numpy()\n\ntrain_eval_labels = train_eval_data[:,0]\ntrain_eval_data = train_eval_data[:,1:]\n\ntrain_eval_labels.shape,train_eval_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T10:17:24.235096Z","iopub.execute_input":"2026-02-23T10:17:24.235380Z","iopub.status.idle":"2026-02-23T10:17:26.051765Z","shell.execute_reply.started":"2026-02-23T10:17:24.235355Z","shell.execute_reply":"2026-02-23T10:17:26.050832Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"four_random_indexes = np.random.choice(len(train_eval_labels), size=(4,))\n\nfig, axs = plt.subplots(nrows=2, ncols=2)\nfour_imageplot_matrix = axs.flatten()\n   \n    \nfor idx, ax in enumerate(four_imageplot_matrix):\n    number_image_data = train_eval_data[four_random_indexes[idx]]\n    number_image_label = train_eval_labels[four_random_indexes[idx]]\n    \n    ax.imshow(number_image_data.reshape(28,28))\n    ax.set_title(f\"Number {number_image_label}\")\n    ax.axis(\"off\")\n        \nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T10:17:26.053859Z","iopub.execute_input":"2026-02-23T10:17:26.054228Z","iopub.status.idle":"2026-02-23T10:17:26.249444Z","shell.execute_reply.started":"2026-02-23T10:17:26.054199Z","shell.execute_reply":"2026-02-23T10:17:26.248527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data,eval_data,train_labels,eval_labels = train_test_split(\n    train_eval_data, train_eval_labels, test_size=0.25)\ntrain_data.shape, eval_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T10:17:26.250368Z","iopub.execute_input":"2026-02-23T10:17:26.250636Z","iopub.status.idle":"2026-02-23T10:17:26.560831Z","shell.execute_reply.started":"2026-02-23T10:17:26.250608Z","shell.execute_reply":"2026-02-23T10:17:26.559678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"number_detector =  XGBClassifier(n_estimators=100, max_depth=100, learning_rate=1)\nnumber_detector.fit(train_data, train_labels)\npred_labels = number_detector.predict(eval_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T10:17:26.562560Z","iopub.execute_input":"2026-02-23T10:17:26.562903Z","iopub.status.idle":"2026-02-23T10:17:38.997415Z","shell.execute_reply.started":"2026-02-23T10:17:26.562875Z","shell.execute_reply":"2026-02-23T10:17:38.995897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = np.array(pred_labels == eval_labels).astype(int) \naccuracy = np.sum(accuracy)\naccuracy = accuracy / len(eval_labels) * 100\naccuracy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T10:17:38.998159Z","iopub.status.idle":"2026-02-23T10:17:38.999110Z","shell.execute_reply.started":"2026-02-23T10:17:38.998840Z","shell.execute_reply":"2026-02-23T10:17:38.998899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv(os.path.join(dirname, \"test.csv\")).to_numpy()\ntest_labels = number_detector.predict(test_data)\npd.read_csv(os.path.join(dirname, \"sample_submission.csv\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T10:17:39.002044Z","iopub.status.idle":"2026-02-23T10:17:39.002537Z","shell.execute_reply.started":"2026-02-23T10:17:39.002381Z","shell.execute_reply":"2026-02-23T10:17:39.002402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_id=np.array([i for i in range(len(test_labels))])\nfinal_df = pd.DataFrame({ 'ImageId': image_id + 1, 'Label': test_labels})\nfinal_df.to_csv('submission.csv',index=False,columns=['ImageId', 'Label'])\nfinal_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T10:17:39.004368Z","iopub.status.idle":"2026-02-23T10:17:39.004804Z","shell.execute_reply.started":"2026-02-23T10:17:39.004623Z","shell.execute_reply":"2026-02-23T10:17:39.004638Z"}},"outputs":[],"execution_count":null}]}