{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:52:58.623937Z","iopub.execute_input":"2026-09-22T05:52:58.624371Z","iopub.status.idle":"2026-09-22T05:53:00.228813Z","shell.execute_reply.started":"2026-09-22T05:52:58.624329Z","shell.execute_reply":"2026-09-22T05:53:00.227631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt \nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import accuracy_score\n\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\n\nimport plotly.express as px # For dynamic visualizations","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:00.230916Z","iopub.execute_input":"2026-09-22T05:53:00.231407Z","iopub.status.idle":"2026-09-22T05:53:03.558561Z","shell.execute_reply.started":"2026-09-22T05:53:00.231376Z","shell.execute_reply":"2026-09-22T05:53:03.55752Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:03.560197Z","iopub.execute_input":"2026-09-22T05:53:03.560907Z","iopub.status.idle":"2026-09-22T05:53:06.269379Z","shell.execute_reply.started":"2026-09-22T05:53:03.560868Z","shell.execute_reply":"2026-09-22T05:53:06.268235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.270868Z","iopub.execute_input":"2026-09-22T05:53:06.271207Z","iopub.status.idle":"2026-09-22T05:53:06.308543Z","shell.execute_reply.started":"2026-09-22T05:53:06.271178Z","shell.execute_reply":"2026-09-22T05:53:06.307484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.311188Z","iopub.execute_input":"2026-09-22T05:53:06.311488Z","iopub.status.idle":"2026-09-22T05:53:06.317882Z","shell.execute_reply.started":"2026-09-22T05:53:06.311462Z","shell.execute_reply":"2026-09-22T05:53:06.317059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.318952Z","iopub.execute_input":"2026-09-22T05:53:06.319271Z","iopub.status.idle":"2026-09-22T05:53:06.36985Z","shell.execute_reply.started":"2026-09-22T05:53:06.319233Z","shell.execute_reply":"2026-09-22T05:53:06.368911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.370988Z","iopub.execute_input":"2026-09-22T05:53:06.371294Z","iopub.status.idle":"2026-09-22T05:53:06.399425Z","shell.execute_reply.started":"2026-09-22T05:53:06.37126Z","shell.execute_reply":"2026-09-22T05:53:06.398617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[22146, 1:].values.reshape(28, 28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.400552Z","iopub.execute_input":"2026-09-22T05:53:06.400902Z","iopub.status.idle":"2026-09-22T05:53:06.637622Z","shell.execute_reply.started":"2026-09-22T05:53:06.400864Z","shell.execute_reply":"2026-09-22T05:53:06.636571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.drop(columns=['label'])\ny = df['label']\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.638725Z","iopub.execute_input":"2026-09-22T05:53:06.639013Z","iopub.status.idle":"2026-09-22T05:53:06.928278Z","shell.execute_reply.started":"2026-09-22T05:53:06.638987Z","shell.execute_reply":"2026-09-22T05:53:06.927204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape, X_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.929333Z","iopub.execute_input":"2026-09-22T05:53:06.929624Z","iopub.status.idle":"2026-09-22T05:53:06.936107Z","shell.execute_reply.started":"2026-09-22T05:53:06.9296Z","shell.execute_reply":"2026-09-22T05:53:06.935014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.937577Z","iopub.execute_input":"2026-09-22T05:53:06.937966Z","iopub.status.idle":"2026-09-22T05:53:06.95779Z","shell.execute_reply.started":"2026-09-22T05:53:06.937912Z","shell.execute_reply":"2026-09-22T05:53:06.956622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:06.959176Z","iopub.execute_input":"2026-09-22T05:53:06.959681Z","iopub.status.idle":"2026-09-22T05:53:07.156594Z","shell.execute_reply.started":"2026-09-22T05:53:06.959649Z","shell.execute_reply":"2026-09-22T05:53:07.155382Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predicting without PCA on KNN takes a lot of time --- due to the no of computations/calculations\n\nimport time\nstart = time.time()\ny_pred = knn.predict(X_test)\nprint(time.time() - start) ## Time taken to store predictions on X_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:07.157608Z","iopub.execute_input":"2026-09-22T05:53:07.157895Z","iopub.status.idle":"2026-09-22T05:53:19.532988Z","shell.execute_reply.started":"2026-09-22T05:53:07.15787Z","shell.execute_reply":"2026-09-22T05:53:19.53195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# It took only 12 seconds due to kaggle's hardware being used, otherwise it could take more time","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:19.537332Z","iopub.execute_input":"2026-09-22T05:53:19.53769Z","iopub.status.idle":"2026-09-22T05:53:19.54283Z","shell.execute_reply.started":"2026-09-22T05:53:19.537659Z","shell.execute_reply":"2026-09-22T05:53:19.541475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(accuracy_score(y_test, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:19.544227Z","iopub.execute_input":"2026-09-22T05:53:19.544751Z","iopub.status.idle":"2026-09-22T05:53:19.563973Z","shell.execute_reply.started":"2026-09-22T05:53:19.544706Z","shell.execute_reply":"2026-09-22T05:53:19.562862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Accuracy = 96.4% without performing PCA on MNIST","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:19.565705Z","iopub.execute_input":"2026-09-22T05:53:19.566065Z","iopub.status.idle":"2026-09-22T05:53:19.582714Z","shell.execute_reply.started":"2026-09-22T05:53:19.566029Z","shell.execute_reply":"2026-09-22T05:53:19.581626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"### Now, i will compare this with the results obtained after performing PCA on MNIST","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:19.584012Z","iopub.execute_input":"2026-09-22T05:53:19.584406Z","iopub.status.idle":"2026-09-22T05:53:19.603177Z","shell.execute_reply.started":"2026-09-22T05:53:19.584365Z","shell.execute_reply":"2026-09-22T05:53:19.602293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1-- Standardizing(Mean Centering) the data\n\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:19.604461Z","iopub.execute_input":"2026-09-22T05:53:19.605298Z","iopub.status.idle":"2026-09-22T05:53:20.08193Z","shell.execute_reply.started":"2026-09-22T05:53:19.60526Z","shell.execute_reply":"2026-09-22T05:53:20.080913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2-- Now implementing PCA from sklearn\n\n# pca = PCA(n_components = None) # if n_components taken,\n                                # then number of components generated is same as columns in X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:20.08344Z","iopub.execute_input":"2026-09-22T05:53:20.083724Z","iopub.status.idle":"2026-09-22T05:53:20.087399Z","shell.execute_reply.started":"2026-09-22T05:53:20.083699Z","shell.execute_reply":"2026-09-22T05:53:20.086494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = 100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:20.088533Z","iopub.execute_input":"2026-09-22T05:53:20.088838Z","iopub.status.idle":"2026-09-22T05:53:20.107739Z","shell.execute_reply.started":"2026-09-22T05:53:20.088802Z","shell.execute_reply":"2026-09-22T05:53:20.106708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_transformed = pca.fit_transform(X_train_scaled)\nX_test_transformed = pca.transform(X_test_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:20.109062Z","iopub.execute_input":"2026-09-22T05:53:20.109421Z","iopub.status.idle":"2026-09-22T05:53:20.98142Z","shell.execute_reply.started":"2026-09-22T05:53:20.109381Z","shell.execute_reply":"2026-09-22T05:53:20.980717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# X_train_transformed.shape, X_test_transformed.shape # shows 100 cols, if n_components = None","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:20.982335Z","iopub.execute_input":"2026-09-22T05:53:20.982666Z","iopub.status.idle":"2026-09-22T05:53:20.987421Z","shell.execute_reply.started":"2026-09-22T05:53:20.982634Z","shell.execute_reply":"2026-09-22T05:53:20.986606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_transformed.shape, X_test_transformed.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:20.98902Z","iopub.execute_input":"2026-09-22T05:53:20.989349Z","iopub.status.idle":"2026-09-22T05:53:21.011342Z","shell.execute_reply.started":"2026-09-22T05:53:20.989318Z","shell.execute_reply":"2026-09-22T05:53:21.01057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training knn again --\n\nknn = KNeighborsClassifier()\n\nknn.fit(X_train_transformed, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:21.012698Z","iopub.execute_input":"2026-09-22T05:53:21.013042Z","iopub.status.idle":"2026-09-22T05:53:21.040895Z","shell.execute_reply.started":"2026-09-22T05:53:21.013008Z","shell.execute_reply":"2026-09-22T05:53:21.040243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_transformed)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:21.043281Z","iopub.execute_input":"2026-09-22T05:53:21.043634Z","iopub.status.idle":"2026-09-22T05:53:23.005747Z","shell.execute_reply.started":"2026-09-22T05:53:21.043602Z","shell.execute_reply":"2026-09-22T05:53:23.004659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:23.00704Z","iopub.execute_input":"2026-09-22T05:53:23.007452Z","iopub.status.idle":"2026-09-22T05:53:23.019099Z","shell.execute_reply.started":"2026-09-22T05:53:23.007412Z","shell.execute_reply":"2026-09-22T05:53:23.017782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Accuracy will change with n_components","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:23.020537Z","iopub.execute_input":"2026-09-22T05:53:23.020942Z","iopub.status.idle":"2026-09-22T05:53:23.044574Z","shell.execute_reply.started":"2026-09-22T05:53:23.020899Z","shell.execute_reply":"2026-09-22T05:53:23.043358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Running a loop to see accuracy_changes with change in n_components\n\nfor i in range(1, 785):\n\n    pca = PCA(n_components = i)\n    X_train_trf = pca.fit_transform(X_train_scaled)\n    X_test_trf = pca.transform(X_test_scaled)\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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:53:23.047302Z","iopub.execute_input":"2026-09-22T05:53:23.047701Z","iopub.status.idle":"2026-09-22T05:55:10.011879Z","shell.execute_reply.started":"2026-09-22T05:53:23.047672Z","shell.execute_reply":"2026-09-22T05:55:10.009657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Visualizing data of MNIST in 2D using PCA ---","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:17.147998Z","iopub.execute_input":"2026-09-22T05:55:17.148552Z","iopub.status.idle":"2026-09-22T05:55:17.15521Z","shell.execute_reply.started":"2026-09-22T05:55:17.148484Z","shell.execute_reply":"2026-09-22T05:55:17.153308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = 2)\n\nX_train_trf = pca.fit_transform(X_train_scaled)\nX_test_trf = pca.transform(X_test_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:17.929321Z","iopub.execute_input":"2026-09-22T05:55:17.929692Z","iopub.status.idle":"2026-09-22T05:55:19.196172Z","shell.execute_reply.started":"2026-09-22T05:55:17.929662Z","shell.execute_reply":"2026-09-22T05:55:19.195101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:21.715647Z","iopub.execute_input":"2026-09-22T05:55:21.71606Z","iopub.status.idle":"2026-09-22T05:55:21.723489Z","shell.execute_reply.started":"2026-09-22T05:55:21.716022Z","shell.execute_reply":"2026-09-22T05:55:21.722163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape, X_test_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:23.600915Z","iopub.execute_input":"2026-09-22T05:55:23.60127Z","iopub.status.idle":"2026-09-22T05:55:23.608057Z","shell.execute_reply.started":"2026-09-22T05:55:23.60124Z","shell.execute_reply":"2026-09-22T05:55:23.606713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Using plotly for dynamic visualizations, otherwise matplotlib/seaborn can also be used\n\ny_train_transformed = y_train.astype(str)\n\nfig = px.scatter(\n    x = X_train_trf[:, 0],\n    y = X_train_trf[:, 1],\n    color = y_train_transformed,\n    color_discrete_sequence = px.colors.qualitative.G10\n)\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:24.40975Z","iopub.execute_input":"2026-09-22T05:55:24.410118Z","iopub.status.idle":"2026-09-22T05:55:29.452658Z","shell.execute_reply.started":"2026-09-22T05:55:24.410088Z","shell.execute_reply":"2026-09-22T05:55:29.450646Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualizing data of MNIST in 3D using PCA ->","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:29.455907Z","iopub.execute_input":"2026-09-22T05:55:29.456295Z","iopub.status.idle":"2026-09-22T05:55:29.461882Z","shell.execute_reply.started":"2026-09-22T05:55:29.456253Z","shell.execute_reply":"2026-09-22T05:55:29.460448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = 3)\nX_train_trf = pca.fit_transform(X_train_scaled)\nX_test_trf = pca.transform(X_test_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:29.463174Z","iopub.execute_input":"2026-09-22T05:55:29.463458Z","iopub.status.idle":"2026-09-22T05:55:30.857718Z","shell.execute_reply.started":"2026-09-22T05:55:29.463432Z","shell.execute_reply":"2026-09-22T05:55:30.85658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Using plotly for 3d-dynamic visualization of data (3d scatter plot)\n\ny_train_trf = y_train.astype(str)\n\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(margin = dict(l=20, r=20, t=20, b=20))\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:30.859667Z","iopub.execute_input":"2026-09-22T05:55:30.8603Z","iopub.status.idle":"2026-09-22T05:55:31.084958Z","shell.execute_reply.started":"2026-09-22T05:55:30.860268Z","shell.execute_reply":"2026-09-22T05:55:31.082313Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ # Eigen values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:38.637756Z","iopub.execute_input":"2026-09-22T05:55:38.638332Z","iopub.status.idle":"2026-09-22T05:55:38.646591Z","shell.execute_reply.started":"2026-09-22T05:55:38.638279Z","shell.execute_reply":"2026-09-22T05:55:38.645422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape # PCA components shape [3 eigen vectors in 784-dimensional space]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:39.391965Z","iopub.execute_input":"2026-09-22T05:55:39.392299Z","iopub.status.idle":"2026-09-22T05:55:39.399853Z","shell.execute_reply.started":"2026-09-22T05:55:39.392272Z","shell.execute_reply":"2026-09-22T05:55:39.398769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:42.812099Z","iopub.execute_input":"2026-09-22T05:55:42.812428Z","iopub.status.idle":"2026-09-22T05:55:42.818522Z","shell.execute_reply.started":"2026-09-22T05:55:42.812401Z","shell.execute_reply":"2026-09-22T05:55:42.817799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:43.668483Z","iopub.execute_input":"2026-09-22T05:55:43.668898Z","iopub.status.idle":"2026-09-22T05:55:43.675818Z","shell.execute_reply.started":"2026-09-22T05:55:43.668868Z","shell.execute_reply":"2026-09-22T05:55:43.674696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = None)\n\nX_train_trf = pca.fit_transform(X_train_scaled)\nX_test_trf = pca.transform(X_test_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:46.523264Z","iopub.execute_input":"2026-09-22T05:55:46.523639Z","iopub.status.idle":"2026-09-22T05:55:49.596042Z","shell.execute_reply.started":"2026-09-22T05:55:46.523609Z","shell.execute_reply":"2026-09-22T05:55:49.595064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:55:58.668874Z","iopub.execute_input":"2026-09-22T05:55:58.669346Z","iopub.status.idle":"2026-09-22T05:55:58.678993Z","shell.execute_reply.started":"2026-09-22T05:55:58.669305Z","shell.execute_reply":"2026-09-22T05:55:58.67798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:56:07.961782Z","iopub.execute_input":"2026-09-22T05:56:07.962562Z","iopub.status.idle":"2026-09-22T05:56:07.976317Z","shell.execute_reply.started":"2026-09-22T05:56:07.962528Z","shell.execute_reply":"2026-09-22T05:56:07.975232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:56:23.642279Z","iopub.execute_input":"2026-09-22T05:56:23.642723Z","iopub.status.idle":"2026-09-22T05:56:23.655237Z","shell.execute_reply.started":"2026-09-22T05:56:23.642685Z","shell.execute_reply":"2026-09-22T05:56:23.652481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:56:31.284275Z","iopub.execute_input":"2026-09-22T05:56:31.284635Z","iopub.status.idle":"2026-09-22T05:56:31.298107Z","shell.execute_reply.started":"2026-09-22T05:56:31.284608Z","shell.execute_reply":"2026-09-22T05:56:31.297111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:56:53.593271Z","iopub.execute_input":"2026-09-22T05:56:53.593702Z","iopub.status.idle":"2026-09-22T05:56:53.605971Z","shell.execute_reply.started":"2026-09-22T05:56:53.59367Z","shell.execute_reply":"2026-09-22T05:56:53.605245Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T05:57:16.577704Z","iopub.execute_input":"2026-09-22T05:57:16.578047Z","iopub.status.idle":"2026-09-22T05:57:16.723968Z","shell.execute_reply.started":"2026-09-22T05:57:16.57802Z","shell.execute_reply":"2026-09-22T05:57:16.722971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 90 % variance is explained at around 250 components so we can take that for good results!","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T06:03:49.024945Z","iopub.execute_input":"2026-09-22T06:03:49.025279Z","iopub.status.idle":"2026-09-22T06:03:49.030126Z","shell.execute_reply.started":"2026-09-22T06:03:49.025251Z","shell.execute_reply":"2026-09-22T06:03:49.029194Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# done :)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T06:03:58.144463Z","iopub.execute_input":"2026-09-22T06:03:58.144853Z","iopub.status.idle":"2026-09-22T06:03:58.149196Z","shell.execute_reply.started":"2026-09-22T06:03:58.144822Z","shell.execute_reply":"2026-09-22T06:03:58.14806Z"}},"outputs":[],"execution_count":null}]}