{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30626,"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","execution":{"iopub.status.busy":"2024-01-09T17:17:07.319348Z","iopub.execute_input":"2024-01-09T17:17:07.319798Z","iopub.status.idle":"2024-01-09T17:17:07.328513Z","shell.execute_reply.started":"2024-01-09T17:17:07.319761Z","shell.execute_reply":"2024-01-09T17:17:07.327096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:07.330615Z","iopub.execute_input":"2024-01-09T17:17:07.331086Z","iopub.status.idle":"2024-01-09T17:17:10.314822Z","shell.execute_reply.started":"2024-01-09T17:17:07.331051Z","shell.execute_reply":"2024-01-09T17:17:10.313654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.316470Z","iopub.execute_input":"2024-01-09T17:17:10.316783Z","iopub.status.idle":"2024-01-09T17:17:10.337706Z","shell.execute_reply.started":"2024-01-09T17:17:10.316755Z","shell.execute_reply":"2024-01-09T17:17:10.336327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.341968Z","iopub.execute_input":"2024-01-09T17:17:10.342494Z","iopub.status.idle":"2024-01-09T17:17:10.349971Z","shell.execute_reply.started":"2024-01-09T17:17:10.342448Z","shell.execute_reply":"2024-01-09T17:17:10.348611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.353801Z","iopub.execute_input":"2024-01-09T17:17:10.354272Z","iopub.status.idle":"2024-01-09T17:17:10.359599Z","shell.execute_reply.started":"2024-01-09T17:17:10.354223Z","shell.execute_reply":"2024-01-09T17:17:10.358363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.361495Z","iopub.execute_input":"2024-01-09T17:17:10.361901Z","iopub.status.idle":"2024-01-09T17:17:10.384920Z","shell.execute_reply.started":"2024-01-09T17:17:10.361856Z","shell.execute_reply":"2024-01-09T17:17:10.383816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[21383,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.386499Z","iopub.execute_input":"2024-01-09T17:17:10.387150Z","iopub.status.idle":"2024-01-09T17:17:10.676887Z","shell.execute_reply.started":"2024-01-09T17:17:10.387115Z","shell.execute_reply":"2024-01-09T17:17:10.675585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= df.iloc[:,1:]\ny= df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.678413Z","iopub.execute_input":"2024-01-09T17:17:10.679178Z","iopub.status.idle":"2024-01-09T17:17:10.685790Z","shell.execute_reply.started":"2024-01-09T17:17:10.679145Z","shell.execute_reply":"2024-01-09T17:17:10.684494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.687347Z","iopub.execute_input":"2024-01-09T17:17:10.688304Z","iopub.status.idle":"2024-01-09T17:17:10.906648Z","shell.execute_reply.started":"2024-01-09T17:17:10.688261Z","shell.execute_reply":"2024-01-09T17:17:10.905458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.908063Z","iopub.execute_input":"2024-01-09T17:17:10.908430Z","iopub.status.idle":"2024-01-09T17:17:10.915449Z","shell.execute_reply.started":"2024-01-09T17:17:10.908377Z","shell.execute_reply":"2024-01-09T17:17:10.914370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.921544Z","iopub.execute_input":"2024-01-09T17:17:10.922322Z","iopub.status.idle":"2024-01-09T17:17:10.927102Z","shell.execute_reply.started":"2024-01-09T17:17:10.922286Z","shell.execute_reply":"2024-01-09T17:17:10.926015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn= KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.928882Z","iopub.execute_input":"2024-01-09T17:17:10.929314Z","iopub.status.idle":"2024-01-09T17:17:10.936281Z","shell.execute_reply.started":"2024-01-09T17:17:10.929275Z","shell.execute_reply":"2024-01-09T17:17:10.935125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:10.938157Z","iopub.execute_input":"2024-01-09T17:17:10.938771Z","iopub.status.idle":"2024-01-09T17:17:11.068601Z","shell.execute_reply.started":"2024-01-09T17:17:10.938715Z","shell.execute_reply":"2024-01-09T17:17:11.067461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(x_test)\nprint(time.time()-start)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:11.070180Z","iopub.execute_input":"2024-01-09T17:17:11.070872Z","iopub.status.idle":"2024-01-09T17:17:24.133764Z","shell.execute_reply.started":"2024-01-09T17:17:11.070826Z","shell.execute_reply":"2024-01-09T17:17:24.132453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:24.135309Z","iopub.execute_input":"2024-01-09T17:17:24.135824Z","iopub.status.idle":"2024-01-09T17:17:24.145682Z","shell.execute_reply.started":"2024-01-09T17:17:24.135771Z","shell.execute_reply":"2024-01-09T17:17:24.144564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler= StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:24.147321Z","iopub.execute_input":"2024-01-09T17:17:24.147792Z","iopub.status.idle":"2024-01-09T17:17:24.153874Z","shell.execute_reply.started":"2024-01-09T17:17:24.147750Z","shell.execute_reply":"2024-01-09T17:17:24.152703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train= scaler.fit_transform(x_train)\nx_test= scaler.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:24.155348Z","iopub.execute_input":"2024-01-09T17:17:24.155805Z","iopub.status.idle":"2024-01-09T17:17:24.642409Z","shell.execute_reply.started":"2024-01-09T17:17:24.155764Z","shell.execute_reply":"2024-01-09T17:17:24.641329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PCA \nfrom sklearn.decomposition import PCA\npca= PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:24.643748Z","iopub.execute_input":"2024-01-09T17:17:24.644173Z","iopub.status.idle":"2024-01-09T17:17:24.650550Z","shell.execute_reply.started":"2024-01-09T17:17:24.644134Z","shell.execute_reply":"2024-01-09T17:17:24.649370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf=pca.fit_transform(x_train)\nx_test_trf= pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:24.651841Z","iopub.execute_input":"2024-01-09T17:17:24.652213Z","iopub.status.idle":"2024-01-09T17:17:30.439720Z","shell.execute_reply.started":"2024-01-09T17:17:24.652175Z","shell.execute_reply":"2024-01-09T17:17:30.438136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:30.448610Z","iopub.execute_input":"2024-01-09T17:17:30.453565Z","iopub.status.idle":"2024-01-09T17:17:30.470593Z","shell.execute_reply.started":"2024-01-09T17:17:30.453487Z","shell.execute_reply":"2024-01-09T17:17:30.469050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn= KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:30.479220Z","iopub.execute_input":"2024-01-09T17:17:30.480474Z","iopub.status.idle":"2024-01-09T17:17:30.494204Z","shell.execute_reply.started":"2024-01-09T17:17:30.480410Z","shell.execute_reply":"2024-01-09T17:17:30.492321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:30.496898Z","iopub.execute_input":"2024-01-09T17:17:30.498162Z","iopub.status.idle":"2024-01-09T17:17:30.563945Z","shell.execute_reply.started":"2024-01-09T17:17:30.498104Z","shell.execute_reply":"2024-01-09T17:17:30.562770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred= knn.predict(x_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:30.565530Z","iopub.execute_input":"2024-01-09T17:17:30.565981Z","iopub.status.idle":"2024-01-09T17:17:33.325401Z","shell.execute_reply.started":"2024-01-09T17:17:30.565940Z","shell.execute_reply":"2024-01-09T17:17:33.324020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:33.326964Z","iopub.execute_input":"2024-01-09T17:17:33.327338Z","iopub.status.idle":"2024-01-09T17:17:33.336693Z","shell.execute_reply.started":"2024-01-09T17:17:33.327306Z","shell.execute_reply":"2024-01-09T17:17:33.335534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=3)\nx_train_trf= pca.fit_transform(x_train)\nx_test_trf=pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:33.338168Z","iopub.execute_input":"2024-01-09T17:17:33.338623Z","iopub.status.idle":"2024-01-09T17:17:35.088543Z","shell.execute_reply.started":"2024-01-09T17:17:33.338580Z","shell.execute_reply":"2024-01-09T17:17:35.086936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.090848Z","iopub.execute_input":"2024-01-09T17:17:35.091726Z","iopub.status.idle":"2024-01-09T17:17:35.307323Z","shell.execute_reply.started":"2024-01-09T17:17:35.091679Z","shell.execute_reply":"2024-01-09T17:17:35.306075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_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],color=y_train_trf)\nfig.update_layout(\nmargin=dict(l=20,r=20,t=20,b=20)\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:18:29.728908Z","iopub.execute_input":"2024-01-09T17:18:29.729707Z","iopub.status.idle":"2024-01-09T17:18:29.910574Z","shell.execute_reply.started":"2024-01-09T17:18:29.729672Z","shell.execute_reply":"2024-01-09T17:18:29.909118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n#Eigen Values","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.684556Z","iopub.status.idle":"2024-01-09T17:17:35.684968Z","shell.execute_reply.started":"2024-01-09T17:17:35.684763Z","shell.execute_reply":"2024-01-09T17:17:35.684782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen Vectors","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.686305Z","iopub.status.idle":"2024-01-09T17:17:35.686779Z","shell.execute_reply.started":"2024-01-09T17:17:35.686572Z","shell.execute_reply":"2024-01-09T17:17:35.686592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" pca= PCA(n_components=None)\n x_train_trf=pca.fit_transform(x_train)\n x_test_trf= pca.transform(x_test)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.688253Z","iopub.status.idle":"2024-01-09T17:17:35.688737Z","shell.execute_reply.started":"2024-01-09T17:17:35.688530Z","shell.execute_reply":"2024-01-09T17:17:35.688552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.690963Z","iopub.status.idle":"2024-01-09T17:17:35.691553Z","shell.execute_reply.started":"2024-01-09T17:17:35.691308Z","shell.execute_reply":"2024-01-09T17:17:35.691328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.692519Z","iopub.status.idle":"2024-01-09T17:17:35.693611Z","shell.execute_reply.started":"2024-01-09T17:17:35.693334Z","shell.execute_reply":"2024-01-09T17:17:35.693357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.694547Z","iopub.status.idle":"2024-01-09T17:17:35.694981Z","shell.execute_reply.started":"2024-01-09T17:17:35.694787Z","shell.execute_reply":"2024-01-09T17:17:35.694806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i in range(1,785):\n#     pca= PCA(n_components=i)\n#     x_train_trf=pca.fit_transform(x_train)\n#     x_test_trf= pca.transform(x_test)\n    \n#     knn= KNeighborsClassifier()\n#     knn.fit(x_train_trf,y_train)\n#     y_pred= knn.predict(x_test_trf)\n    \n#     print(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:17:35.696540Z","iopub.status.idle":"2024-01-09T17:17:35.697072Z","shell.execute_reply.started":"2024-01-09T17:17:35.696792Z","shell.execute_reply":"2024-01-09T17:17:35.696817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}