{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31239,"isInternetEnabled":true,"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\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":"2025-12-22T17:04:20.259548Z","iopub.execute_input":"2025-12-22T17:04:20.259915Z","iopub.status.idle":"2025-12-22T17:04:20.267315Z","shell.execute_reply.started":"2025-12-22T17:04:20.259890Z","shell.execute_reply":"2025-12-22T17:04:20.266390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:04:20.269321Z","iopub.execute_input":"2025-12-22T17:04:20.269694Z","iopub.status.idle":"2025-12-22T17:04:22.233017Z","shell.execute_reply.started":"2025-12-22T17:04:20.269663Z","shell.execute_reply":"2025-12-22T17:04:22.232237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = data.iloc[:,1:]\ny = data.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:04:22.233879Z","iopub.execute_input":"2025-12-22T17:04:22.234109Z","iopub.status.idle":"2025-12-22T17:04:22.240003Z","shell.execute_reply.started":"2025-12-22T17:04:22.234091Z","shell.execute_reply":"2025-12-22T17:04:22.238924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\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-12-22T17:04:22.241058Z","iopub.execute_input":"2025-12-22T17:04:22.241827Z","iopub.status.idle":"2025-12-22T17:04:22.419340Z","shell.execute_reply.started":"2025-12-22T17:04:22.241796Z","shell.execute_reply":"2025-12-22T17:04:22.418336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.ensemble import VotingClassifier\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:04:22.421560Z","iopub.execute_input":"2025-12-22T17:04:22.421913Z","iopub.status.idle":"2025-12-22T17:04:22.426623Z","shell.execute_reply.started":"2025-12-22T17:04:22.421893Z","shell.execute_reply":"2025-12-22T17:04:22.425624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lr_modified = Pipeline(steps=[\n    ('scaler',StandardScaler()),\n    ('pca',PCA(n_components=0.95)),\n    ('lr',LogisticRegression(max_iter=1000))\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:04:22.427612Z","iopub.execute_input":"2025-12-22T17:04:22.427876Z","iopub.status.idle":"2025-12-22T17:04:22.442937Z","shell.execute_reply.started":"2025-12-22T17:04:22.427855Z","shell.execute_reply":"2025-12-22T17:04:22.441957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vc = VotingClassifier(estimators=[\n    ('lr-m',lr_modified),\n    ('knn',KNeighborsClassifier(n_neighbors=5,weights='distance')),\n    ('rf',RandomForestClassifier())\n],voting='soft')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:04:22.443841Z","iopub.execute_input":"2025-12-22T17:04:22.444116Z","iopub.status.idle":"2025-12-22T17:04:22.460858Z","shell.execute_reply.started":"2025-12-22T17:04:22.444090Z","shell.execute_reply":"2025-12-22T17:04:22.459623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vc.fit(X_train,y_train)\nvc.score(X_test,y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:04:22.462039Z","iopub.execute_input":"2025-12-22T17:04:22.462354Z","iopub.status.idle":"2025-12-22T17:06:04.415507Z","shell.execute_reply.started":"2025-12-22T17:04:22.462327Z","shell.execute_reply":"2025-12-22T17:06:04.414369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ny_pred = vc.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:16:42.029502Z","iopub.execute_input":"2025-12-22T17:16:42.029812Z","iopub.status.idle":"2025-12-22T17:17:33.611570Z","shell.execute_reply.started":"2025-12-22T17:16:42.029791Z","shell.execute_reply":"2025-12-22T17:17:33.610511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': range(1, len(y_pred) + 1),\n    'Label': y_pred\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:20:11.465407Z","iopub.execute_input":"2025-12-22T17:20:11.465743Z","iopub.status.idle":"2025-12-22T17:20:11.471417Z","shell.execute_reply.started":"2025-12-22T17:20:11.465720Z","shell.execute_reply":"2025-12-22T17:20:11.470531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T17:20:28.074031Z","iopub.execute_input":"2025-12-22T17:20:28.075009Z","iopub.status.idle":"2025-12-22T17:20:28.105067Z","shell.execute_reply.started":"2025-12-22T17:20:28.074977Z","shell.execute_reply":"2025-12-22T17:20:28.104135Z"}},"outputs":[],"execution_count":null}]}