{"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"}},"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-05T07:33:26.024539Z","iopub.execute_input":"2023-09-05T07:33:26.025398Z","iopub.status.idle":"2023-09-05T07:33:26.453144Z","shell.execute_reply.started":"2023-09-05T07:33:26.025354Z","shell.execute_reply":"2023-09-05T07:33:26.451852Z"},"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-05T07:33:32.508504Z","iopub.execute_input":"2023-09-05T07:33:32.509061Z","iopub.status.idle":"2023-09-05T07:33:36.943136Z","shell.execute_reply.started":"2023-09-05T07:33:32.509025Z","shell.execute_reply":"2023-09-05T07:33:36.942036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:33:52.459973Z","iopub.execute_input":"2023-09-05T07:33:52.460366Z","iopub.status.idle":"2023-09-05T07:33:52.468882Z","shell.execute_reply.started":"2023-09-05T07:33:52.460335Z","shell.execute_reply":"2023-09-05T07:33:52.467656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:33:54.743697Z","iopub.execute_input":"2023-09-05T07:33:54.744119Z","iopub.status.idle":"2023-09-05T07:33:54.812009Z","shell.execute_reply.started":"2023-09-05T07:33:54.744089Z","shell.execute_reply":"2023-09-05T07:33:54.810839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:35:47.453166Z","iopub.execute_input":"2023-09-05T07:35:47.453649Z","iopub.status.idle":"2023-09-05T07:35:47.478963Z","shell.execute_reply.started":"2023-09-05T07:35:47.453578Z","shell.execute_reply":"2023-09-05T07:35:47.477759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample(8)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:35:50.606462Z","iopub.execute_input":"2023-09-05T07:35:50.607169Z","iopub.status.idle":"2023-09-05T07:35:50.636843Z","shell.execute_reply.started":"2023-09-05T07:35:50.607109Z","shell.execute_reply":"2023-09-05T07:35:50.635106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly as pl","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:35:53.187863Z","iopub.execute_input":"2023-09-05T07:35:53.188313Z","iopub.status.idle":"2023-09-05T07:35:54.224864Z","shell.execute_reply.started":"2023-09-05T07:35:53.188278Z","shell.execute_reply":"2023-09-05T07:35:54.222462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[40257,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:35:56.287202Z","iopub.execute_input":"2023-09-05T07:35:56.287741Z","iopub.status.idle":"2023-09-05T07:35:56.646164Z","shell.execute_reply.started":"2023-09-05T07:35:56.287704Z","shell.execute_reply":"2023-09-05T07:35:56.644889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:35:59.495190Z","iopub.execute_input":"2023-09-05T07:35:59.495717Z","iopub.status.idle":"2023-09-05T07:35:59.503969Z","shell.execute_reply.started":"2023-09-05T07:35:59.495679Z","shell.execute_reply":"2023-09-05T07:35:59.501936Z"},"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":"2023-09-05T07:36:01.491686Z","iopub.execute_input":"2023-09-05T07:36:01.492094Z","iopub.status.idle":"2023-09-05T07:36:02.063857Z","shell.execute_reply.started":"2023-09-05T07:36:01.492064Z","shell.execute_reply":"2023-09-05T07:36:02.062159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:04.186151Z","iopub.execute_input":"2023-09-05T07:36:04.186622Z","iopub.status.idle":"2023-09-05T07:36:04.195167Z","shell.execute_reply.started":"2023-09-05T07:36:04.186566Z","shell.execute_reply":"2023-09-05T07:36:04.193344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:06.694547Z","iopub.execute_input":"2023-09-05T07:36:06.695179Z","iopub.status.idle":"2023-09-05T07:36:06.702712Z","shell.execute_reply.started":"2023-09-05T07:36:06.695141Z","shell.execute_reply":"2023-09-05T07:36:06.700972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nKNN=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:22.786575Z","iopub.execute_input":"2023-09-05T07:36:22.787752Z","iopub.status.idle":"2023-09-05T07:36:23.071634Z","shell.execute_reply.started":"2023-09-05T07:36:22.787698Z","shell.execute_reply":"2023-09-05T07:36:23.070083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"KNN.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:26.387557Z","iopub.execute_input":"2023-09-05T07:36:26.388074Z","iopub.status.idle":"2023-09-05T07:36:26.591670Z","shell.execute_reply.started":"2023-09-05T07:36:26.388034Z","shell.execute_reply":"2023-09-05T07:36:26.589709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=KNN.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:27.986351Z","iopub.execute_input":"2023-09-05T07:36:27.986811Z","iopub.status.idle":"2023-09-05T07:36:42.687126Z","shell.execute_reply.started":"2023-09-05T07:36:27.986779Z","shell.execute_reply":"2023-09-05T07:36:42.685352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:46.024897Z","iopub.execute_input":"2023-09-05T07:36:46.025350Z","iopub.status.idle":"2023-09-05T07:36:46.033700Z","shell.execute_reply.started":"2023-09-05T07:36:46.025317Z","shell.execute_reply":"2023-09-05T07:36:46.031240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_pred,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:47.900572Z","iopub.execute_input":"2023-09-05T07:36:47.901281Z","iopub.status.idle":"2023-09-05T07:36:47.912034Z","shell.execute_reply.started":"2023-09-05T07:36:47.901184Z","shell.execute_reply":"2023-09-05T07:36:47.910896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA \n\nfrom sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:52.586480Z","iopub.execute_input":"2023-09-05T07:36:52.587380Z","iopub.status.idle":"2023-09-05T07:36:52.593218Z","shell.execute_reply.started":"2023-09-05T07:36:52.587340Z","shell.execute_reply":"2023-09-05T07:36:52.591271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=200)\n\n# these are the components that are we want....like no. of columns we want","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:36:58.925415Z","iopub.execute_input":"2023-09-05T07:36:58.926297Z","iopub.status.idle":"2023-09-05T07:36:58.930776Z","shell.execute_reply.started":"2023-09-05T07:36:58.926259Z","shell.execute_reply":"2023-09-05T07:36:58.929867Z"},"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":"2023-09-05T07:37:17.103945Z","iopub.execute_input":"2023-09-05T07:37:17.105411Z","iopub.status.idle":"2023-09-05T07:37:17.812080Z","shell.execute_reply.started":"2023-09-05T07:37:17.105350Z","shell.execute_reply":"2023-09-05T07:37:17.810704Z"},"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":"2023-09-05T07:37:59.789205Z","iopub.execute_input":"2023-09-05T07:37:59.789744Z","iopub.status.idle":"2023-09-05T07:38:05.802386Z","shell.execute_reply.started":"2023-09-05T07:37:59.789705Z","shell.execute_reply":"2023-09-05T07:38:05.800714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"KNN.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:38:36.486257Z","iopub.execute_input":"2023-09-05T07:38:36.486679Z","iopub.status.idle":"2023-09-05T07:38:36.525711Z","shell.execute_reply.started":"2023-09-05T07:38:36.486644Z","shell.execute_reply":"2023-09-05T07:38:36.524529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_trf=KNN.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:39:29.186073Z","iopub.execute_input":"2023-09-05T07:39:29.186485Z","iopub.status.idle":"2023-09-05T07:39:32.337840Z","shell.execute_reply.started":"2023-09-05T07:39:29.186457Z","shell.execute_reply":"2023-09-05T07:39:32.336646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_pred_trf,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:39:34.018723Z","iopub.execute_input":"2023-09-05T07:39:34.019114Z","iopub.status.idle":"2023-09-05T07:39:34.027106Z","shell.execute_reply.started":"2023-09-05T07:39:34.019085Z","shell.execute_reply":"2023-09-05T07:39:34.025982Z"},"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.fit(X_train_trf,y_train)\n    y_pred_trf=KNN.predict(X_test_trf)\n    print(accuracy_score(y_pred_trf,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:46:17.158082Z","iopub.execute_input":"2023-09-05T07:46:17.159433Z","iopub.status.idle":"2023-09-05T07:56:02.338738Z","shell.execute_reply.started":"2023-09-05T07:46:17.159390Z","shell.execute_reply":"2023-09-05T07:56:02.337364Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming to a 2D cordinate system\npca=PCA(n_components=2)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:57:46.603827Z","iopub.execute_input":"2023-09-05T07:57:46.604651Z","iopub.status.idle":"2023-09-05T07:57:48.010481Z","shell.execute_reply.started":"2023-09-05T07:57:46.604614Z","shell.execute_reply":"2023-09-05T07:57:48.009351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:57:59.908567Z","iopub.execute_input":"2023-09-05T07:57:59.908920Z","iopub.status.idle":"2023-09-05T07:57:59.915304Z","shell.execute_reply.started":"2023-09-05T07:57:59.908893Z","shell.execute_reply":"2023-09-05T07:57:59.914248Z"},"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":{"execution":{"iopub.status.busy":"2023-09-05T08:01:22.783482Z","iopub.execute_input":"2023-09-05T08:01:22.784031Z","iopub.status.idle":"2023-09-05T08:01:25.121346Z","shell.execute_reply.started":"2023-09-05T08:01:22.783997Z","shell.execute_reply":"2023-09-05T08:01:25.120466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming to 3D cordinate system\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":"2023-09-05T07:59:18.137768Z","iopub.execute_input":"2023-09-05T07:59:18.138157Z","iopub.status.idle":"2023-09-05T07:59:19.648646Z","shell.execute_reply.started":"2023-09-05T07:59:18.138131Z","shell.execute_reply":"2023-09-05T07:59:19.647782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:59:27.322566Z","iopub.execute_input":"2023-09-05T07:59:27.323141Z","iopub.status.idle":"2023-09-05T07:59:27.329835Z","shell.execute_reply.started":"2023-09-05T07:59:27.323114Z","shell.execute_reply":"2023-09-05T07:59:27.328630Z"},"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(\n    margin=dict(l=20, r=20, t=20, b=20),\n    paper_bgcolor=\"LightSteelBlue\",\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:08:07.599633Z","iopub.execute_input":"2023-09-05T08:08:07.600081Z","iopub.status.idle":"2023-09-05T08:08:07.799490Z","shell.execute_reply.started":"2023-09-05T08:08:07.600051Z","shell.execute_reply":"2023-09-05T08:08:07.798638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n# Eigen values","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:13:46.879184Z","iopub.execute_input":"2023-09-05T08:13:46.879728Z","iopub.status.idle":"2023-09-05T08:13:46.892961Z","shell.execute_reply.started":"2023-09-05T08:13:46.879702Z","shell.execute_reply":"2023-09-05T08:13:46.892233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:14:07.563946Z","iopub.execute_input":"2023-09-05T08:14:07.564447Z","iopub.status.idle":"2023-09-05T08:14:07.569886Z","shell.execute_reply.started":"2023-09-05T08:14:07.564421Z","shell.execute_reply":"2023-09-05T08:14:07.568938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:14:17.502975Z","iopub.execute_input":"2023-09-05T08:14:17.503345Z","iopub.status.idle":"2023-09-05T08:14:17.518377Z","shell.execute_reply.started":"2023-09-05T08:14:17.503316Z","shell.execute_reply":"2023-09-05T08:14:17.517480Z"},"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":{"execution":{"iopub.status.busy":"2023-09-05T08:14:25.693708Z","iopub.execute_input":"2023-09-05T08:14:25.694039Z","iopub.status.idle":"2023-09-05T08:14:31.797891Z","shell.execute_reply.started":"2023-09-05T08:14:25.694014Z","shell.execute_reply":"2023-09-05T08:14:31.797071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:14:34.748262Z","iopub.execute_input":"2023-09-05T08:14:34.748611Z","iopub.status.idle":"2023-09-05T08:14:34.755052Z","shell.execute_reply.started":"2023-09-05T08:14:34.748565Z","shell.execute_reply":"2023-09-05T08:14:34.754034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:14:36.627836Z","iopub.execute_input":"2023-09-05T08:14:36.628154Z","iopub.status.idle":"2023-09-05T08:14:36.634950Z","shell.execute_reply.started":"2023-09-05T08:14:36.628130Z","shell.execute_reply":"2023-09-05T08:14:36.634080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:14:38.422386Z","iopub.execute_input":"2023-09-05T08:14:38.422749Z","iopub.status.idle":"2023-09-05T08:14:38.437401Z","shell.execute_reply.started":"2023-09-05T08:14:38.422722Z","shell.execute_reply":"2023-09-05T08:14:38.436632Z"},"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-05T08:14:44.058204Z","iopub.execute_input":"2023-09-05T08:14:44.058566Z","iopub.status.idle":"2023-09-05T08:14:44.243398Z","shell.execute_reply.started":"2023-09-05T08:14:44.058537Z","shell.execute_reply":"2023-09-05T08:14:44.242527Z"},"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"}}