{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2022-09-18T12:50:26.737932Z","iopub.execute_input":"2022-09-18T12:50:26.739123Z","iopub.status.idle":"2022-09-18T12:50:26.774866Z","shell.execute_reply.started":"2022-09-18T12:50:26.738936Z","shell.execute_reply":"2022-09-18T12:50:26.773251Z"},"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":"2022-09-18T13:45:03.343321Z","iopub.execute_input":"2022-09-18T13:45:03.343908Z","iopub.status.idle":"2022-09-18T13:45:06.216743Z","shell.execute_reply.started":"2022-09-18T13:45:03.343860Z","shell.execute_reply":"2022-09-18T13:45:06.214925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample(2)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T12:51:41.754248Z","iopub.execute_input":"2022-09-18T12:51:41.754749Z","iopub.status.idle":"2022-09-18T12:51:41.779784Z","shell.execute_reply.started":"2022-09-18T12:51:41.754710Z","shell.execute_reply":"2022-09-18T12:51:41.777856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape # label is 1 more column(hence we have 784+1=785)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T12:55:49.053554Z","iopub.execute_input":"2022-09-18T12:55:49.054118Z","iopub.status.idle":"2022-09-18T12:55:49.063885Z","shell.execute_reply.started":"2022-09-18T12:55:49.054068Z","shell.execute_reply":"2022-09-18T12:55:49.062368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df.iloc[26596,1:].values.reshape(28,28),cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-09-18T12:58:29.716073Z","iopub.execute_input":"2022-09-18T12:58:29.716616Z","iopub.status.idle":"2022-09-18T12:58:29.947595Z","shell.execute_reply.started":"2022-09-18T12:58:29.716563Z","shell.execute_reply":"2022-09-18T12:58:29.946455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-09-18T13:45:11.921483Z","iopub.execute_input":"2022-09-18T13:45:11.922013Z","iopub.status.idle":"2022-09-18T13:45:11.930013Z","shell.execute_reply.started":"2022-09-18T13:45:11.921963Z","shell.execute_reply":"2022-09-18T13:45:11.928485Z"},"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":"2022-09-18T13:46:02.288580Z","iopub.execute_input":"2022-09-18T13:46:02.289082Z","iopub.status.idle":"2022-09-18T13:46:02.629551Z","shell.execute_reply.started":"2022-09-18T13:46:02.289039Z","shell.execute_reply":"2022-09-18T13:46:02.628103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-18T13:45:23.146174Z","iopub.execute_input":"2022-09-18T13:45:23.146664Z","iopub.status.idle":"2022-09-18T13:45:23.157868Z","shell.execute_reply.started":"2022-09-18T13:45:23.146622Z","shell.execute_reply":"2022-09-18T13:45:23.155912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn= KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-09-18T13:33:02.236452Z","iopub.execute_input":"2022-09-18T13:33:02.237695Z","iopub.status.idle":"2022-09-18T13:33:02.244425Z","shell.execute_reply.started":"2022-09-18T13:33:02.237622Z","shell.execute_reply":"2022-09-18T13:33:02.242919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T13:33:30.341679Z","iopub.execute_input":"2022-09-18T13:33:30.342525Z","iopub.status.idle":"2022-09-18T13:33:30.371833Z","shell.execute_reply.started":"2022-09-18T13:33:30.342455Z","shell.execute_reply":"2022-09-18T13:33:30.370390Z"},"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":"2022-09-18T13:36:27.380347Z","iopub.execute_input":"2022-09-18T13:36:27.380837Z","iopub.status.idle":"2022-09-18T13:36:41.035928Z","shell.execute_reply.started":"2022-09-18T13:36:27.380799Z","shell.execute_reply":"2022-09-18T13:36:41.034551Z"},"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":"2022-09-18T13:37:42.709612Z","iopub.execute_input":"2022-09-18T13:37:42.711026Z","iopub.status.idle":"2022-09-18T13:37:42.721120Z","shell.execute_reply.started":"2022-09-18T13:37:42.710968Z","shell.execute_reply":"2022-09-18T13:37:42.719498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-09-18T13:38:54.456633Z","iopub.execute_input":"2022-09-18T13:38:54.457210Z","iopub.status.idle":"2022-09-18T13:38:54.463823Z","shell.execute_reply.started":"2022-09-18T13:38:54.457136Z","shell.execute_reply":"2022-09-18T13:38:54.462755Z"},"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":"2022-09-18T13:46:47.249278Z","iopub.execute_input":"2022-09-18T13:46:47.250860Z","iopub.status.idle":"2022-09-18T13:46:47.773327Z","shell.execute_reply.started":"2022-09-18T13:46:47.250794Z","shell.execute_reply":"2022-09-18T13:46:47.771760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA\nfrom sklearn.decomposition import PCA\npca=PCA(n_components=None)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T13:47:04.854880Z","iopub.execute_input":"2022-09-18T13:47:04.855424Z","iopub.status.idle":"2022-09-18T13:47:04.861698Z","shell.execute_reply.started":"2022-09-18T13:47:04.855378Z","shell.execute_reply":"2022-09-18T13:47:04.860262Z"},"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":"2022-09-18T13:47:34.853427Z","iopub.execute_input":"2022-09-18T13:47:34.853934Z","iopub.status.idle":"2022-09-18T13:47:41.855216Z","shell.execute_reply.started":"2022-09-18T13:47:34.853887Z","shell.execute_reply":"2022-09-18T13:47:41.852940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-18T13:47:42.261503Z","iopub.execute_input":"2022-09-18T13:47:42.262068Z","iopub.status.idle":"2022-09-18T13:47:42.271421Z","shell.execute_reply.started":"2022-09-18T13:47:42.262016Z","shell.execute_reply":"2022-09-18T13:47:42.269643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=75)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T14:00:39.799379Z","iopub.execute_input":"2022-09-18T14:00:39.800531Z","iopub.status.idle":"2022-09-18T14:00:44.536262Z","shell.execute_reply.started":"2022-09-18T14:00:39.800466Z","shell.execute_reply":"2022-09-18T14:00:44.534292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-18T14:00:59.310907Z","iopub.execute_input":"2022-09-18T14:00:59.311482Z","iopub.status.idle":"2022-09-18T14:00:59.321409Z","shell.execute_reply.started":"2022-09-18T14:00:59.311430Z","shell.execute_reply":"2022-09-18T14:00:59.319666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-09-18T14:01:00.625999Z","iopub.execute_input":"2022-09-18T14:01:00.627680Z","iopub.status.idle":"2022-09-18T14:01:00.633471Z","shell.execute_reply.started":"2022-09-18T14:01:00.627615Z","shell.execute_reply":"2022-09-18T14:01:00.632078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T14:01:03.418003Z","iopub.execute_input":"2022-09-18T14:01:03.419106Z","iopub.status.idle":"2022-09-18T14:01:03.436233Z","shell.execute_reply.started":"2022-09-18T14:01:03.418981Z","shell.execute_reply":"2022-09-18T14:01:03.434367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=knn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T14:01:04.790007Z","iopub.execute_input":"2022-09-18T14:01:04.790601Z","iopub.status.idle":"2022-09-18T14:01:11.609265Z","shell.execute_reply.started":"2022-09-18T14:01:04.790549Z","shell.execute_reply":"2022-09-18T14:01:11.607681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T14:01:12.978112Z","iopub.execute_input":"2022-09-18T14:01:12.979208Z","iopub.status.idle":"2022-09-18T14:01:12.989329Z","shell.execute_reply.started":"2022-09-18T14:01:12.979119Z","shell.execute_reply":"2022-09-18T14:01:12.988128Z"},"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    y_pred=knn.predict(X_test_trf)\n    print(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-09-18T14:04:28.439181Z","iopub.execute_input":"2022-09-18T14:04:28.439749Z","iopub.status.idle":"2022-09-18T16:35:31.280443Z","shell.execute_reply.started":"2022-09-18T14:04:28.439705Z","shell.execute_reply":"2022-09-18T16:35:31.275514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Transforming in 2D coordinate system","metadata":{}},{"cell_type":"code","source":"pca=PCA(n_components=2)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T16:36:00.211696Z","iopub.execute_input":"2022-09-18T16:36:00.212273Z","iopub.status.idle":"2022-09-18T16:36:03.651257Z","shell.execute_reply.started":"2022-09-18T16:36:00.212222Z","shell.execute_reply":"2022-09-18T16:36:03.649864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_trf","metadata":{"execution":{"iopub.status.busy":"2022-09-18T16:36:13.058835Z","iopub.execute_input":"2022-09-18T16:36:13.059396Z","iopub.status.idle":"2022-09-18T16:36:13.068437Z","shell.execute_reply.started":"2022-09-18T16:36:13.059345Z","shell.execute_reply":"2022-09-18T16:36:13.066807Z"},"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":"2022-09-18T16:36:17.002830Z","iopub.execute_input":"2022-09-18T16:36:17.003384Z","iopub.status.idle":"2022-09-18T16:36:19.649517Z","shell.execute_reply.started":"2022-09-18T16:36:17.003329Z","shell.execute_reply":"2022-09-18T16:36:19.647616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transforming 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":"2022-09-18T16:36:29.234732Z","iopub.execute_input":"2022-09-18T16:36:29.235274Z","iopub.status.idle":"2022-09-18T16:36:32.173088Z","shell.execute_reply.started":"2022-09-18T16:36:29.235230Z","shell.execute_reply":"2022-09-18T16:36:32.171167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2022-09-18T17:01:05.871430Z","iopub.execute_input":"2022-09-18T17:01:05.874455Z","iopub.status.idle":"2022-09-18T17:01:05.889094Z","shell.execute_reply.started":"2022-09-18T17:01:05.874312Z","shell.execute_reply":"2022-09-18T17:01:05.887532Z"},"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(margin=dict(l=20,r=20,t=20,b=20))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-18T17:41:47.772739Z","iopub.execute_input":"2022-09-18T17:41:47.773395Z","iopub.status.idle":"2022-09-18T17:41:48.052848Z","shell.execute_reply.started":"2022-09-18T17:41:47.773344Z","shell.execute_reply":"2022-09-18T17:41:48.051271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Eigenvalues\npca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2022-09-18T17:43:38.305352Z","iopub.execute_input":"2022-09-18T17:43:38.305866Z","iopub.status.idle":"2022-09-18T17:43:38.315730Z","shell.execute_reply.started":"2022-09-18T17:43:38.305824Z","shell.execute_reply":"2022-09-18T17:43:38.314397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}