{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.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":30407,"isInternetEnabled":false,"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":"2023-03-05T09:39:53.556936Z","iopub.execute_input":"2023-03-05T09:39:53.557437Z","iopub.status.idle":"2023-03-05T09:39:53.567937Z","shell.execute_reply.started":"2023-03-05T09:39:53.557378Z","shell.execute_reply":"2023-03-05T09:39:53.566247Z"},"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":"2023-03-05T09:39:53.570919Z","iopub.execute_input":"2023-03-05T09:39:53.57217Z","iopub.status.idle":"2023-03-05T09:39:56.215148Z","shell.execute_reply.started":"2023-03-05T09:39:53.572119Z","shell.execute_reply":"2023-03-05T09:39:56.213733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.217365Z","iopub.execute_input":"2023-03-05T09:39:56.217888Z","iopub.status.idle":"2023-03-05T09:39:56.241216Z","shell.execute_reply.started":"2023-03-05T09:39:56.217832Z","shell.execute_reply":"2023-03-05T09:39:56.239293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.244995Z","iopub.execute_input":"2023-03-05T09:39:56.245938Z","iopub.status.idle":"2023-03-05T09:39:56.266736Z","shell.execute_reply.started":"2023-03-05T09:39:56.245885Z","shell.execute_reply":"2023-03-05T09:39:56.265361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.268399Z","iopub.execute_input":"2023-03-05T09:39:56.268901Z","iopub.status.idle":"2023-03-05T09:39:56.275527Z","shell.execute_reply.started":"2023-03-05T09:39:56.268846Z","shell.execute_reply":"2023-03-05T09:39:56.27417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[27416,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.276952Z","iopub.execute_input":"2023-03-05T09:39:56.277295Z","iopub.status.idle":"2023-03-05T09:39:56.503665Z","shell.execute_reply.started":"2023-03-05T09:39:56.27726Z","shell.execute_reply":"2023-03-05T09:39:56.502695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.504906Z","iopub.execute_input":"2023-03-05T09:39:56.505416Z","iopub.status.idle":"2023-03-05T09:39:56.512003Z","shell.execute_reply.started":"2023-03-05T09:39:56.505362Z","shell.execute_reply":"2023-03-05T09:39:56.51061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.513635Z","iopub.execute_input":"2023-03-05T09:39:56.514383Z","iopub.status.idle":"2023-03-05T09:39:56.522451Z","shell.execute_reply.started":"2023-03-05T09:39:56.514281Z","shell.execute_reply":"2023-03-05T09:39:56.520977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.527572Z","iopub.execute_input":"2023-03-05T09:39:56.528063Z","iopub.status.idle":"2023-03-05T09:39:56.787042Z","shell.execute_reply.started":"2023-03-05T09:39:56.528006Z","shell.execute_reply":"2023-03-05T09:39:56.785687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.78902Z","iopub.execute_input":"2023-03-05T09:39:56.789445Z","iopub.status.idle":"2023-03-05T09:39:56.798179Z","shell.execute_reply.started":"2023-03-05T09:39:56.789392Z","shell.execute_reply":"2023-03-05T09:39:56.796865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.799846Z","iopub.execute_input":"2023-03-05T09:39:56.80024Z","iopub.status.idle":"2023-03-05T09:39:56.808103Z","shell.execute_reply.started":"2023-03-05T09:39:56.800193Z","shell.execute_reply":"2023-03-05T09:39:56.8066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.809658Z","iopub.execute_input":"2023-03-05T09:39:56.810097Z","iopub.status.idle":"2023-03-05T09:39:56.819265Z","shell.execute_reply.started":"2023-03-05T09:39:56.810056Z","shell.execute_reply":"2023-03-05T09:39:56.817991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:41:06.909815Z","iopub.execute_input":"2023-03-05T09:41:06.910243Z","iopub.status.idle":"2023-03-05T09:41:06.937556Z","shell.execute_reply.started":"2023-03-05T09:41:06.910204Z","shell.execute_reply":"2023-03-05T09:41:06.936236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.821298Z","iopub.execute_input":"2023-03-05T09:39:56.821935Z","iopub.status.idle":"2023-03-05T09:39:56.846653Z","shell.execute_reply.started":"2023-03-05T09:39:56.821876Z","shell.execute_reply":"2023-03-05T09:39:56.845411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstrat = time.time()\ny_pred = knn.predict(X_test)\nprint(time.time() - strat)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:39:56.848279Z","iopub.execute_input":"2023-03-05T09:39:56.848756Z","iopub.status.idle":"2023-03-05T09:40:09.220567Z","shell.execute_reply.started":"2023-03-05T09:39:56.848707Z","shell.execute_reply":"2023-03-05T09:40:09.219058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.predict(X_train.iloc[27416].values.reshape(1,1))","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:47.517618Z","iopub.execute_input":"2023-03-05T09:40:47.518129Z","iopub.status.idle":"2023-03-05T09:40:47.54443Z","shell.execute_reply.started":"2023-03-05T09:40:47.518083Z","shell.execute_reply":"2023-03-05T09:40:47.542568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\naccuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.248909Z","iopub.status.idle":"2023-03-05T09:40:09.24962Z","shell.execute_reply.started":"2023-03-05T09:40:09.249236Z","shell.execute_reply":"2023-03-05T09:40:09.249274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.251462Z","iopub.status.idle":"2023-03-05T09:40:09.252019Z","shell.execute_reply.started":"2023-03-05T09:40:09.251778Z","shell.execute_reply":"2023-03-05T09:40:09.251808Z"},"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":"2023-03-05T09:40:09.253655Z","iopub.status.idle":"2023-03-05T09:40:09.254113Z","shell.execute_reply.started":"2023-03-05T09:40:09.253886Z","shell.execute_reply":"2023-03-05T09:40:09.25391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pca\n\nfrom sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.255278Z","iopub.status.idle":"2023-03-05T09:40:09.255714Z","shell.execute_reply.started":"2023-03-05T09:40:09.255513Z","shell.execute_reply":"2023-03-05T09:40:09.255535Z"},"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":"2023-03-05T09:40:09.259647Z","iopub.status.idle":"2023-03-05T09:40:09.260373Z","shell.execute_reply.started":"2023-03-05T09:40:09.260012Z","shell.execute_reply":"2023-03-05T09:40:09.260052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.262681Z","iopub.status.idle":"2023-03-05T09:40:09.263781Z","shell.execute_reply.started":"2023-03-05T09:40:09.263425Z","shell.execute_reply":"2023-03-05T09:40:09.263466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knnn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.26556Z","iopub.status.idle":"2023-03-05T09:40:09.266215Z","shell.execute_reply.started":"2023-03-05T09:40:09.26588Z","shell.execute_reply":"2023-03-05T09:40:09.265915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knnn.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.268089Z","iopub.status.idle":"2023-03-05T09:40:09.268769Z","shell.execute_reply.started":"2023-03-05T09:40:09.268441Z","shell.execute_reply":"2023-03-05T09:40:09.268476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knnn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.271363Z","iopub.status.idle":"2023-03-05T09:40:09.272052Z","shell.execute_reply.started":"2023-03-05T09:40:09.271694Z","shell.execute_reply":"2023-03-05T09:40:09.27173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.273892Z","iopub.status.idle":"2023-03-05T09:40:09.274544Z","shell.execute_reply.started":"2023-03-05T09:40:09.274199Z","shell.execute_reply":"2023-03-05T09:40:09.274234Z"},"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#     \n#     knn.fit(X_train_trf,y_train)\n#     \n#     y_pred = knn.predict(X_test_trf)\n    \n#     print(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.278274Z","iopub.status.idle":"2023-03-05T09:40:09.279038Z","shell.execute_reply.started":"2023-03-05T09:40:09.278667Z","shell.execute_reply":"2023-03-05T09:40:09.278705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming to z 2D coordinate system\n\npca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.280898Z","iopub.status.idle":"2023-03-05T09:40:09.281601Z","shell.execute_reply.started":"2023-03-05T09:40:09.281241Z","shell.execute_reply":"2023-03-05T09:40:09.281278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.283854Z","iopub.status.idle":"2023-03-05T09:40:09.284394Z","shell.execute_reply.started":"2023-03-05T09:40:09.284112Z","shell.execute_reply":"2023-03-05T09:40:09.284162Z"},"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\n                )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.285935Z","iopub.status.idle":"2023-03-05T09:40:09.286412Z","shell.execute_reply.started":"2023-03-05T09:40:09.286173Z","shell.execute_reply":"2023-03-05T09:40:09.286197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainforming in 3D\npca = PCA(n_components = 3)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.287942Z","iopub.status.idle":"2023-03-05T09:40:09.288443Z","shell.execute_reply.started":"2023-03-05T09:40:09.288171Z","shell.execute_reply":"2023-03-05T09:40:09.288196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.290213Z","iopub.status.idle":"2023-03-05T09:40:09.290712Z","shell.execute_reply.started":"2023-03-05T09:40:09.29047Z","shell.execute_reply":"2023-03-05T09:40:09.290504Z"},"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],\n                   color=y_train_trf)\nfig.update_layout(\n    margin = dict(l=20 , r=20 , t=20 , b=20)\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.2938Z","iopub.status.idle":"2023-03-05T09:40:09.294471Z","shell.execute_reply.started":"2023-03-05T09:40:09.294126Z","shell.execute_reply":"2023-03-05T09:40:09.294161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n# Eigen value","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.296501Z","iopub.status.idle":"2023-03-05T09:40:09.297188Z","shell.execute_reply.started":"2023-03-05T09:40:09.296835Z","shell.execute_reply":"2023-03-05T09:40:09.296871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vactors","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.299373Z","iopub.status.idle":"2023-03-05T09:40:09.300057Z","shell.execute_reply.started":"2023-03-05T09:40:09.299711Z","shell.execute_reply":"2023-03-05T09:40:09.299749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=None)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.301886Z","iopub.status.idle":"2023-03-05T09:40:09.302594Z","shell.execute_reply.started":"2023-03-05T09:40:09.302232Z","shell.execute_reply":"2023-03-05T09:40:09.30227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.304468Z","iopub.status.idle":"2023-03-05T09:40:09.305116Z","shell.execute_reply.started":"2023-03-05T09:40:09.304787Z","shell.execute_reply":"2023-03-05T09:40:09.304822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.307627Z","iopub.status.idle":"2023-03-05T09:40:09.308288Z","shell.execute_reply.started":"2023-03-05T09:40:09.307959Z","shell.execute_reply":"2023-03-05T09:40:09.307995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.310098Z","iopub.status.idle":"2023-03-05T09:40:09.310754Z","shell.execute_reply.started":"2023-03-05T09:40:09.310421Z","shell.execute_reply":"2023-03-05T09:40:09.310455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.312479Z","iopub.status.idle":"2023-03-05T09:40:09.313123Z","shell.execute_reply.started":"2023-03-05T09:40:09.312811Z","shell.execute_reply":"2023-03-05T09:40:09.312845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2023-03-05T09:40:09.315307Z","iopub.status.idle":"2023-03-05T09:40:09.315816Z","shell.execute_reply.started":"2023-03-05T09:40:09.31559Z","shell.execute_reply":"2023-03-05T09:40:09.315618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}