{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31040,"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","trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:22.352935Z","iopub.execute_input":"2025-07-03T15:05:22.353395Z","iopub.status.idle":"2025-07-03T15:05:22.372562Z","shell.execute_reply.started":"2025-07-03T15:05:22.353369Z","shell.execute_reply":"2025-07-03T15:05:22.371452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:22.374223Z","iopub.execute_input":"2025-07-03T15:05:22.374649Z","iopub.status.idle":"2025-07-03T15:05:24.986556Z","shell.execute_reply.started":"2025-07-03T15:05:22.374613Z","shell.execute_reply":"2025-07-03T15:05:24.984927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:24.988654Z","iopub.execute_input":"2025-07-03T15:05:24.989857Z","iopub.status.idle":"2025-07-03T15:05:25.014866Z","shell.execute_reply.started":"2025-07-03T15:05:24.989815Z","shell.execute_reply":"2025-07-03T15:05:25.013932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.015832Z","iopub.execute_input":"2025-07-03T15:05:25.016155Z","iopub.status.idle":"2025-07-03T15:05:25.044481Z","shell.execute_reply.started":"2025-07-03T15:05:25.016132Z","shell.execute_reply":"2025-07-03T15:05:25.043027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.047259Z","iopub.execute_input":"2025-07-03T15:05:25.048635Z","iopub.status.idle":"2025-07-03T15:05:25.077189Z","shell.execute_reply.started":"2025-07-03T15:05:25.048604Z","shell.execute_reply":"2025-07-03T15:05:25.075878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df.iloc[18062,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.078233Z","iopub.execute_input":"2025-07-03T15:05:25.079114Z","iopub.status.idle":"2025-07-03T15:05:25.275191Z","shell.execute_reply.started":"2025-07-03T15:05:25.079079Z","shell.execute_reply":"2025-07-03T15:05:25.274052Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.276243Z","iopub.execute_input":"2025-07-03T15:05:25.276626Z","iopub.status.idle":"2025-07-03T15:05:25.282361Z","shell.execute_reply.started":"2025-07-03T15:05:25.276605Z","shell.execute_reply":"2025-07-03T15:05:25.281117Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.283468Z","iopub.execute_input":"2025-07-03T15:05:25.283922Z","iopub.status.idle":"2025-07-03T15:05:25.642065Z","shell.execute_reply.started":"2025-07-03T15:05:25.283868Z","shell.execute_reply":"2025-07-03T15:05:25.640729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.643187Z","iopub.execute_input":"2025-07-03T15:05:25.643532Z","iopub.status.idle":"2025-07-03T15:05:25.650930Z","shell.execute_reply.started":"2025-07-03T15:05:25.643509Z","shell.execute_reply":"2025-07-03T15:05:25.649745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn =KNeighborsClassifier()\nknn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.652204Z","iopub.execute_input":"2025-07-03T15:05:25.652619Z","iopub.status.idle":"2025-07-03T15:05:25.802686Z","shell.execute_reply.started":"2025-07-03T15:05:25.652581Z","shell.execute_reply":"2025-07-03T15:05:25.801619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred=knn.predict(X_test)\nprint(time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:25.805930Z","iopub.execute_input":"2025-07-03T15:05:25.806285Z","iopub.status.idle":"2025-07-03T15:05:46.866194Z","shell.execute_reply.started":"2025-07-03T15:05:25.806260Z","shell.execute_reply":"2025-07-03T15:05:46.865277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:46.867034Z","iopub.execute_input":"2025-07-03T15:05:46.867273Z","iopub.status.idle":"2025-07-03T15:05:46.876649Z","shell.execute_reply.started":"2025-07-03T15:05:46.867254Z","shell.execute_reply":"2025-07-03T15:05:46.875606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:46.877553Z","iopub.execute_input":"2025-07-03T15:05:46.877890Z","iopub.status.idle":"2025-07-03T15:05:47.552439Z","shell.execute_reply.started":"2025-07-03T15:05:46.877863Z","shell.execute_reply":"2025-07-03T15:05:47.551546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components = 300)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:47.553271Z","iopub.execute_input":"2025-07-03T15:05:47.553627Z","iopub.status.idle":"2025-07-03T15:05:56.993493Z","shell.execute_reply.started":"2025-07-03T15:05:47.553596Z","shell.execute_reply":"2025-07-03T15:05:56.992436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:56.994514Z","iopub.execute_input":"2025-07-03T15:05:56.994851Z","iopub.status.idle":"2025-07-03T15:05:57.001007Z","shell.execute_reply.started":"2025-07-03T15:05:56.994822Z","shell.execute_reply":"2025-07-03T15:05:56.999808Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn =KNeighborsClassifier()\nknn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:57.002276Z","iopub.execute_input":"2025-07-03T15:05:57.002639Z","iopub.status.idle":"2025-07-03T15:05:57.060157Z","shell.execute_reply.started":"2025-07-03T15:05:57.002611Z","shell.execute_reply":"2025-07-03T15:05:57.059053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred1=knn.predict(X_test_trf)\nprint(time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:05:57.061281Z","iopub.execute_input":"2025-07-03T15:05:57.061587Z","iopub.status.idle":"2025-07-03T15:06:01.061298Z","shell.execute_reply.started":"2025-07-03T15:05:57.061564Z","shell.execute_reply":"2025-07-03T15:06:01.060008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:01.062420Z","iopub.execute_input":"2025-07-03T15:06:01.062759Z","iopub.status.idle":"2025-07-03T15:06:01.071266Z","shell.execute_reply.started":"2025-07-03T15:06:01.062711Z","shell.execute_reply":"2025-07-03T15:06:01.070392Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### for get to know at which component we get what result\n\n\n    for i in range (1,785):\n        if i %10 == 0:\n            print('i=',i)\n            pca = PCA(n_components = i)\n        \n            X_train_trf = pca.fit_transform(X_train)\n            X_test_trf = pca.transform(X_test)\n        \n            knn =KNeighborsClassifier()\n            knn.fit(X_train_trf,y_train)\n        \n            y_pred=knn.predict(X_test_trf)\n        \n            \n            print(accuracy_score(y_test,y_pred))\n\n    ","metadata":{"execution":{"iopub.status.busy":"2025-07-03T14:19:46.268841Z","iopub.execute_input":"2025-07-03T14:19:46.269829Z","iopub.status.idle":"2025-07-03T14:29:22.449455Z","shell.execute_reply.started":"2025-07-03T14:19:46.269796Z","shell.execute_reply":"2025-07-03T14:29:22.447469Z"}}},{"cell_type":"code","source":"#transforming to a 2D coordinate system\npca = PCA(n_components = 2)\n    \nX_train_trf1 = pca.fit_transform(X_train)\nX_test_trf1 = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:01.072324Z","iopub.execute_input":"2025-07-03T15:06:01.072610Z","iopub.status.idle":"2025-07-03T15:06:02.357098Z","shell.execute_reply.started":"2025-07-03T15:06:01.072590Z","shell.execute_reply":"2025-07-03T15:06:02.355855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:02.358394Z","iopub.execute_input":"2025-07-03T15:06:02.358723Z","iopub.status.idle":"2025-07-03T15:06:02.366646Z","shell.execute_reply.started":"2025-07-03T15:06:02.358700Z","shell.execute_reply":"2025-07-03T15:06:02.365378Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf1 = y_train.astype(str)\nfig = px.scatter(x=X_train_trf1[:,0],\n                y = X_train_trf1[:,1],\n                color = y_train_trf1,\n                color_discrete_sequence = px.colors.qualitative.G10)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:02.367717Z","iopub.execute_input":"2025-07-03T15:06:02.368122Z","iopub.status.idle":"2025-07-03T15:06:02.486479Z","shell.execute_reply.started":"2025-07-03T15:06:02.368100Z","shell.execute_reply":"2025-07-03T15:06:02.484979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#transforming to a 3D coordinate system\npca = PCA(n_components = 3)\n    \nX_train_trf1 = pca.fit_transform(X_train)\nX_test_trf1 = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:02.488087Z","iopub.execute_input":"2025-07-03T15:06:02.488504Z","iopub.status.idle":"2025-07-03T15:06:03.887295Z","shell.execute_reply.started":"2025-07-03T15:06:02.488469Z","shell.execute_reply":"2025-07-03T15:06:03.886237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:03.888137Z","iopub.execute_input":"2025-07-03T15:06:03.888490Z","iopub.status.idle":"2025-07-03T15:06:03.896138Z","shell.execute_reply.started":"2025-07-03T15:06:03.888456Z","shell.execute_reply":"2025-07-03T15:06:03.894639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf1 = y_train.astype(str)\nfig = px.scatter_3d(x=X_train_trf1[:,0],\n                    y = X_train_trf1[:,1],\n                    z = X_train_trf1[:,2],\n                color = y_train_trf1,\n                color_discrete_sequence = px.colors.qualitative.G10)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:03.897217Z","iopub.execute_input":"2025-07-03T15:06:03.897670Z","iopub.status.idle":"2025-07-03T15:06:04.024514Z","shell.execute_reply.started":"2025-07-03T15:06:03.897639Z","shell.execute_reply":"2025-07-03T15:06:04.023280Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ # they are eigen value","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:04.026478Z","iopub.execute_input":"2025-07-03T15:06:04.026951Z","iopub.status.idle":"2025-07-03T15:06:04.035255Z","shell.execute_reply.started":"2025-07-03T15:06:04.026913Z","shell.execute_reply":"2025-07-03T15:06:04.034113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_ # they are all eigen vectors","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:04.036457Z","iopub.execute_input":"2025-07-03T15:06:04.036746Z","iopub.status.idle":"2025-07-03T15:06:04.065636Z","shell.execute_reply.started":"2025-07-03T15:06:04.036724Z","shell.execute_reply":"2025-07-03T15:06:04.064503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape # they are all eigen vectors","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:04.066928Z","iopub.execute_input":"2025-07-03T15:06:04.067247Z","iopub.status.idle":"2025-07-03T15:06:04.082882Z","shell.execute_reply.started":"2025-07-03T15:06:04.067225Z","shell.execute_reply":"2025-07-03T15:06:04.081654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:04.083955Z","iopub.execute_input":"2025-07-03T15:06:04.084255Z","iopub.status.idle":"2025-07-03T15:06:04.106903Z","shell.execute_reply.started":"2025-07-03T15:06:04.084225Z","shell.execute_reply":"2025-07-03T15:06:04.105913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\npca = PCA(n_components = None)\n    \nX_train_trf1 = pca.fit_transform(X_train)\nX_test_trf1 = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:04.111182Z","iopub.execute_input":"2025-07-03T15:06:04.111521Z","iopub.status.idle":"2025-07-03T15:06:11.556342Z","shell.execute_reply.started":"2025-07-03T15:06:04.111494Z","shell.execute_reply":"2025-07-03T15:06:11.555292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:11.557226Z","iopub.execute_input":"2025-07-03T15:06:11.557471Z","iopub.status.idle":"2025-07-03T15:06:11.563678Z","shell.execute_reply.started":"2025-07-03T15:06:11.557452Z","shell.execute_reply":"2025-07-03T15:06:11.562658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:11.564734Z","iopub.execute_input":"2025-07-03T15:06:11.565824Z","iopub.status.idle":"2025-07-03T15:06:11.588443Z","shell.execute_reply.started":"2025-07-03T15:06:11.565793Z","shell.execute_reply":"2025-07-03T15:06:11.587592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:11.589502Z","iopub.execute_input":"2025-07-03T15:06:11.589773Z","iopub.status.idle":"2025-07-03T15:06:11.616733Z","shell.execute_reply.started":"2025-07-03T15:06:11.589753Z","shell.execute_reply":"2025-07-03T15:06:11.615702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:11.617848Z","iopub.execute_input":"2025-07-03T15:06:11.618176Z","iopub.status.idle":"2025-07-03T15:06:11.641844Z","shell.execute_reply.started":"2025-07-03T15:06:11.618155Z","shell.execute_reply":"2025-07-03T15:06:11.640733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:11.642872Z","iopub.execute_input":"2025-07-03T15:06:11.643227Z","iopub.status.idle":"2025-07-03T15:06:11.871281Z","shell.execute_reply.started":"2025-07-03T15:06:11.643204Z","shell.execute_reply":"2025-07-03T15:06:11.870379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# so we take 300 component to get 90 % variace","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:06:11.872467Z","iopub.execute_input":"2025-07-03T15:06:11.872748Z","iopub.status.idle":"2025-07-03T15:06:11.876868Z","shell.execute_reply.started":"2025-07-03T15:06:11.872719Z","shell.execute_reply":"2025-07-03T15:06:11.875879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}