{"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-08-21T14:33:16.998956Z","iopub.execute_input":"2026-08-21T14:33:16.999287Z","iopub.status.idle":"2026-08-21T14:33:17.007933Z","shell.execute_reply.started":"2026-08-21T14:33:16.999259Z","shell.execute_reply":"2026-08-21T14:33:17.007014Z"}},"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-08-21T14:33:17.009614Z","iopub.execute_input":"2026-08-21T14:33:17.009917Z","iopub.status.idle":"2026-08-21T14:33:19.388719Z","shell.execute_reply.started":"2026-08-21T14:33:17.009854Z","shell.execute_reply":"2026-08-21T14:33:19.387711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.389904Z","iopub.execute_input":"2026-08-21T14:33:19.390251Z","iopub.status.idle":"2026-08-21T14:33:19.398169Z","shell.execute_reply.started":"2026-08-21T14:33:19.390216Z","shell.execute_reply":"2026-08-21T14:33:19.396992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.400053Z","iopub.execute_input":"2026-08-21T14:33:19.400795Z","iopub.status.idle":"2026-08-21T14:33:19.436452Z","shell.execute_reply.started":"2026-08-21T14:33:19.400761Z","shell.execute_reply":"2026-08-21T14:33:19.435428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.437495Z","iopub.execute_input":"2026-08-21T14:33:19.437844Z","iopub.status.idle":"2026-08-21T14:33:19.471459Z","shell.execute_reply.started":"2026-08-21T14:33:19.437809Z","shell.execute_reply":"2026-08-21T14:33:19.470102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.472758Z","iopub.execute_input":"2026-08-21T14:33:19.473097Z","iopub.status.idle":"2026-08-21T14:33:19.498295Z","shell.execute_reply.started":"2026-08-21T14:33:19.473068Z","shell.execute_reply":"2026-08-21T14:33:19.497376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[3788,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.499858Z","iopub.execute_input":"2026-08-21T14:33:19.500494Z","iopub.status.idle":"2026-08-21T14:33:19.683500Z","shell.execute_reply.started":"2026-08-21T14:33:19.500452Z","shell.execute_reply":"2026-08-21T14:33:19.682520Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.685000Z","iopub.execute_input":"2026-08-21T14:33:19.685387Z","iopub.status.idle":"2026-08-21T14:33:19.691282Z","shell.execute_reply.started":"2026-08-21T14:33:19.685349Z","shell.execute_reply":"2026-08-21T14:33:19.690356Z"}},"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":"2026-08-21T14:33:19.693898Z","iopub.execute_input":"2026-08-21T14:33:19.694254Z","iopub.status.idle":"2026-08-21T14:33:19.950023Z","shell.execute_reply.started":"2026-08-21T14:33:19.694216Z","shell.execute_reply":"2026-08-21T14:33:19.949126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.951433Z","iopub.execute_input":"2026-08-21T14:33:19.951696Z","iopub.status.idle":"2026-08-21T14:33:19.957910Z","shell.execute_reply.started":"2026-08-21T14:33:19.951671Z","shell.execute_reply":"2026-08-21T14:33:19.957135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.959023Z","iopub.execute_input":"2026-08-21T14:33:19.959645Z","iopub.status.idle":"2026-08-21T14:33:19.976656Z","shell.execute_reply.started":"2026-08-21T14:33:19.959613Z","shell.execute_reply":"2026-08-21T14:33:19.975699Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.978042Z","iopub.execute_input":"2026-08-21T14:33:19.978464Z","iopub.status.idle":"2026-08-21T14:33:19.995869Z","shell.execute_reply.started":"2026-08-21T14:33:19.978427Z","shell.execute_reply":"2026-08-21T14:33:19.994759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:19.997265Z","iopub.execute_input":"2026-08-21T14:33:19.997696Z","iopub.status.idle":"2026-08-21T14:33:20.133323Z","shell.execute_reply.started":"2026-08-21T14:33:19.997615Z","shell.execute_reply":"2026-08-21T14:33:20.132370Z"}},"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":"2026-08-21T14:33:20.134535Z","iopub.execute_input":"2026-08-21T14:33:20.134839Z","iopub.status.idle":"2026-08-21T14:33:30.148922Z","shell.execute_reply.started":"2026-08-21T14:33:20.134803Z","shell.execute_reply":"2026-08-21T14:33:30.147863Z"}},"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":"2026-08-21T14:33:30.150184Z","iopub.execute_input":"2026-08-21T14:33:30.150548Z","iopub.status.idle":"2026-08-21T14:33:30.158709Z","shell.execute_reply.started":"2026-08-21T14:33:30.150509Z","shell.execute_reply":"2026-08-21T14:33:30.157747Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## PCA use","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:30.160034Z","iopub.execute_input":"2026-08-21T14:33:30.160349Z","iopub.status.idle":"2026-08-21T14:33:30.176674Z","shell.execute_reply.started":"2026-08-21T14:33:30.160321Z","shell.execute_reply":"2026-08-21T14:33:30.175428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf = scaler.fit_transform(X_train)\nX_test_trf = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:30.177847Z","iopub.execute_input":"2026-08-21T14:33:30.178192Z","iopub.status.idle":"2026-08-21T14:33:30.663387Z","shell.execute_reply.started":"2026-08-21T14:33:30.178164Z","shell.execute_reply":"2026-08-21T14:33:30.662518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# PCA \nfrom sklearn.decomposition import PCA\n# pca = PCA(n_components=None) # same num of col we create\npca = PCA(n_components=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:30.664931Z","iopub.execute_input":"2026-08-21T14:33:30.665291Z","iopub.status.idle":"2026-08-21T14:33:30.669477Z","shell.execute_reply.started":"2026-08-21T14:33:30.665254Z","shell.execute_reply":"2026-08-21T14:33:30.668447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:30.670625Z","iopub.execute_input":"2026-08-21T14:33:30.670981Z","iopub.status.idle":"2026-08-21T14:33:31.523969Z","shell.execute_reply.started":"2026-08-21T14:33:30.670951Z","shell.execute_reply":"2026-08-21T14:33:31.523180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:31.525116Z","iopub.execute_input":"2026-08-21T14:33:31.525431Z","iopub.status.idle":"2026-08-21T14:33:31.533013Z","shell.execute_reply.started":"2026-08-21T14:33:31.525400Z","shell.execute_reply":"2026-08-21T14:33:31.529667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:31.533896Z","iopub.execute_input":"2026-08-21T14:33:31.534207Z","iopub.status.idle":"2026-08-21T14:33:31.548624Z","shell.execute_reply.started":"2026-08-21T14:33:31.534175Z","shell.execute_reply":"2026-08-21T14:33:31.547643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:31.551640Z","iopub.execute_input":"2026-08-21T14:33:31.552039Z","iopub.status.idle":"2026-08-21T14:33:31.580664Z","shell.execute_reply.started":"2026-08-21T14:33:31.552002Z","shell.execute_reply":"2026-08-21T14:33:31.578665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:31.582258Z","iopub.execute_input":"2026-08-21T14:33:31.584173Z","iopub.status.idle":"2026-08-21T14:33:33.093084Z","shell.execute_reply.started":"2026-08-21T14:33:31.584131Z","shell.execute_reply":"2026-08-21T14:33:33.092358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:33:33.095997Z","iopub.execute_input":"2026-08-21T14:33:33.096308Z","iopub.status.idle":"2026-08-21T14:33:33.105027Z","shell.execute_reply.started":"2026-08-21T14:33:33.096282Z","shell.execute_reply":"2026-08-21T14:33:33.104059Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T14:38:17.534605Z","iopub.execute_input":"2026-08-21T14:38:17.534983Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T15:57:47.089578Z","iopub.execute_input":"2026-08-21T15:57:47.090085Z","iopub.status.idle":"2026-08-21T15:57:47.757438Z","shell.execute_reply.started":"2026-08-21T15:57:47.089989Z","shell.execute_reply":"2026-08-21T15:57:47.756747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T15:57:47.758574Z","iopub.execute_input":"2026-08-21T15:57:47.758911Z","iopub.status.idle":"2026-08-21T15:57:47.767196Z","shell.execute_reply.started":"2026-08-21T15:57:47.758861Z","shell.execute_reply":"2026-08-21T15:57:47.764960Z"}},"outputs":[],"execution_count":null},{"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                )\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T15:57:48.500982Z","iopub.execute_input":"2026-08-21T15:57:48.501315Z","iopub.status.idle":"2026-08-21T15:57:48.590501Z","shell.execute_reply.started":"2026-08-21T15:57:48.501290Z","shell.execute_reply":"2026-08-21T15:57:48.589505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#  same as 2d now convert into 3d \npca = PCA(n_components=3)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T15:58:02.022017Z","iopub.execute_input":"2026-08-21T15:58:02.022421Z","iopub.status.idle":"2026-08-21T15:58:02.699214Z","shell.execute_reply.started":"2026-08-21T15:58:02.022384Z","shell.execute_reply":"2026-08-21T15:58:02.698275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T15:58:02.700668Z","iopub.execute_input":"2026-08-21T15:58:02.701267Z","iopub.status.idle":"2026-08-21T15:58:02.709604Z","shell.execute_reply.started":"2026-08-21T15:58:02.701227Z","shell.execute_reply":"2026-08-21T15:58:02.708740Z"}},"outputs":[],"execution_count":null},{"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                )\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T15:58:03.179481Z","iopub.execute_input":"2026-08-21T15:58:03.179824Z","iopub.status.idle":"2026-08-21T15:58:03.271268Z","shell.execute_reply.started":"2026-08-21T15:58:03.179798Z","shell.execute_reply":"2026-08-21T15:58:03.270220Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_\n# eigen values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:00:42.996496Z","iopub.execute_input":"2026-08-21T16:00:42.997025Z","iopub.status.idle":"2026-08-21T16:00:43.003822Z","shell.execute_reply.started":"2026-08-21T16:00:42.996864Z","shell.execute_reply":"2026-08-21T16:00:43.002914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# eigen vector\npca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:00:45.312453Z","iopub.execute_input":"2026-08-21T16:00:45.312761Z","iopub.status.idle":"2026-08-21T16:00:45.320265Z","shell.execute_reply.started":"2026-08-21T16:00:45.312737Z","shell.execute_reply":"2026-08-21T16:00:45.319317Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:00:46.478547Z","iopub.execute_input":"2026-08-21T16:00:46.479485Z","iopub.status.idle":"2026-08-21T16:00:46.484986Z","shell.execute_reply.started":"2026-08-21T16:00:46.479446Z","shell.execute_reply":"2026-08-21T16:00:46.484124Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## now we will find percentage of every vector","metadata":{}},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:00:56.421761Z","iopub.execute_input":"2026-08-21T16:00:56.422442Z","iopub.status.idle":"2026-08-21T16:00:56.429108Z","shell.execute_reply.started":"2026-08-21T16:00:56.422408Z","shell.execute_reply":"2026-08-21T16:00:56.428226Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:00:59.377288Z","iopub.execute_input":"2026-08-21T16:00:59.377653Z","iopub.status.idle":"2026-08-21T16:01:00.847324Z","shell.execute_reply.started":"2026-08-21T16:00:59.377620Z","shell.execute_reply":"2026-08-21T16:01:00.846633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:01:27.442844Z","iopub.execute_input":"2026-08-21T16:01:27.443494Z","iopub.status.idle":"2026-08-21T16:01:27.450250Z","shell.execute_reply.started":"2026-08-21T16:01:27.443463Z","shell.execute_reply":"2026-08-21T16:01:27.449151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:01:31.746948Z","iopub.execute_input":"2026-08-21T16:01:31.747256Z","iopub.status.idle":"2026-08-21T16:01:31.754063Z","shell.execute_reply.started":"2026-08-21T16:01:31.747231Z","shell.execute_reply":"2026-08-21T16:01:31.753095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:01:39.430778Z","iopub.execute_input":"2026-08-21T16:01:39.431109Z","iopub.status.idle":"2026-08-21T16:01:39.444575Z","shell.execute_reply.started":"2026-08-21T16:01:39.431082Z","shell.execute_reply":"2026-08-21T16:01:39.443762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:01:49.483230Z","iopub.execute_input":"2026-08-21T16:01:49.483544Z","iopub.status.idle":"2026-08-21T16:01:49.494671Z","shell.execute_reply.started":"2026-08-21T16:01:49.483517Z","shell.execute_reply":"2026-08-21T16:01:49.493801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-21T16:01:59.736540Z","iopub.execute_input":"2026-08-21T16:01:59.737268Z","iopub.status.idle":"2026-08-21T16:01:59.874410Z","shell.execute_reply.started":"2026-08-21T16:01:59.737234Z","shell.execute_reply":"2026-08-21T16:01:59.873655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}