{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":31040,"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-06-11T01:58:31.231392Z","iopub.execute_input":"2025-06-11T01:58:31.232151Z","iopub.status.idle":"2025-06-11T01:58:31.239633Z","shell.execute_reply.started":"2025-06-11T01:58:31.232125Z","shell.execute_reply":"2025-06-11T01:58:31.238527Z"}},"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-06-11T01:58:31.240526Z","iopub.execute_input":"2025-06-11T01:58:31.240827Z","iopub.status.idle":"2025-06-11T01:58:33.805123Z","shell.execute_reply.started":"2025-06-11T01:58:31.240806Z","shell.execute_reply":"2025-06-11T01:58:33.804294Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:33.806135Z","iopub.execute_input":"2025-06-11T01:58:33.806473Z","iopub.status.idle":"2025-06-11T01:58:33.827350Z","shell.execute_reply.started":"2025-06-11T01:58:33.806444Z","shell.execute_reply":"2025-06-11T01:58:33.825957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:33.828274Z","iopub.execute_input":"2025-06-11T01:58:33.828563Z","iopub.status.idle":"2025-06-11T01:58:33.843206Z","shell.execute_reply.started":"2025-06-11T01:58:33.828530Z","shell.execute_reply":"2025-06-11T01:58:33.842174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[41998,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:33.844022Z","iopub.execute_input":"2025-06-11T01:58:33.844293Z","iopub.status.idle":"2025-06-11T01:58:34.039108Z","shell.execute_reply.started":"2025-06-11T01:58:33.844273Z","shell.execute_reply":"2025-06-11T01:58:34.038340Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:34.040205Z","iopub.execute_input":"2025-06-11T01:58:34.040675Z","iopub.status.idle":"2025-06-11T01:58:34.045551Z","shell.execute_reply.started":"2025-06-11T01:58:34.040643Z","shell.execute_reply":"2025-06-11T01:58:34.044484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=df.iloc[:,1:].values\ny=df.iloc[:,0].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:34.046429Z","iopub.execute_input":"2025-06-11T01:58:34.046761Z","iopub.status.idle":"2025-06-11T01:58:34.066669Z","shell.execute_reply.started":"2025-06-11T01:58:34.046731Z","shell.execute_reply":"2025-06-11T01:58:34.065505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=41,test_size=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:34.067776Z","iopub.execute_input":"2025-06-11T01:58:34.068128Z","iopub.status.idle":"2025-06-11T01:58:34.541844Z","shell.execute_reply.started":"2025-06-11T01:58:34.068097Z","shell.execute_reply":"2025-06-11T01:58:34.540845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:34.543009Z","iopub.execute_input":"2025-06-11T01:58:34.543245Z","iopub.status.idle":"2025-06-11T01:58:34.547904Z","shell.execute_reply.started":"2025-06-11T01:58:34.543227Z","shell.execute_reply":"2025-06-11T01:58:34.546968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T01:58:34.548834Z","iopub.execute_input":"2025-06-11T01:58:34.549105Z","iopub.status.idle":"2025-06-11T01:58:34.572382Z","shell.execute_reply.started":"2025-06-11T01:58:34.549080Z","shell.execute_reply":"2025-06-11T01:58:34.571515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(X_test)\nprint(time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:00:32.293396Z","iopub.execute_input":"2025-06-11T02:00:32.293772Z","iopub.status.idle":"2025-06-11T02:00:46.383904Z","shell.execute_reply.started":"2025-06-11T02:00:32.293748Z","shell.execute_reply":"2025-06-11T02:00:46.382924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_pred,y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:01:20.462137Z","iopub.execute_input":"2025-06-11T02:01:20.462998Z","iopub.status.idle":"2025-06-11T02:01:20.471162Z","shell.execute_reply.started":"2025-06-11T02:01:20.462966Z","shell.execute_reply":"2025-06-11T02:01:20.470159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:05:18.155039Z","iopub.execute_input":"2025-06-11T02:05:18.155374Z","iopub.status.idle":"2025-06-11T02:05:18.160607Z","shell.execute_reply.started":"2025-06-11T02:05:18.155351Z","shell.execute_reply":"2025-06-11T02:05:18.159174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sc=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:17:49.542290Z","iopub.execute_input":"2025-06-11T02:17:49.542650Z","iopub.status.idle":"2025-06-11T02:17:49.548139Z","shell.execute_reply.started":"2025-06-11T02:17:49.542609Z","shell.execute_reply":"2025-06-11T02:17:49.547006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train=sc.fit_transform(X_train)\nX_test=sc.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:18:23.184718Z","iopub.execute_input":"2025-06-11T02:18:23.185033Z","iopub.status.idle":"2025-06-11T02:18:23.769573Z","shell.execute_reply.started":"2025-06-11T02:18:23.185011Z","shell.execute_reply":"2025-06-11T02:18:23.768554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:20:12.453280Z","iopub.execute_input":"2025-06-11T02:20:12.453697Z","iopub.status.idle":"2025-06-11T02:20:12.459165Z","shell.execute_reply.started":"2025-06-11T02:20:12.453669Z","shell.execute_reply":"2025-06-11T02:20:12.457676Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:21:48.181492Z","iopub.execute_input":"2025-06-11T02:21:48.181943Z","iopub.status.idle":"2025-06-11T02:21:51.273718Z","shell.execute_reply.started":"2025-06-11T02:21:48.181915Z","shell.execute_reply":"2025-06-11T02:21:51.272164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"KNN=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:22:13.009491Z","iopub.execute_input":"2025-06-11T02:22:13.009810Z","iopub.status.idle":"2025-06-11T02:22:13.015520Z","shell.execute_reply.started":"2025-06-11T02:22:13.009790Z","shell.execute_reply":"2025-06-11T02:22:13.014187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"KNN.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:22:27.997263Z","iopub.execute_input":"2025-06-11T02:22:27.998142Z","iopub.status.idle":"2025-06-11T02:22:28.019138Z","shell.execute_reply.started":"2025-06-11T02:22:27.998111Z","shell.execute_reply":"2025-06-11T02:22:28.017550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=KNN.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:22:43.530449Z","iopub.execute_input":"2025-06-11T02:22:43.530809Z","iopub.status.idle":"2025-06-11T02:22:45.388387Z","shell.execute_reply.started":"2025-06-11T02:22:43.530787Z","shell.execute_reply":"2025-06-11T02:22:45.387417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_pred,y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:22:55.382820Z","iopub.execute_input":"2025-06-11T02:22:55.383133Z","iopub.status.idle":"2025-06-11T02:22:55.391432Z","shell.execute_reply.started":"2025-06-11T02:22:55.383110Z","shell.execute_reply":"2025-06-11T02:22:55.390434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range (1,785):\n    pca1=PCA(n_components=i)\n    X_train_trf=pca1.fit_transform(X_train)\n    X_test_trf=pca1.transform(X_test)\n    KNN1=KNeighborsClassifier()\n    KNN1.fit(X_train_trf,y_train)\n    y_pred=KNN1.predict(X_test_trf)\n    print(accuracy_score(y_pred,y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:24:44.288969Z","iopub.execute_input":"2025-06-11T02:24:44.289321Z","iopub.status.idle":"2025-06-11T02:25:58.905260Z","shell.execute_reply.started":"2025-06-11T02:24:44.289296Z","shell.execute_reply":"2025-06-11T02:25:58.902198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming to a 2D coordinate system\npca2 = PCA(n_components=2)\nX_train_trf = pca2.fit_transform(X_train)\nX_test_trf = pca2.transform(X_test)\n\nimport plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter(x=X_train_trf[:,0],\n                 y=X_train_trf[:,1],\n                 color=y_train_trf,\n                 color_discrete_sequence=px.colors.qualitative.G10\n                )\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:27:52.829276Z","iopub.execute_input":"2025-06-11T02:27:52.829608Z","iopub.status.idle":"2025-06-11T02:27:53.760970Z","shell.execute_reply.started":"2025-06-11T02:27:52.829568Z","shell.execute_reply":"2025-06-11T02:27:53.759597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming in 3D\npca3 = PCA(n_components=3)\nX_train_trf = pca3.fit_transform(X_train)\nX_test_trf = pca3.transform(X_test)\n\nimport plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df, x=X_train_trf[:,0], y=X_train_trf[:,1], z=X_train_trf[:,2],\n              color=y_train_trf)\nfig.update_layout(\n    margin=dict(l=20, r=20, t=20, b=20),\n    paper_bgcolor=\"LightSteelBlue\",\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:27:26.304098Z","iopub.execute_input":"2025-06-11T02:27:26.304438Z","iopub.status.idle":"2025-06-11T02:27:27.347688Z","shell.execute_reply.started":"2025-06-11T02:27:26.304414Z","shell.execute_reply":"2025-06-11T02:27:27.346170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca3.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:28:52.309702Z","iopub.execute_input":"2025-06-11T02:28:52.310038Z","iopub.status.idle":"2025-06-11T02:28:52.316750Z","shell.execute_reply.started":"2025-06-11T02:28:52.310016Z","shell.execute_reply":"2025-06-11T02:28:52.315683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca3.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:29:07.169394Z","iopub.execute_input":"2025-06-11T02:29:07.169793Z","iopub.status.idle":"2025-06-11T02:29:07.175822Z","shell.execute_reply.started":"2025-06-11T02:29:07.169768Z","shell.execute_reply":"2025-06-11T02:29:07.174877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(pca3.explained_variance_)/sum(pca3.explained_variance_)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:32:07.492983Z","iopub.execute_input":"2025-06-11T02:32:07.493317Z","iopub.status.idle":"2025-06-11T02:32:07.500155Z","shell.execute_reply.started":"2025-06-11T02:32:07.493295Z","shell.execute_reply":"2025-06-11T02:32:07.499261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca3.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:34:09.942934Z","iopub.execute_input":"2025-06-11T02:34:09.943849Z","iopub.status.idle":"2025-06-11T02:34:09.951699Z","shell.execute_reply.started":"2025-06-11T02:34:09.943818Z","shell.execute_reply":"2025-06-11T02:34:09.949932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca784=PCA(n_components=None)\nX_train_trf=pca784.fit_transform(X_train)\nX_test_trf=pca784.transform(X_test)\nKNN784=KNeighborsClassifier()\nKNN784.fit(X_train_trf,y_train)\ny_pred=KNN784.predict(X_test_trf)\nprint(accuracy_score(y_pred,y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:36:23.931442Z","iopub.execute_input":"2025-06-11T02:36:23.932274Z","iopub.status.idle":"2025-06-11T02:36:37.143929Z","shell.execute_reply.started":"2025-06-11T02:36:23.932240Z","shell.execute_reply":"2025-06-11T02:36:37.142514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape\npca.components_.shape\nnp.cumsum(pca.explained_variance_ratio_)\nplt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:37:10.643842Z","iopub.execute_input":"2025-06-11T02:37:10.644144Z","iopub.status.idle":"2025-06-11T02:37:10.813657Z","shell.execute_reply.started":"2025-06-11T02:37:10.644125Z","shell.execute_reply":"2025-06-11T02:37:10.812731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca784.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:38:21.692176Z","iopub.execute_input":"2025-06-11T02:38:21.693110Z","iopub.status.idle":"2025-06-11T02:38:21.704219Z","shell.execute_reply.started":"2025-06-11T02:38:21.693081Z","shell.execute_reply":"2025-06-11T02:38:21.703400Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca784.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:38:59.794979Z","iopub.execute_input":"2025-06-11T02:38:59.795279Z","iopub.status.idle":"2025-06-11T02:38:59.802371Z","shell.execute_reply.started":"2025-06-11T02:38:59.795258Z","shell.execute_reply":"2025-06-11T02:38:59.801383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca784.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:39:04.663342Z","iopub.execute_input":"2025-06-11T02:39:04.663748Z","iopub.status.idle":"2025-06-11T02:39:04.675766Z","shell.execute_reply.started":"2025-06-11T02:39:04.663716Z","shell.execute_reply":"2025-06-11T02:39:04.674930Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca784.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:39:19.924960Z","iopub.execute_input":"2025-06-11T02:39:19.925285Z","iopub.status.idle":"2025-06-11T02:39:19.937012Z","shell.execute_reply.started":"2025-06-11T02:39:19.925262Z","shell.execute_reply":"2025-06-11T02:39:19.935949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca784.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-11T02:39:42.273451Z","iopub.execute_input":"2025-06-11T02:39:42.273808Z","iopub.status.idle":"2025-06-11T02:39:42.440664Z","shell.execute_reply.started":"2025-06-11T02:39:42.273784Z","shell.execute_reply":"2025-06-11T02:39:42.439742Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}