{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"02108926-0f16-48f7-b276-16ca48fa49b6","cell_type":"code","source":"import pandas as pd \ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ny = train['label']\nX = train.drop(columns='label')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:04:46.973902Z","iopub.execute_input":"2025-04-13T18:04:46.974601Z","iopub.status.idle":"2025-04-13T18:04:53.535462Z","shell.execute_reply.started":"2025-04-13T18:04:46.974573Z","shell.execute_reply":"2025-04-13T18:04:53.534747Z"}},"outputs":[],"execution_count":null},{"id":"707a762f-ccab-4134-977f-eeba279aabda","cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(X.iloc[4].values.reshape(28,28), cmap='gray')\nplt.title(f\"Label: {y.iloc[4]}\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:04:53.536253Z","iopub.execute_input":"2025-04-13T18:04:53.536487Z","iopub.status.idle":"2025-04-13T18:04:53.790821Z","shell.execute_reply.started":"2025-04-13T18:04:53.536465Z","shell.execute_reply":"2025-04-13T18:04:53.789798Z"}},"outputs":[],"execution_count":null},{"id":"e2ff21ba-6251-4310-9593-523dede7da49","cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nX = scaler.fit_transform(X)\ntest = scaler.fit_transform(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:04:53.792434Z","iopub.execute_input":"2025-04-13T18:04:53.792685Z","iopub.status.idle":"2025-04-13T18:04:55.483064Z","shell.execute_reply.started":"2025-04-13T18:04:53.792665Z","shell.execute_reply":"2025-04-13T18:04:55.482294Z"}},"outputs":[],"execution_count":null},{"id":"762f1a10-fcfd-4686-87dd-7827e390814a","cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:04:55.483936Z","iopub.execute_input":"2025-04-13T18:04:55.484389Z","iopub.status.idle":"2025-04-13T18:04:55.998978Z","shell.execute_reply.started":"2025-04-13T18:04:55.484353Z","shell.execute_reply":"2025-04-13T18:04:55.997993Z"}},"outputs":[],"execution_count":null},{"id":"9b58aa07-5bf3-45fe-9cbc-15ad2b00cd2c","cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom xgboost import XGBClassifier\nclassificatio_models = {\n    'Logistic Regression': LogisticRegression(max_iter=1000),\n    'KNN': KNeighborsClassifier(),\n    'SVC': SVC(),\n    'Decision Tree': DecisionTreeClassifier(),\n    'Random Forest': RandomForestClassifier(),\n    'xgboost': XGBClassifier()\n} \nresult = {}\nfor name, model in classificatio_models.items():\n    model.fit(x_train, y_train)\n    y_pred = model.predict(x_test)\n    accuracy = accuracy_score(y_test, y_pred)\n    report = classification_report(y_test, y_pred)\n    confusion = confusion_matrix(y_test, y_pred)\n    result[name] = accuracy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:20:19.559420Z","iopub.execute_input":"2025-04-13T18:20:19.559814Z","iopub.status.idle":"2025-04-13T18:29:28.757197Z","shell.execute_reply.started":"2025-04-13T18:20:19.559790Z","shell.execute_reply":"2025-04-13T18:29:28.756309Z"}},"outputs":[],"execution_count":null},{"id":"53be038d-99b8-44d2-84d2-f562fd97dc9c","cell_type":"code","source":"df_result = pd.DataFrame([result], index=['Accuracy']).T.sort_values(by='Accuracy', ascending=False)\ndf_result","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:29:28.771984Z","iopub.execute_input":"2025-04-13T18:29:28.772296Z","iopub.status.idle":"2025-04-13T18:29:28.794426Z","shell.execute_reply.started":"2025-04-13T18:29:28.772264Z","shell.execute_reply":"2025-04-13T18:29:28.793554Z"}},"outputs":[],"execution_count":null},{"id":"4ee81d58-a656-402f-9c5e-3a0bfa74e288","cell_type":"code","source":"xgboost = XGBClassifier()\nxgboost.fit(x_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:29:28.796063Z","iopub.execute_input":"2025-04-13T18:29:28.796377Z","iopub.status.idle":"2025-04-13T18:31:50.749748Z","shell.execute_reply.started":"2025-04-13T18:29:28.796354Z","shell.execute_reply":"2025-04-13T18:31:50.748917Z"}},"outputs":[],"execution_count":null},{"id":"9bf4961f-ab77-40ed-8d3c-baea17165e03","cell_type":"code","source":"y_pred = xgboost.predict(test)\nsubmission = pd.DataFrame({\n    \"ImageId\": list(range(1, len(y_pred) + 1)),\n    \"Label\": y_pred\n})\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T18:33:05.636340Z","iopub.execute_input":"2025-04-13T18:33:05.637179Z","iopub.status.idle":"2025-04-13T18:33:06.271134Z","shell.execute_reply.started":"2025-04-13T18:33:05.637150Z","shell.execute_reply":"2025-04-13T18:33:06.269993Z"}},"outputs":[],"execution_count":null},{"id":"261408ed-c3c8-4cea-83a1-f3fac559d4f4","cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}