{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{"_uuid":"363da1ac-34e1-4a28-8fd6-69da9f21233a","_cell_guid":"fb347ef6-2f37-42a7-9935-53da86e0181e","trusted":true}},{"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":"64d1cbde-32c4-43bf-845a-6b04172eea48","_cell_guid":"8b1bb48b-607d-4c1e-906a-1297775ade48","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:43.568105Z","iopub.execute_input":"2023-09-08T11:29:43.568474Z","iopub.status.idle":"2023-09-08T11:29:43.583308Z","shell.execute_reply.started":"2023-09-08T11:29:43.568442Z","shell.execute_reply":"2023-09-08T11:29:43.581954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"_uuid":"30c80bc5-d053-4540-a872-91983d02f78c","_cell_guid":"94a0054e-40e7-4ca0-82b9-2d8fcca78644","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:43.585003Z","iopub.execute_input":"2023-09-08T11:29:43.585775Z","iopub.status.idle":"2023-09-08T11:29:46.038593Z","shell.execute_reply.started":"2023-09-08T11:29:43.585722Z","shell.execute_reply":"2023-09-08T11:29:46.037497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"_uuid":"c473c92f-270c-45e4-b3f6-94591a5d594c","_cell_guid":"06a8be2e-aa8b-4414-8b7b-79b3d6f6ebed","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.040719Z","iopub.execute_input":"2023-09-08T11:29:46.041082Z","iopub.status.idle":"2023-09-08T11:29:46.047165Z","shell.execute_reply.started":"2023-09-08T11:29:46.041044Z","shell.execute_reply":"2023-09-08T11:29:46.046038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"_uuid":"fa127afa-4778-4b2b-a4e6-d2df8aa9a093","_cell_guid":"7dc3a86b-58f3-4ce7-9f58-29baf90eee7f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.048991Z","iopub.execute_input":"2023-09-08T11:29:46.049313Z","iopub.status.idle":"2023-09-08T11:29:46.075825Z","shell.execute_reply.started":"2023-09-08T11:29:46.049280Z","shell.execute_reply":"2023-09-08T11:29:46.074741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"_uuid":"79659a34-5a72-40e8-abb3-57a21b6dbb45","_cell_guid":"54cba4cd-d75a-46c1-8654-7e55f84cde9c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.077222Z","iopub.execute_input":"2023-09-08T11:29:46.077538Z","iopub.status.idle":"2023-09-08T11:29:46.081932Z","shell.execute_reply.started":"2023-09-08T11:29:46.077505Z","shell.execute_reply":"2023-09-08T11:29:46.080810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))","metadata":{"_uuid":"8cdc6c9d-4f2b-45d3-8cee-b12916a5ea04","_cell_guid":"4fe4ffc3-2bdd-4daa-8b81-283789688721","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.083565Z","iopub.execute_input":"2023-09-08T11:29:46.084017Z","iopub.status.idle":"2023-09-08T11:29:46.222388Z","shell.execute_reply.started":"2023-09-08T11:29:46.083969Z","shell.execute_reply":"2023-09-08T11:29:46.221395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"_uuid":"1755794d-b44d-4737-8321-82a4dd80df58","_cell_guid":"e7444126-9b87-46d9-a516-99ab9a268a14","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.223681Z","iopub.execute_input":"2023-09-08T11:29:46.223983Z","iopub.status.idle":"2023-09-08T11:29:46.230133Z","shell.execute_reply.started":"2023-09-08T11:29:46.223952Z","shell.execute_reply":"2023-09-08T11:29:46.229183Z"},"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":{"_uuid":"69da4310-305e-42e9-adda-2ae89fd83360","_cell_guid":"a17a3106-30da-4706-82ec-6f549545e8ef","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.231474Z","iopub.execute_input":"2023-09-08T11:29:46.231817Z","iopub.status.idle":"2023-09-08T11:29:46.507812Z","shell.execute_reply.started":"2023-09-08T11:29:46.231784Z","shell.execute_reply":"2023-09-08T11:29:46.506627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"_uuid":"04c50cf2-d6f6-4e85-a38e-a643aae72674","_cell_guid":"e434e2e3-21a8-44d4-ba78-92d5d380c40d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.510374Z","iopub.execute_input":"2023-09-08T11:29:46.510708Z","iopub.status.idle":"2023-09-08T11:29:46.516363Z","shell.execute_reply.started":"2023-09-08T11:29:46.510669Z","shell.execute_reply":"2023-09-08T11:29:46.515227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"_uuid":"fce4224f-ba2d-44bf-b676-029192fbc027","_cell_guid":"cc3d2c8d-6acf-4f1c-bf3d-7e4d361a0aa1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.518861Z","iopub.execute_input":"2023-09-08T11:29:46.519193Z","iopub.status.idle":"2023-09-08T11:29:46.528536Z","shell.execute_reply.started":"2023-09-08T11:29:46.519130Z","shell.execute_reply":"2023-09-08T11:29:46.527545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"_uuid":"aa7cd8ec-8b50-4ea1-8010-91027167edbe","_cell_guid":"0fbfe657-a323-4b94-b455-dbdadd5480ce","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.529964Z","iopub.execute_input":"2023-09-08T11:29:46.530365Z","iopub.status.idle":"2023-09-08T11:29:46.541771Z","shell.execute_reply.started":"2023-09-08T11:29:46.530322Z","shell.execute_reply":"2023-09-08T11:29:46.540733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"_uuid":"94a514a7-dec0-47ef-be74-38e013bf8c13","_cell_guid":"03a8241d-0833-4f7d-a1b7-f732cbb91199","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.543426Z","iopub.execute_input":"2023-09-08T11:29:46.543958Z","iopub.status.idle":"2023-09-08T11:29:46.569052Z","shell.execute_reply.started":"2023-09-08T11:29:46.543888Z","shell.execute_reply":"2023-09-08T11:29:46.568076Z"},"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":{"_uuid":"7c93090e-a464-4389-89e2-706c0eca8cc7","_cell_guid":"c1ca8591-2c01-4095-96d0-be660f51fd8f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:46.570431Z","iopub.execute_input":"2023-09-08T11:29:46.570804Z","iopub.status.idle":"2023-09-08T11:29:58.904537Z","shell.execute_reply.started":"2023-09-08T11:29:46.570767Z","shell.execute_reply":"2023-09-08T11:29:58.903454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"_uuid":"66b81527-a3a1-44c9-98f6-726318899343","_cell_guid":"222ea4cb-c30a-44a1-92a7-932b39a51cdc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:58.905941Z","iopub.execute_input":"2023-09-08T11:29:58.906259Z","iopub.status.idle":"2023-09-08T11:29:58.913367Z","shell.execute_reply.started":"2023-09-08T11:29:58.906225Z","shell.execute_reply":"2023-09-08T11:29:58.912563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"_uuid":"f6ecee5a-a40b-47ad-9c4f-c00279ee8fef","_cell_guid":"2b1aad4b-369c-4ad5-bcea-36cb1bc51bb3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:58.914551Z","iopub.execute_input":"2023-09-08T11:29:58.915067Z","iopub.status.idle":"2023-09-08T11:29:58.924277Z","shell.execute_reply.started":"2023-09-08T11:29:58.915024Z","shell.execute_reply":"2023-09-08T11:29:58.923092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"_uuid":"09920847-1c6d-40aa-bb1b-6fb1f9b0d36b","_cell_guid":"bdf60ed2-090c-42df-9e46-684d6fef06fa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:58.925746Z","iopub.execute_input":"2023-09-08T11:29:58.926146Z","iopub.status.idle":"2023-09-08T11:29:59.633558Z","shell.execute_reply.started":"2023-09-08T11:29:58.926109Z","shell.execute_reply":"2023-09-08T11:29:59.632427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA \nfrom sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"_uuid":"8eaba932-45d7-4618-b560-27bfd171cfcc","_cell_guid":"ee5afef7-eddc-4a29-9e65-87a8265d92cc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:59.635173Z","iopub.execute_input":"2023-09-08T11:29:59.635635Z","iopub.status.idle":"2023-09-08T11:29:59.639918Z","shell.execute_reply.started":"2023-09-08T11:29:59.635562Z","shell.execute_reply":"2023-09-08T11:29:59.638690Z"},"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":{"_uuid":"2cccb772-5170-46fa-9926-0f34b30f6914","_cell_guid":"1a42cd03-8260-44c3-aa0d-abae151ec90b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:29:59.641935Z","iopub.execute_input":"2023-09-08T11:29:59.642400Z","iopub.status.idle":"2023-09-08T11:30:05.093170Z","shell.execute_reply.started":"2023-09-08T11:29:59.642352Z","shell.execute_reply":"2023-09-08T11:30:05.091769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"_uuid":"30d6ec99-9558-480b-9307-b88bb873df94","_cell_guid":"21a01ca7-f3c2-4651-a151-ac1a3efe77c1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:05.095161Z","iopub.execute_input":"2023-09-08T11:30:05.095661Z","iopub.status.idle":"2023-09-08T11:30:05.106400Z","shell.execute_reply.started":"2023-09-08T11:30:05.095594Z","shell.execute_reply":"2023-09-08T11:30:05.105190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"_uuid":"2a86f404-b645-4c9a-abbb-9f306f7a8f18","_cell_guid":"4f6e24d2-66f5-41c3-9f6b-65d78d64b612","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:05.108591Z","iopub.execute_input":"2023-09-08T11:30:05.109142Z","iopub.status.idle":"2023-09-08T11:30:05.127485Z","shell.execute_reply.started":"2023-09-08T11:30:05.109091Z","shell.execute_reply":"2023-09-08T11:30:05.126041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"_uuid":"4e961148-5b73-4a8f-aafd-f6c8cec06ba8","_cell_guid":"4dd9396f-340e-4ccd-8d33-0857c18c5493","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:05.129528Z","iopub.execute_input":"2023-09-08T11:30:05.130018Z","iopub.status.idle":"2023-09-08T11:30:05.174868Z","shell.execute_reply.started":"2023-09-08T11:30:05.129967Z","shell.execute_reply":"2023-09-08T11:30:05.173373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"_uuid":"619a96b8-8351-4fda-88f3-93611ce77e81","_cell_guid":"cd1c84a9-87ff-43e5-b4ff-788d69fca38c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:05.176880Z","iopub.execute_input":"2023-09-08T11:30:05.179025Z","iopub.status.idle":"2023-09-08T11:30:12.846484Z","shell.execute_reply.started":"2023-09-08T11:30:05.178969Z","shell.execute_reply":"2023-09-08T11:30:12.845601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"_uuid":"414f08ee-145e-41ce-bc07-7ddd58d7aa80","_cell_guid":"b75df2d3-741d-4d95-8486-5eefe013987f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:12.847547Z","iopub.execute_input":"2023-09-08T11:30:12.847996Z","iopub.status.idle":"2023-09-08T11:30:12.853605Z","shell.execute_reply.started":"2023-09-08T11:30:12.847963Z","shell.execute_reply":"2023-09-08T11:30:12.852865Z"},"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":{"_uuid":"719aae4c-3475-4d53-bbff-a3d125329dc2","_cell_guid":"69c7ed83-b071-41ef-a54e-0033ee077dce","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:12.854670Z","iopub.execute_input":"2023-09-08T11:30:12.855155Z","iopub.status.idle":"2023-09-08T11:30:27.011340Z","shell.execute_reply.started":"2023-09-08T11:30:12.855120Z","shell.execute_reply":"2023-09-08T11:30:27.008274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming to a 2D coordinate system\npca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"_uuid":"3717fab4-ae0c-42b7-b1b2-15a0923d7e8a","_cell_guid":"523981af-ac5e-44df-b601-ab7650a49a4d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:30.332728Z","iopub.execute_input":"2023-09-08T11:30:30.333270Z","iopub.status.idle":"2023-09-08T11:30:32.900481Z","shell.execute_reply.started":"2023-09-08T11:30:30.333233Z","shell.execute_reply":"2023-09-08T11:30:32.899493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"_uuid":"7c1a840f-8251-4b72-9338-365e34385c0f","_cell_guid":"21a9b2a2-3f88-4d2f-8713-d7f4d1c6387b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:32.902646Z","iopub.execute_input":"2023-09-08T11:30:32.903123Z","iopub.status.idle":"2023-09-08T11:30:32.911890Z","shell.execute_reply.started":"2023-09-08T11:30:32.903074Z","shell.execute_reply":"2023-09-08T11:30:32.910592Z"},"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":{"_uuid":"4d5a9190-cdab-4270-b7d8-ee9ef22303ab","_cell_guid":"8d63fd33-9ef4-4da4-bdc2-61efd99d4c83","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:32.913884Z","iopub.execute_input":"2023-09-08T11:30:32.914415Z","iopub.status.idle":"2023-09-08T11:30:33.294338Z","shell.execute_reply.started":"2023-09-08T11:30:32.914364Z","shell.execute_reply":"2023-09-08T11:30:33.293073Z"},"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":{"_uuid":"f95e16d4-86fa-43ff-a17b-3644fa82c81b","_cell_guid":"b488b77a-d52b-4933-800d-950345bca79d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:33.295951Z","iopub.execute_input":"2023-09-08T11:30:33.296406Z","iopub.status.idle":"2023-09-08T11:30:35.904459Z","shell.execute_reply.started":"2023-09-08T11:30:33.296369Z","shell.execute_reply":"2023-09-08T11:30:35.903446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"_uuid":"99d96a47-1a61-48ce-aa57-9984021d3d10","_cell_guid":"e6b971b9-1a57-437d-8594-b16de79d3392","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:35.906469Z","iopub.execute_input":"2023-09-08T11:30:35.906942Z","iopub.status.idle":"2023-09-08T11:30:35.915549Z","shell.execute_reply.started":"2023-09-08T11:30:35.906895Z","shell.execute_reply":"2023-09-08T11:30:35.913674Z"},"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_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    paper_bgcolor=\"LightSteelBlue\",\n)\nfig.show()","metadata":{"_uuid":"3f34b6e0-c860-4617-b74d-359e01296001","_cell_guid":"05875dc5-0a31-435f-b18c-3101e09125fc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:35.917568Z","iopub.execute_input":"2023-09-08T11:30:35.918000Z","iopub.status.idle":"2023-09-08T11:30:36.328599Z","shell.execute_reply.started":"2023-09-08T11:30:35.917954Z","shell.execute_reply":"2023-09-08T11:30:36.327770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n# Eigen values","metadata":{"_uuid":"482eff6b-c1c2-41fc-b570-f69abb560cb8","_cell_guid":"3567311e-5ccf-4b41-b3ba-fcee7d4bd0b6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:36.329895Z","iopub.execute_input":"2023-09-08T11:30:36.330353Z","iopub.status.idle":"2023-09-08T11:30:36.335534Z","shell.execute_reply.started":"2023-09-08T11:30:36.330303Z","shell.execute_reply":"2023-09-08T11:30:36.334772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"_uuid":"0b9db356-75f9-4be9-96b4-fff2d72f9c70","_cell_guid":"321de07f-9cbc-4517-90a7-b1d93bed5f08","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:36.337665Z","iopub.execute_input":"2023-09-08T11:30:36.338176Z","iopub.status.idle":"2023-09-08T11:30:36.348059Z","shell.execute_reply.started":"2023-09-08T11:30:36.338130Z","shell.execute_reply":"2023-09-08T11:30:36.347167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"_uuid":"87b3a005-1662-4b60-a9d7-246058668d11","_cell_guid":"dce81d85-7cfe-46f7-9b15-58b234444856","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:36.349628Z","iopub.execute_input":"2023-09-08T11:30:36.350001Z","iopub.status.idle":"2023-09-08T11:30:36.363018Z","shell.execute_reply.started":"2023-09-08T11:30:36.349964Z","shell.execute_reply":"2023-09-08T11:30:36.362086Z"},"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":{"_uuid":"ff72edc6-0df5-45d7-832c-5ec3ec8aff50","_cell_guid":"6b205717-86c7-48df-9ef9-0fd75841fe7e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:36.364319Z","iopub.execute_input":"2023-09-08T11:30:36.364874Z","iopub.status.idle":"2023-09-08T11:30:40.294834Z","shell.execute_reply.started":"2023-09-08T11:30:36.364824Z","shell.execute_reply":"2023-09-08T11:30:40.293627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"_uuid":"3cf54d2e-dda7-488c-bb46-90e9e0717ba2","_cell_guid":"0361833a-dd3a-4101-91a3-f2b62a7d9bd4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:40.300237Z","iopub.execute_input":"2023-09-08T11:30:40.303272Z","iopub.status.idle":"2023-09-08T11:30:40.314566Z","shell.execute_reply.started":"2023-09-08T11:30:40.303189Z","shell.execute_reply":"2023-09-08T11:30:40.313430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"_uuid":"1f07d5b4-2425-4bb6-a492-cd46c3cd8628","_cell_guid":"1c028da7-62b9-42d5-86f3-4c4a285644d4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:40.319688Z","iopub.execute_input":"2023-09-08T11:30:40.322805Z","iopub.status.idle":"2023-09-08T11:30:40.335014Z","shell.execute_reply.started":"2023-09-08T11:30:40.322730Z","shell.execute_reply":"2023-09-08T11:30:40.333913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"_uuid":"5013384b-2192-4ca0-9f8d-31abfe2cb0ec","_cell_guid":"893c9aef-984c-48a1-a7c7-c14832ce51df","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:40.339257Z","iopub.execute_input":"2023-09-08T11:30:40.343479Z","iopub.status.idle":"2023-09-08T11:30:40.371140Z","shell.execute_reply.started":"2023-09-08T11:30:40.343407Z","shell.execute_reply":"2023-09-08T11:30:40.369979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"_uuid":"eb84f4bc-c904-4057-81ae-a5d8831008cf","_cell_guid":"467d8082-6418-441b-8186-26f401677392","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:30:40.376466Z","iopub.execute_input":"2023-09-08T11:30:40.379250Z","iopub.status.idle":"2023-09-08T11:30:40.538655Z","shell.execute_reply.started":"2023-09-08T11:30:40.379182Z","shell.execute_reply":"2023-09-08T11:30:40.537451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"fa0753db-68fa-472e-9f76-dd6e8e8a71f2","_cell_guid":"a9df1075-e0cf-440c-8706-b8b20e645140","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-08T11:31:30.101401Z","iopub.execute_input":"2023-09-08T11:31:30.101789Z","iopub.status.idle":"2023-09-08T11:31:32.500000Z","shell.execute_reply.started":"2023-09-08T11:31:30.101749Z","shell.execute_reply":"2023-09-08T11:31:32.499050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"6438b31a-d595-466b-9c2d-b855ace4d0b0","_cell_guid":"9e99941e-f5f7-41a4-b634-4adebc5ab20e","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}