{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30732,"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":"2024-07-02T08:19:27.108590Z","iopub.execute_input":"2024-07-02T08:19:27.109011Z","iopub.status.idle":"2024-07-02T08:19:27.117233Z","shell.execute_reply.started":"2024-07-02T08:19:27.108980Z","shell.execute_reply":"2024-07-02T08:19:27.115954Z"},"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-07-02T08:34:03.222248Z","iopub.execute_input":"2024-07-02T08:34:03.222680Z","iopub.status.idle":"2024-07-02T08:34:06.074013Z","shell.execute_reply.started":"2024-07-02T08:34:03.222651Z","shell.execute_reply":"2024-07-02T08:34:06.072835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.075975Z","iopub.execute_input":"2024-07-02T08:34:06.076328Z","iopub.status.idle":"2024-07-02T08:34:06.096790Z","shell.execute_reply.started":"2024-07-02T08:34:06.076300Z","shell.execute_reply":"2024-07-02T08:34:06.095734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.098164Z","iopub.execute_input":"2024-07-02T08:34:06.098629Z","iopub.status.idle":"2024-07-02T08:34:06.110985Z","shell.execute_reply.started":"2024-07-02T08:34:06.098591Z","shell.execute_reply":"2024-07-02T08:34:06.109842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample( 2)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.115634Z","iopub.execute_input":"2024-07-02T08:34:06.116193Z","iopub.status.idle":"2024-07-02T08:34:06.138168Z","shell.execute_reply.started":"2024-07-02T08:34:06.116158Z","shell.execute_reply":"2024-07-02T08:34:06.136835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.139681Z","iopub.execute_input":"2024-07-02T08:34:06.140162Z","iopub.status.idle":"2024-07-02T08:34:06.149436Z","shell.execute_reply.started":"2024-07-02T08:34:06.140123Z","shell.execute_reply":"2024-07-02T08:34:06.148113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.151261Z","iopub.execute_input":"2024-07-02T08:34:06.151710Z","iopub.status.idle":"2024-07-02T08:34:06.384110Z","shell.execute_reply.started":"2024-07-02T08:34:06.151670Z","shell.execute_reply":"2024-07-02T08:34:06.382827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.iloc[18306,1:].values","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.385508Z","iopub.execute_input":"2024-07-02T08:34:06.385905Z","iopub.status.idle":"2024-07-02T08:34:06.399713Z","shell.execute_reply.started":"2024-07-02T08:34:06.385873Z","shell.execute_reply":"2024-07-02T08:34:06.398453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.401484Z","iopub.execute_input":"2024-07-02T08:34:06.402654Z","iopub.status.idle":"2024-07-02T08:34:06.408230Z","shell.execute_reply.started":"2024-07-02T08:34:06.402611Z","shell.execute_reply":"2024-07-02T08:34:06.407219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.409631Z","iopub.execute_input":"2024-07-02T08:34:06.410029Z","iopub.status.idle":"2024-07-02T08:34:06.420124Z","shell.execute_reply.started":"2024-07-02T08:34:06.410000Z","shell.execute_reply":"2024-07-02T08:34:06.418865Z"},"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-07-02T08:34:06.423667Z","iopub.execute_input":"2024-07-02T08:34:06.424080Z","iopub.status.idle":"2024-07-02T08:34:06.653691Z","shell.execute_reply.started":"2024-07-02T08:34:06.424051Z","shell.execute_reply":"2024-07-02T08:34:06.652470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.655367Z","iopub.execute_input":"2024-07-02T08:34:06.656370Z","iopub.status.idle":"2024-07-02T08:34:06.661307Z","shell.execute_reply.started":"2024-07-02T08:34:06.656330Z","shell.execute_reply":"2024-07-02T08:34:06.660174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.662624Z","iopub.execute_input":"2024-07-02T08:34:06.662990Z","iopub.status.idle":"2024-07-02T08:34:06.674476Z","shell.execute_reply.started":"2024-07-02T08:34:06.662961Z","shell.execute_reply":"2024-07-02T08:34:06.673343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train , y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.675863Z","iopub.execute_input":"2024-07-02T08:34:06.676189Z","iopub.status.idle":"2024-07-02T08:34:06.813367Z","shell.execute_reply.started":"2024-07-02T08:34:06.676162Z","shell.execute_reply":"2024-07-02T08:34:06.812258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:06.814946Z","iopub.execute_input":"2024-07-02T08:34:06.815371Z","iopub.status.idle":"2024-07-02T08:34:20.440328Z","shell.execute_reply.started":"2024-07-02T08:34:06.815331Z","shell.execute_reply":"2024-07-02T08:34:20.439237Z"},"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-07-02T08:34:20.441592Z","iopub.execute_input":"2024-07-02T08:34:20.441936Z","iopub.status.idle":"2024-07-02T08:34:20.450821Z","shell.execute_reply.started":"2024-07-02T08:34:20.441908Z","shell.execute_reply":"2024-07-02T08:34:20.449819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:20.452198Z","iopub.execute_input":"2024-07-02T08:34:20.453266Z","iopub.status.idle":"2024-07-02T08:34:20.459904Z","shell.execute_reply.started":"2024-07-02T08:34:20.453236Z","shell.execute_reply":"2024-07-02T08:34:20.458632Z"},"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-07-02T08:34:20.461309Z","iopub.execute_input":"2024-07-02T08:34:20.461758Z","iopub.status.idle":"2024-07-02T08:34:20.979902Z","shell.execute_reply.started":"2024-07-02T08:34:20.461700Z","shell.execute_reply":"2024-07-02T08:34:20.978717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:34:20.983373Z","iopub.execute_input":"2024-07-02T08:34:20.983756Z","iopub.status.idle":"2024-07-02T08:34:20.988942Z","shell.execute_reply.started":"2024-07-02T08:34:20.983712Z","shell.execute_reply":"2024-07-02T08:34:20.987818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:36:49.232310Z","iopub.execute_input":"2024-07-02T08:36:49.232722Z","iopub.status.idle":"2024-07-02T08:36:49.238385Z","shell.execute_reply.started":"2024-07-02T08:36:49.232691Z","shell.execute_reply":"2024-07-02T08:36:49.237012Z"},"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-07-02T08:36:49.437626Z","iopub.execute_input":"2024-07-02T08:36:49.438042Z","iopub.status.idle":"2024-07-02T08:36:55.441759Z","shell.execute_reply.started":"2024-07-02T08:36:49.438011Z","shell.execute_reply":"2024-07-02T08:36:55.440305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape[1]","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:36:55.443999Z","iopub.execute_input":"2024-07-02T08:36:55.448599Z","iopub.status.idle":"2024-07-02T08:36:55.457152Z","shell.execute_reply.started":"2024-07-02T08:36:55.448446Z","shell.execute_reply":"2024-07-02T08:36:55.455847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:36:55.458830Z","iopub.execute_input":"2024-07-02T08:36:55.459832Z","iopub.status.idle":"2024-07-02T08:36:55.468323Z","shell.execute_reply.started":"2024-07-02T08:36:55.459790Z","shell.execute_reply":"2024-07-02T08:36:55.466098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:36:55.472530Z","iopub.execute_input":"2024-07-02T08:36:55.477423Z","iopub.status.idle":"2024-07-02T08:36:55.548205Z","shell.execute_reply.started":"2024-07-02T08:36:55.477355Z","shell.execute_reply":"2024-07-02T08:36:55.546655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:36:55.556381Z","iopub.execute_input":"2024-07-02T08:36:55.558795Z","iopub.status.idle":"2024-07-02T08:36:58.277030Z","shell.execute_reply.started":"2024-07-02T08:36:55.558712Z","shell.execute_reply":"2024-07-02T08:36:58.275846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:36:58.278709Z","iopub.execute_input":"2024-07-02T08:36:58.279160Z","iopub.status.idle":"2024-07-02T08:36:58.288044Z","shell.execute_reply.started":"2024-07-02T08:36:58.279119Z","shell.execute_reply":"2024-07-02T08:36:58.286959Z"},"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#     knn = KNeighborsClassifier()\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":"2024-07-02T09:04:01.629571Z","iopub.execute_input":"2024-07-02T09:04:01.631006Z","iopub.status.idle":"2024-07-02T09:04:01.637635Z","shell.execute_reply.started":"2024-07-02T09:04:01.630961Z","shell.execute_reply":"2024-07-02T09:04:01.636277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=2)\nX_train_trf1 = pca.fit_transform(X_train)\nX_test_trf2 =pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:43:11.752826Z","iopub.execute_input":"2024-07-02T08:43:11.753269Z","iopub.status.idle":"2024-07-02T08:43:13.337440Z","shell.execute_reply.started":"2024-07-02T08:43:11.753231Z","shell.execute_reply":"2024-07-02T08:43:13.336467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf1","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:43:23.237404Z","iopub.execute_input":"2024-07-02T08:43:23.238312Z","iopub.status.idle":"2024-07-02T08:43:23.245980Z","shell.execute_reply.started":"2024-07-02T08:43:23.238275Z","shell.execute_reply":"2024-07-02T08:43:23.244597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf1 = y_train.astype(str)\nfig = px.scatter(x=X_train_trf1[:,0],\n                 y = X_train_trf1[:,1],\n                 color = y_train_trf1,\n                 color_discrete_sequence = px.colors.qualitative.G10)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:48:35.366804Z","iopub.execute_input":"2024-07-02T08:48:35.367235Z","iopub.status.idle":"2024-07-02T08:48:37.433455Z","shell.execute_reply.started":"2024-07-02T08:48:35.367204Z","shell.execute_reply":"2024-07-02T08:48:37.432335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tranfroming into 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":"2024-07-02T08:56:12.483143Z","iopub.execute_input":"2024-07-02T08:56:12.484114Z","iopub.status.idle":"2024-07-02T08:56:14.198303Z","shell.execute_reply.started":"2024-07-02T08:56:12.484077Z","shell.execute_reply":"2024-07-02T08:56:14.197241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:56:22.506605Z","iopub.execute_input":"2024-07-02T08:56:22.507063Z","iopub.status.idle":"2024-07-02T08:56:22.515358Z","shell.execute_reply.started":"2024-07-02T08:56:22.507029Z","shell.execute_reply":"2024-07-02T08:56:22.513962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df,x=X_train_trf[:,0],\n                 y = X_train_trf[:,1],\n                z=X_train_trf[:,2],\n                 color = y_train_trf1)\nfig.update_layout(\n          margin = dict(l=20 , r=20 , t =20 , b = 20))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:59:57.843375Z","iopub.execute_input":"2024-07-02T08:59:57.844406Z","iopub.status.idle":"2024-07-02T08:59:58.078087Z","shell.execute_reply.started":"2024-07-02T08:59:57.844369Z","shell.execute_reply":"2024-07-02T08:59:58.076638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:01:24.927275Z","iopub.execute_input":"2024-07-02T09:01:24.927699Z","iopub.status.idle":"2024-07-02T09:01:24.935453Z","shell.execute_reply.started":"2024-07-02T09:01:24.927666Z","shell.execute_reply":"2024-07-02T09:01:24.934274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:12:39.810615Z","iopub.execute_input":"2024-07-02T09:12:39.811061Z","iopub.status.idle":"2024-07-02T09:12:39.818029Z","shell.execute_reply.started":"2024-07-02T09:12:39.811029Z","shell.execute_reply":"2024-07-02T09:12:39.816845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(pca.explained_variance_ratio_)*100","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:32:09.775713Z","iopub.execute_input":"2024-07-02T09:32:09.776656Z","iopub.status.idle":"2024-07-02T09:32:09.784020Z","shell.execute_reply.started":"2024-07-02T09:32:09.776621Z","shell.execute_reply":"2024-07-02T09:32:09.782713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# finding optimum number of PCA\npca = PCA(n_components=None)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf =pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:34:51.051019Z","iopub.execute_input":"2024-07-02T09:34:51.051453Z","iopub.status.idle":"2024-07-02T09:34:58.275898Z","shell.execute_reply.started":"2024-07-02T09:34:51.051421Z","shell.execute_reply":"2024-07-02T09:34:58.274788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:34:58.278323Z","iopub.execute_input":"2024-07-02T09:34:58.278813Z","iopub.status.idle":"2024-07-02T09:34:58.286953Z","shell.execute_reply.started":"2024-07-02T09:34:58.278769Z","shell.execute_reply":"2024-07-02T09:34:58.285682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:34:58.288456Z","iopub.execute_input":"2024-07-02T09:34:58.289017Z","iopub.status.idle":"2024-07-02T09:34:58.302147Z","shell.execute_reply.started":"2024-07-02T09:34:58.288985Z","shell.execute_reply":"2024-07-02T09:34:58.300863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:36:55.890387Z","iopub.execute_input":"2024-07-02T09:36:55.890820Z","iopub.status.idle":"2024-07-02T09:36:55.909966Z","shell.execute_reply.started":"2024-07-02T09:36:55.890778Z","shell.execute_reply":"2024-07-02T09:36:55.908347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:35:49.730409Z","iopub.execute_input":"2024-07-02T09:35:49.730906Z","iopub.status.idle":"2024-07-02T09:35:49.750224Z","shell.execute_reply.started":"2024-07-02T09:35:49.730871Z","shell.execute_reply":"2024-07-02T09:35:49.748874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-07-02T09:38:33.825263Z","iopub.execute_input":"2024-07-02T09:38:33.825695Z","iopub.status.idle":"2024-07-02T09:38:34.071842Z","shell.execute_reply.started":"2024-07-02T09:38:33.825661Z","shell.execute_reply":"2024-07-02T09:38:34.070673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}