{"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":30558,"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-09-08T07:50:05.626139Z","iopub.execute_input":"2023-09-08T07:50:05.626484Z","iopub.status.idle":"2023-09-08T07:50:05.934229Z","shell.execute_reply.started":"2023-09-08T07:50:05.626451Z","shell.execute_reply":"2023-09-08T07:50:05.933051Z"},"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-09-08T08:09:50.416172Z","iopub.execute_input":"2023-09-08T08:09:50.416511Z","iopub.status.idle":"2023-09-08T08:09:52.078843Z","shell.execute_reply.started":"2023-09-08T08:09:50.416489Z","shell.execute_reply":"2023-09-08T08:09:52.077711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:09:53.566604Z","iopub.execute_input":"2023-09-08T08:09:53.566911Z","iopub.status.idle":"2023-09-08T08:09:53.582069Z","shell.execute_reply.started":"2023-09-08T08:09:53.566887Z","shell.execute_reply":"2023-09-08T08:09:53.581369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:09:56.067009Z","iopub.execute_input":"2023-09-08T08:09:56.068011Z","iopub.status.idle":"2023-09-08T08:09:56.072638Z","shell.execute_reply.started":"2023-09-08T08:09:56.067973Z","shell.execute_reply":"2023-09-08T08:09:56.071728Z"},"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":"2023-09-08T08:09:58.756672Z","iopub.execute_input":"2023-09-08T08:09:58.756975Z","iopub.status.idle":"2023-09-08T08:09:58.918653Z","shell.execute_reply.started":"2023-09-08T08:09:58.756952Z","shell.execute_reply":"2023-09-08T08:09:58.917742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:, 1:]\ny = df.iloc[:, 0]\nfrom 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":"2023-09-08T08:10:03.745597Z","iopub.execute_input":"2023-09-08T08:10:03.746410Z","iopub.status.idle":"2023-09-08T08:10:03.917743Z","shell.execute_reply.started":"2023-09-08T08:10:03.746382Z","shell.execute_reply":"2023-09-08T08:10:03.916732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:10:06.373716Z","iopub.execute_input":"2023-09-08T08:10:06.374064Z","iopub.status.idle":"2023-09-08T08:10:06.379524Z","shell.execute_reply.started":"2023-09-08T08:10:06.374036Z","shell.execute_reply":"2023-09-08T08:10:06.378655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:10:09.024053Z","iopub.execute_input":"2023-09-08T08:10:09.025098Z","iopub.status.idle":"2023-09-08T08:10:09.029558Z","shell.execute_reply.started":"2023-09-08T08:10:09.025055Z","shell.execute_reply":"2023-09-08T08:10:09.028601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:10:11.458120Z","iopub.execute_input":"2023-09-08T08:10:11.458465Z","iopub.status.idle":"2023-09-08T08:10:11.543139Z","shell.execute_reply.started":"2023-09-08T08:10:11.458441Z","shell.execute_reply":"2023-09-08T08:10:11.542070Z"},"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":"2023-09-08T08:10:16.386178Z","iopub.execute_input":"2023-09-08T08:10:16.386825Z","iopub.status.idle":"2023-09-08T08:10:25.947641Z","shell.execute_reply.started":"2023-09-08T08:10:16.386793Z","shell.execute_reply":"2023-09-08T08:10:25.946799Z"},"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":"2023-09-08T08:10:25.949614Z","iopub.execute_input":"2023-09-08T08:10:25.949912Z","iopub.status.idle":"2023-09-08T08:10:25.956897Z","shell.execute_reply.started":"2023-09-08T08:10:25.949884Z","shell.execute_reply":"2023-09-08T08:10:25.956001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:02:40.733238Z","iopub.execute_input":"2023-09-08T08:02:40.733622Z","iopub.status.idle":"2023-09-08T08:02:40.738200Z","shell.execute_reply.started":"2023-09-08T08:02:40.733593Z","shell.execute_reply":"2023-09-08T08:02:40.736839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf = scaler.fit_transform(X_train)\nX_test_trf = scaler.transform(X_test) ","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:10:35.958997Z","iopub.execute_input":"2023-09-08T08:10:35.959330Z","iopub.status.idle":"2023-09-08T08:10:36.215090Z","shell.execute_reply.started":"2023-09-08T08:10:35.959304Z","shell.execute_reply":"2023-09-08T08:10:36.214006Z"},"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":"2023-09-08T08:18:28.104428Z","iopub.execute_input":"2023-09-08T08:18:28.104781Z","iopub.status.idle":"2023-09-08T08:18:28.109810Z","shell.execute_reply.started":"2023-09-08T08:18:28.104754Z","shell.execute_reply":"2023-09-08T08:18:28.108718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf_pca = pca.fit_transform(X_train_trf)\nX_test_trf_pca = pca.transform(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:18:28.792299Z","iopub.execute_input":"2023-09-08T08:18:28.792602Z","iopub.status.idle":"2023-09-08T08:18:32.083495Z","shell.execute_reply.started":"2023-09-08T08:18:28.792580Z","shell.execute_reply":"2023-09-08T08:18:32.082761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf_pca.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:18:32.084883Z","iopub.execute_input":"2023-09-08T08:18:32.085233Z","iopub.status.idle":"2023-09-08T08:18:32.090364Z","shell.execute_reply.started":"2023-09-08T08:18:32.085210Z","shell.execute_reply":"2023-09-08T08:18:32.089724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(X_train_trf_pca, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:18:32.091539Z","iopub.execute_input":"2023-09-08T08:18:32.092045Z","iopub.status.idle":"2023-09-08T08:18:32.119965Z","shell.execute_reply.started":"2023-09-08T08:18:32.092020Z","shell.execute_reply":"2023-09-08T08:18:32.119304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred1 = knn.predict(X_test_trf_pca)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:18:33.457444Z","iopub.execute_input":"2023-09-08T08:18:33.457768Z","iopub.status.idle":"2023-09-08T08:18:35.349543Z","shell.execute_reply.started":"2023-09-08T08:18:33.457744Z","shell.execute_reply":"2023-09-08T08:18:35.348113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred1)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:18:36.788168Z","iopub.execute_input":"2023-09-08T08:18:36.788500Z","iopub.status.idle":"2023-09-08T08:18:36.795780Z","shell.execute_reply.started":"2023-09-08T08:18:36.788477Z","shell.execute_reply":"2023-09-08T08:18:36.794696Z"},"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 = pca.fit_transform(X_train_trf)\n    X_test_trf_pca = pca.transform(X_test_trf)\n    \n    knn = KNeighborsClassifier()\n    \n    knn.fit(X_train_trf_pca, y_train)\n    \n    y_pred2 = knn.predict(X_test_trf_pca)\n    print(accuracy_score(y_test, y_pred2))","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:21:49.469993Z","iopub.execute_input":"2023-09-08T08:21:49.470328Z","iopub.status.idle":"2023-09-08T08:29:53.460400Z","shell.execute_reply.started":"2023-09-08T08:21:49.470298Z","shell.execute_reply":"2023-09-08T08:29:53.459158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components=2 )\n\nX_train_trf_pca = pca.fit_transform(X_train_trf)\nX_test_trf_pca = pca.transform(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:32:53.279667Z","iopub.execute_input":"2023-09-08T08:32:53.279982Z","iopub.status.idle":"2023-09-08T08:32:54.404645Z","shell.execute_reply.started":"2023-09-08T08:32:53.279959Z","shell.execute_reply":"2023-09-08T08:32:54.403677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf_pca","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:33:07.009421Z","iopub.execute_input":"2023-09-08T08:33:07.009718Z","iopub.status.idle":"2023-09-08T08:33:07.017329Z","shell.execute_reply.started":"2023-09-08T08:33:07.009696Z","shell.execute_reply":"2023-09-08T08:33:07.016200Z"},"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_pca[:, 0],\n                 y = X_train_trf_pca[:, 1],\n                 color = y_train_trf,\n                 color_discrete_sequence= px.colors.qualitative.G10\n                )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:36:40.543632Z","iopub.execute_input":"2023-09-08T08:36:40.544409Z","iopub.status.idle":"2023-09-08T08:36:42.524955Z","shell.execute_reply.started":"2023-09-08T08:36:40.544377Z","shell.execute_reply":"2023-09-08T08:36:42.524288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transforming in 3D\n#PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components= 3 )\n\nX_train_trf_pca = pca.fit_transform(X_train_trf)\nX_test_trf_pca = pca.transform(X_test_trf)\n\nimport plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df, x = X_train_trf_pca[:, 0], y = X_train_trf_pca[:, 1], z = X_train_trf_pca[:, 2], 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-09-08T08:45:33.519690Z","iopub.execute_input":"2023-09-08T08:45:33.520056Z","iopub.status.idle":"2023-09-08T08:45:34.837912Z","shell.execute_reply.started":"2023-09-08T08:45:33.520029Z","shell.execute_reply":"2023-09-08T08:45:34.836943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf_pca","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:41:15.449974Z","iopub.execute_input":"2023-09-08T08:41:15.450295Z","iopub.status.idle":"2023-09-08T08:41:15.456052Z","shell.execute_reply.started":"2023-09-08T08:41:15.450250Z","shell.execute_reply":"2023-09-08T08:41:15.455218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n# eigen values (lambda)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:51:53.532612Z","iopub.execute_input":"2023-09-08T08:51:53.532953Z","iopub.status.idle":"2023-09-08T08:51:53.539735Z","shell.execute_reply.started":"2023-09-08T08:51:53.532928Z","shell.execute_reply":"2023-09-08T08:51:53.538513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T08:52:40.365837Z","iopub.execute_input":"2023-09-08T08:52:40.366185Z","iopub.status.idle":"2023-09-08T08:52:40.372445Z","shell.execute_reply.started":"2023-09-08T08:52:40.366149Z","shell.execute_reply":"2023-09-08T08:52:40.371479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:06:14.285688Z","iopub.execute_input":"2023-09-08T09:06:14.286022Z","iopub.status.idle":"2023-09-08T09:06:14.293254Z","shell.execute_reply.started":"2023-09-08T09:06:14.285997Z","shell.execute_reply":"2023-09-08T09:06:14.292321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lets find out the optimum no. of PC for this dataset","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:06:42.271911Z","iopub.execute_input":"2023-09-08T09:06:42.272245Z","iopub.status.idle":"2023-09-08T09:06:42.277205Z","shell.execute_reply.started":"2023-09-08T09:06:42.272217Z","shell.execute_reply":"2023-09-08T09:06:42.275937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=None)\nX_train_trf_pca = pca.fit_transform(X_train_trf)\nX_test_trf_pca = pca.transform(X_test_trf)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:07:13.974051Z","iopub.execute_input":"2023-09-08T09:07:13.974377Z","iopub.status.idle":"2023-09-08T09:07:16.925013Z","shell.execute_reply.started":"2023-09-08T09:07:13.974356Z","shell.execute_reply":"2023-09-08T09:07:16.924130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:07:23.368695Z","iopub.execute_input":"2023-09-08T09:07:23.369180Z","iopub.status.idle":"2023-09-08T09:07:23.375138Z","shell.execute_reply.started":"2023-09-08T09:07:23.369157Z","shell.execute_reply":"2023-09-08T09:07:23.374224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:07:32.532330Z","iopub.execute_input":"2023-09-08T09:07:32.532682Z","iopub.status.idle":"2023-09-08T09:07:32.539955Z","shell.execute_reply.started":"2023-09-08T09:07:32.532656Z","shell.execute_reply":"2023-09-08T09:07:32.538876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:08:17.468907Z","iopub.execute_input":"2023-09-08T09:08:17.470095Z","iopub.status.idle":"2023-09-08T09:08:17.482302Z","shell.execute_reply.started":"2023-09-08T09:08:17.470041Z","shell.execute_reply":"2023-09-08T09:08:17.481291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:09:11.660454Z","iopub.execute_input":"2023-09-08T09:09:11.661237Z","iopub.status.idle":"2023-09-08T09:09:11.824885Z","shell.execute_reply.started":"2023-09-08T09:09:11.661208Z","shell.execute_reply":"2023-09-08T09:09:11.823827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}