{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30840,"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":"2025-01-24T14:07:03.709601Z","iopub.execute_input":"2025-01-24T14:07:03.710003Z","iopub.status.idle":"2025-01-24T14:07:03.716646Z","shell.execute_reply.started":"2025-01-24T14:07:03.709969Z","shell.execute_reply":"2025-01-24T14:07:03.71582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:03.717682Z","iopub.execute_input":"2025-01-24T14:07:03.717964Z","iopub.status.idle":"2025-01-24T14:07:03.734256Z","shell.execute_reply.started":"2025-01-24T14:07:03.717936Z","shell.execute_reply":"2025-01-24T14:07:03.733475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:03.735779Z","iopub.execute_input":"2025-01-24T14:07:03.736128Z","iopub.status.idle":"2025-01-24T14:07:05.188401Z","shell.execute_reply.started":"2025-01-24T14:07:03.736102Z","shell.execute_reply":"2025-01-24T14:07:05.187266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.189708Z","iopub.execute_input":"2025-01-24T14:07:05.190015Z","iopub.status.idle":"2025-01-24T14:07:05.194584Z","shell.execute_reply.started":"2025-01-24T14:07:05.189988Z","shell.execute_reply":"2025-01-24T14:07:05.193835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.195281Z","iopub.execute_input":"2025-01-24T14:07:05.195483Z","iopub.status.idle":"2025-01-24T14:07:05.215838Z","shell.execute_reply.started":"2025-01-24T14:07:05.195463Z","shell.execute_reply":"2025-01-24T14:07:05.215107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:, 1:]\ny = df.iloc[:, 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.216608Z","iopub.execute_input":"2025-01-24T14:07:05.216839Z","iopub.status.idle":"2025-01-24T14:07:05.227115Z","shell.execute_reply.started":"2025-01-24T14:07:05.21681Z","shell.execute_reply":"2025-01-24T14:07:05.226425Z"}},"outputs":[],"execution_count":null},{"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-01-24T14:07:05.228798Z","iopub.execute_input":"2025-01-24T14:07:05.229017Z","iopub.status.idle":"2025-01-24T14:07:05.450775Z","shell.execute_reply.started":"2025-01-24T14:07:05.228992Z","shell.execute_reply":"2025-01-24T14:07:05.449879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.452453Z","iopub.execute_input":"2025-01-24T14:07:05.452783Z","iopub.status.idle":"2025-01-24T14:07:05.457642Z","shell.execute_reply.started":"2025-01-24T14:07:05.452752Z","shell.execute_reply":"2025-01-24T14:07:05.45696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.458582Z","iopub.execute_input":"2025-01-24T14:07:05.458905Z","iopub.status.idle":"2025-01-24T14:07:05.469724Z","shell.execute_reply.started":"2025-01-24T14:07:05.458876Z","shell.execute_reply":"2025-01-24T14:07:05.469034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.470506Z","iopub.execute_input":"2025-01-24T14:07:05.4708Z","iopub.status.idle":"2025-01-24T14:07:05.485152Z","shell.execute_reply.started":"2025-01-24T14:07:05.47077Z","shell.execute_reply":"2025-01-24T14:07:05.484481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.486016Z","iopub.execute_input":"2025-01-24T14:07:05.486326Z","iopub.status.idle":"2025-01-24T14:07:05.607595Z","shell.execute_reply.started":"2025-01-24T14:07:05.486295Z","shell.execute_reply":"2025-01-24T14:07:05.606889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:05.608399Z","iopub.execute_input":"2025-01-24T14:07:05.608609Z","iopub.status.idle":"2025-01-24T14:07:14.794788Z","shell.execute_reply.started":"2025-01-24T14:07:05.60859Z","shell.execute_reply":"2025-01-24T14:07:14.793816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy = accuracy_score(y_test, y_pred)\nprint(accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:14.795763Z","iopub.execute_input":"2025-01-24T14:07:14.796042Z","iopub.status.idle":"2025-01-24T14:07:14.801919Z","shell.execute_reply.started":"2025-01-24T14:07:14.796006Z","shell.execute_reply":"2025-01-24T14:07:14.801091Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:14.802804Z","iopub.execute_input":"2025-01-24T14:07:14.803101Z","iopub.status.idle":"2025-01-24T14:07:14.861861Z","shell.execute_reply.started":"2025-01-24T14:07:14.803069Z","shell.execute_reply":"2025-01-24T14:07:14.860979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:14.862671Z","iopub.execute_input":"2025-01-24T14:07:14.862901Z","iopub.status.idle":"2025-01-24T14:07:15.3787Z","shell.execute_reply.started":"2025-01-24T14:07:14.862879Z","shell.execute_reply":"2025-01-24T14:07:15.3777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components = 100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:15.379646Z","iopub.execute_input":"2025-01-24T14:07:15.379952Z","iopub.status.idle":"2025-01-24T14:07:15.383736Z","shell.execute_reply.started":"2025-01-24T14:07:15.379916Z","shell.execute_reply":"2025-01-24T14:07:15.382942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.fit(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:15.384527Z","iopub.execute_input":"2025-01-24T14:07:15.384765Z","iopub.status.idle":"2025-01-24T14:07:17.116368Z","shell.execute_reply.started":"2025-01-24T14:07:15.384746Z","shell.execute_reply":"2025-01-24T14:07:17.115444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:17.117301Z","iopub.execute_input":"2025-01-24T14:07:17.117548Z","iopub.status.idle":"2025-01-24T14:07:17.124435Z","shell.execute_reply.started":"2025-01-24T14:07:17.117528Z","shell.execute_reply":"2025-01-24T14:07:17.12371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:17.12674Z","iopub.execute_input":"2025-01-24T14:07:17.126943Z","iopub.status.idle":"2025-01-24T14:07:17.137536Z","shell.execute_reply.started":"2025-01-24T14:07:17.126925Z","shell.execute_reply":"2025-01-24T14:07:17.136703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:17.138635Z","iopub.execute_input":"2025-01-24T14:07:17.138846Z","iopub.status.idle":"2025-01-24T14:07:17.16148Z","shell.execute_reply.started":"2025-01-24T14:07:17.138827Z","shell.execute_reply":"2025-01-24T14:07:17.160765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)\naccuracy = accuracy_score(y_test, y_pred)\nprint(accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T14:07:17.162127Z","iopub.execute_input":"2025-01-24T14:07:17.162334Z","iopub.status.idle":"2025-01-24T14:07:17.175679Z","shell.execute_reply.started":"2025-01-24T14:07:17.162316Z","shell.execute_reply":"2025-01-24T14:07:17.174576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}