{"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":30698,"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-04T03:54:26.917099Z","iopub.execute_input":"2024-07-04T03:54:26.917476Z","iopub.status.idle":"2024-07-04T03:54:28.284930Z","shell.execute_reply.started":"2024-07-04T03:54:26.917447Z","shell.execute_reply":"2024-07-04T03:54:28.283558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:28.287784Z","iopub.execute_input":"2024-07-04T03:54:28.288461Z","iopub.status.idle":"2024-07-04T03:54:33.038898Z","shell.execute_reply.started":"2024-07-04T03:54:28.288417Z","shell.execute_reply":"2024-07-04T03:54:33.037672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.value_counts().sum()","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:33.040371Z","iopub.execute_input":"2024-07-04T03:54:33.040709Z","iopub.status.idle":"2024-07-04T03:54:35.068656Z","shell.execute_reply.started":"2024-07-04T03:54:33.040680Z","shell.execute_reply":"2024-07-04T03:54:35.067255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df.iloc[4004,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:35.070463Z","iopub.execute_input":"2024-07-04T03:54:35.070939Z","iopub.status.idle":"2024-07-04T03:54:35.334668Z","shell.execute_reply.started":"2024-07-04T03:54:35.070870Z","shell.execute_reply":"2024-07-04T03:54:35.333584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:35.339841Z","iopub.execute_input":"2024-07-04T03:54:35.340287Z","iopub.status.idle":"2024-07-04T03:54:35.346633Z","shell.execute_reply.started":"2024-07-04T03:54:35.340252Z","shell.execute_reply":"2024-07-04T03:54:35.345304Z"},"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-04T03:54:35.348340Z","iopub.execute_input":"2024-07-04T03:54:35.348800Z","iopub.status.idle":"2024-07-04T03:54:36.349628Z","shell.execute_reply.started":"2024-07-04T03:54:35.348756Z","shell.execute_reply":"2024-07-04T03:54:36.348582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:36.351010Z","iopub.execute_input":"2024-07-04T03:54:36.351398Z","iopub.status.idle":"2024-07-04T03:54:36.358865Z","shell.execute_reply.started":"2024-07-04T03:54:36.351369Z","shell.execute_reply":"2024-07-04T03:54:36.357775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:36.360309Z","iopub.execute_input":"2024-07-04T03:54:36.360656Z","iopub.status.idle":"2024-07-04T03:54:36.551723Z","shell.execute_reply.started":"2024-07-04T03:54:36.360628Z","shell.execute_reply":"2024-07-04T03:54:36.550626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:36.553001Z","iopub.execute_input":"2024-07-04T03:54:36.553385Z","iopub.status.idle":"2024-07-04T03:54:36.710545Z","shell.execute_reply.started":"2024-07-04T03:54:36.553352Z","shell.execute_reply":"2024-07-04T03:54:36.709254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=knn.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:36.712541Z","iopub.execute_input":"2024-07-04T03:54:36.713010Z","iopub.status.idle":"2024-07-04T03:54:50.366642Z","shell.execute_reply.started":"2024-07-04T03:54:36.712968Z","shell.execute_reply":"2024-07-04T03:54:50.365264Z"},"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-04T03:54:50.368521Z","iopub.execute_input":"2024-07-04T03:54:50.368981Z","iopub.status.idle":"2024-07-04T03:54:50.381822Z","shell.execute_reply.started":"2024-07-04T03:54:50.368939Z","shell.execute_reply":"2024-07-04T03:54:50.380239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:50.384046Z","iopub.execute_input":"2024-07-04T03:54:50.384531Z","iopub.status.idle":"2024-07-04T03:54:50.391777Z","shell.execute_reply.started":"2024-07-04T03:54:50.384492Z","shell.execute_reply":"2024-07-04T03:54:50.389879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = scaler.fit_transform(X_train)\nX_test=scaler.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:50.393164Z","iopub.execute_input":"2024-07-04T03:54:50.393945Z","iopub.status.idle":"2024-07-04T03:54:51.051577Z","shell.execute_reply.started":"2024-07-04T03:54:50.393901Z","shell.execute_reply":"2024-07-04T03:54:51.050295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca =PCA(n_components=200)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:51.059430Z","iopub.execute_input":"2024-07-04T03:54:51.059844Z","iopub.status.idle":"2024-07-04T03:54:51.066405Z","shell.execute_reply.started":"2024-07-04T03:54:51.059810Z","shell.execute_reply":"2024-07-04T03:54:51.064251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X_train)\nX_test_trf=pca.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:54:51.068380Z","iopub.execute_input":"2024-07-04T03:54:51.068774Z","iopub.status.idle":"2024-07-04T03:55:01.921815Z","shell.execute_reply.started":"2024-07-04T03:54:51.068741Z","shell.execute_reply":"2024-07-04T03:55:01.920031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:55:01.923971Z","iopub.execute_input":"2024-07-04T03:55:01.925219Z","iopub.status.idle":"2024-07-04T03:55:01.938487Z","shell.execute_reply.started":"2024-07-04T03:55:01.925159Z","shell.execute_reply":"2024-07-04T03:55:01.935812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn1=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:55:01.942160Z","iopub.execute_input":"2024-07-04T03:55:01.944294Z","iopub.status.idle":"2024-07-04T03:55:01.951790Z","shell.execute_reply.started":"2024-07-04T03:55:01.944223Z","shell.execute_reply":"2024-07-04T03:55:01.950484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn1.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:55:01.955127Z","iopub.execute_input":"2024-07-04T03:55:01.956338Z","iopub.status.idle":"2024-07-04T03:55:02.000511Z","shell.execute_reply.started":"2024-07-04T03:55:01.956288Z","shell.execute_reply":"2024-07-04T03:55:01.998521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn1.predict(X_test_trf)\naccuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:55:02.002523Z","iopub.execute_input":"2024-07-04T03:55:02.002911Z","iopub.status.idle":"2024-07-04T03:55:05.543985Z","shell.execute_reply.started":"2024-07-04T03:55:02.002878Z","shell.execute_reply":"2024-07-04T03:55:05.542709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1,785,5):\n    pca=PCA(n_components=i)\n    X_train_trf = pca.fit_transform(X_train)\n    X_test_trf=pca.fit_transform(X_test)\n    knn1=KNeighborsClassifier()\n    knn1.fit(X_train_trf,y_train)\n    y_pred = knn1.predict(X_test_trf)\n    print(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:55:05.545479Z","iopub.execute_input":"2024-07-04T03:55:05.545851Z","iopub.status.idle":"2024-07-04T03:58:21.376013Z","shell.execute_reply.started":"2024-07-04T03:55:05.545819Z","shell.execute_reply":"2024-07-04T03:58:21.374318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=2)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:58:31.623331Z","iopub.execute_input":"2024-07-04T03:58:31.623743Z","iopub.status.idle":"2024-07-04T03:58:33.647513Z","shell.execute_reply.started":"2024-07-04T03:58:31.623712Z","shell.execute_reply":"2024-07-04T03:58:33.645661Z"},"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],y=X_train_trf[:,1],color=y_train_trf,color_discrete_sequence=px.colors.qualitative.G10)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:58:37.167860Z","iopub.execute_input":"2024-07-04T03:58:37.168428Z","iopub.status.idle":"2024-07-04T03:58:40.361718Z","shell.execute_reply.started":"2024-07-04T03:58:37.168391Z","shell.execute_reply":"2024-07-04T03:58:40.360406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:58:46.628193Z","iopub.execute_input":"2024-07-04T03:58:46.628608Z","iopub.status.idle":"2024-07-04T03:58:46.637334Z","shell.execute_reply.started":"2024-07-04T03:58:46.628576Z","shell.execute_reply":"2024-07-04T03:58:46.635912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=3)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:58:56.188785Z","iopub.execute_input":"2024-07-04T03:58:56.189568Z","iopub.status.idle":"2024-07-04T03:58:57.577784Z","shell.execute_reply.started":"2024-07-04T03:58:56.189531Z","shell.execute_reply":"2024-07-04T03:58:57.575636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:02.923189Z","iopub.execute_input":"2024-07-04T03:59:02.923590Z","iopub.status.idle":"2024-07-04T03:59:02.934098Z","shell.execute_reply.started":"2024-07-04T03:59:02.923562Z","shell.execute_reply":"2024-07-04T03:59:02.932478Z"},"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(\nmargin=dict(l=20,r=20,t=20,b=20)\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:07.948143Z","iopub.execute_input":"2024-07-04T03:59:07.948552Z","iopub.status.idle":"2024-07-04T03:59:08.206126Z","shell.execute_reply.started":"2024-07-04T03:59:07.948521Z","shell.execute_reply":"2024-07-04T03:59:08.204590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n#eigen_value","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:16.307686Z","iopub.execute_input":"2024-07-04T03:59:16.308118Z","iopub.status.idle":"2024-07-04T03:59:16.316707Z","shell.execute_reply.started":"2024-07-04T03:59:16.308058Z","shell.execute_reply":"2024-07-04T03:59:16.315150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:21.598340Z","iopub.execute_input":"2024-07-04T03:59:21.598766Z","iopub.status.idle":"2024-07-04T03:59:21.607021Z","shell.execute_reply.started":"2024-07-04T03:59:21.598731Z","shell.execute_reply":"2024-07-04T03:59:21.605448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"{(pca.explained_variance_ratio_)*100}%\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:24.757979Z","iopub.execute_input":"2024-07-04T03:59:24.758428Z","iopub.status.idle":"2024-07-04T03:59:24.765785Z","shell.execute_reply.started":"2024-07-04T03:59:24.758372Z","shell.execute_reply":"2024-07-04T03:59:24.764562Z"},"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.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:29.408661Z","iopub.execute_input":"2024-07-04T03:59:29.409044Z","iopub.status.idle":"2024-07-04T03:59:35.053253Z","shell.execute_reply.started":"2024-07-04T03:59:29.409016Z","shell.execute_reply":"2024-07-04T03:59:35.051529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:35.898793Z","iopub.execute_input":"2024-07-04T03:59:35.899227Z","iopub.status.idle":"2024-07-04T03:59:35.906859Z","shell.execute_reply.started":"2024-07-04T03:59:35.899192Z","shell.execute_reply":"2024-07-04T03:59:35.905512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:37.763951Z","iopub.execute_input":"2024-07-04T03:59:37.764398Z","iopub.status.idle":"2024-07-04T03:59:37.772892Z","shell.execute_reply.started":"2024-07-04T03:59:37.764364Z","shell.execute_reply":"2024-07-04T03:59:37.771408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T03:59:39.309062Z","iopub.execute_input":"2024-07-04T03:59:39.309492Z","iopub.status.idle":"2024-07-04T03:59:39.327529Z","shell.execute_reply.started":"2024-07-04T03:59:39.309460Z","shell.execute_reply":"2024-07-04T03:59:39.326112Z"},"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-04T03:59:44.473810Z","iopub.execute_input":"2024-07-04T03:59:44.474252Z","iopub.status.idle":"2024-07-04T03:59:44.781830Z","shell.execute_reply.started":"2024-07-04T03:59:44.474217Z","shell.execute_reply":"2024-07-04T03:59:44.780597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# somewhere around n_component = (200 to 300) in 90% range","metadata":{}}]}