{"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-14T18:41:48.022181Z","iopub.execute_input":"2026-08-14T18:41:48.022416Z","iopub.status.idle":"2026-08-14T18:41:48.033411Z","shell.execute_reply.started":"2026-08-14T18:41:48.022395Z","shell.execute_reply":"2026-08-14T18:41:48.032877Z"}},"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-14T18:42:58.454663Z","iopub.execute_input":"2026-08-14T18:42:58.455246Z","iopub.status.idle":"2026-08-14T18:43:00.250655Z","shell.execute_reply.started":"2026-08-14T18:42:58.455220Z","shell.execute_reply":"2026-08-14T18:43:00.249934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T18:43:03.432833Z","iopub.execute_input":"2026-08-14T18:43:03.433074Z","iopub.status.idle":"2026-08-14T18:43:03.458532Z","shell.execute_reply.started":"2026-08-14T18:43:03.433055Z","shell.execute_reply":"2026-08-14T18:43:03.457932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T18:43:11.752999Z","iopub.execute_input":"2026-08-14T18:43:11.753268Z","iopub.status.idle":"2026-08-14T18:43:11.758456Z","shell.execute_reply.started":"2026-08-14T18:43:11.753246Z","shell.execute_reply":"2026-08-14T18:43:11.757548Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"28*28 =784 features and plus 1 os target feature ","metadata":{}},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T18:49:18.379650Z","iopub.execute_input":"2026-08-14T18:49:18.379938Z","iopub.status.idle":"2026-08-14T18:49:18.394178Z","shell.execute_reply.started":"2026-08-14T18:49:18.379916Z","shell.execute_reply":"2026-08-14T18:49:18.393636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.iloc[8565]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T18:50:05.639373Z","iopub.execute_input":"2026-08-14T18:50:05.639594Z","iopub.status.idle":"2026-08-14T18:50:05.645384Z","shell.execute_reply.started":"2026-08-14T18:50:05.639576Z","shell.execute_reply":"2026-08-14T18:50:05.644729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.iloc[8565,1:] # df.iloc[row,column]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T18:51:29.317824Z","iopub.execute_input":"2026-08-14T18:51:29.318083Z","iopub.status.idle":"2026-08-14T18:51:29.324208Z","shell.execute_reply.started":"2026-08-14T18:51:29.318061Z","shell.execute_reply":"2026-08-14T18:51:29.323596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# the data we get after this is series df.iloc[8565, 1:] but reshape requires numpy srray thats why to_numpy\ndf.iloc[8565, 1:].to_numpy() # this to_numpy because i want to get the all values of the columns without to_numpy that wont happen","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:02:27.210388Z","iopub.execute_input":"2026-08-14T19:02:27.210694Z","iopub.status.idle":"2026-08-14T19:02:27.217424Z","shell.execute_reply.started":"2026-08-14T19:02:27.210668Z","shell.execute_reply":"2026-08-14T19:02:27.216800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# here in this dataset each row represents a single image and each row has target value means that actual number...\n# so as it is mnist dataset it has 28*28 pixels image and that are flatend into 784 values thats why we have 784 featuress named as pixel 0 pixel 1 \n\n# 2d data         dataset row\n# 1 2 3         \n# 4 5 6     ->  1 2 3 4 5 6 7 8 9\n# 7 8 9\n\n# so this is how the images are flattend to a row\n\nimport matplotlib.pyplot as plt\n\nimg = df.iloc[8565, 1:].to_numpy().reshape(28, 28) # an iamge is actually 2d so we have to reshape it again in 28*28 because in data frame it store values \n\nplt.imshow(img, cmap=\"gray\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T18:53:05.993446Z","iopub.execute_input":"2026-08-14T18:53:05.993672Z","iopub.status.idle":"2026-08-14T18:53:06.077451Z","shell.execute_reply.started":"2026-08-14T18:53:05.993651Z","shell.execute_reply":"2026-08-14T18:53:06.076864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img2=df.iloc[101,1:].to_numpy().reshape(28,28)\n\nplt.imshow(img2)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:07:07.165647Z","iopub.execute_input":"2026-08-14T19:07:07.166352Z","iopub.status.idle":"2026-08-14T19:07:07.238571Z","shell.execute_reply.started":"2026-08-14T19:07:07.166316Z","shell.execute_reply":"2026-08-14T19:07:07.237986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:12:00.135573Z","iopub.execute_input":"2026-08-14T19:12:00.135852Z","iopub.status.idle":"2026-08-14T19:12:00.146355Z","shell.execute_reply.started":"2026-08-14T19:12:00.135830Z","shell.execute_reply":"2026-08-14T19:12:00.145795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=df.drop('label',axis=1)\ny=df['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:12:30.598512Z","iopub.execute_input":"2026-08-14T19:12:30.598888Z","iopub.status.idle":"2026-08-14T19:12:30.646637Z","shell.execute_reply.started":"2026-08-14T19:12:30.598860Z","shell.execute_reply":"2026-08-14T19:12:30.646009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:12:36.487372Z","iopub.execute_input":"2026-08-14T19:12:36.487648Z","iopub.status.idle":"2026-08-14T19:12:36.498418Z","shell.execute_reply.started":"2026-08-14T19:12:36.487625Z","shell.execute_reply":"2026-08-14T19:12:36.497800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:12:44.494243Z","iopub.execute_input":"2026-08-14T19:12:44.494477Z","iopub.status.idle":"2026-08-14T19:12:44.500039Z","shell.execute_reply.started":"2026-08-14T19:12:44.494447Z","shell.execute_reply":"2026-08-14T19:12:44.499295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\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-14T19:13:04.686479Z","iopub.execute_input":"2026-08-14T19:13:04.686790Z","iopub.status.idle":"2026-08-14T19:13:05.550537Z","shell.execute_reply.started":"2026-08-14T19:13:04.686768Z","shell.execute_reply":"2026-08-14T19:13:05.549759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape,y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:13:19.215525Z","iopub.execute_input":"2026-08-14T19:13:19.215966Z","iopub.status.idle":"2026-08-14T19:13:19.221584Z","shell.execute_reply.started":"2026-08-14T19:13:19.215942Z","shell.execute_reply":"2026-08-14T19:13:19.220756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nmodel=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:14:26.625577Z","iopub.execute_input":"2026-08-14T19:14:26.625872Z","iopub.status.idle":"2026-08-14T19:14:26.629784Z","shell.execute_reply.started":"2026-08-14T19:14:26.625850Z","shell.execute_reply":"2026-08-14T19:14:26.628750Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:14:29.174498Z","iopub.execute_input":"2026-08-14T19:14:29.174803Z","iopub.status.idle":"2026-08-14T19:14:29.185648Z","shell.execute_reply.started":"2026-08-14T19:14:29.174778Z","shell.execute_reply":"2026-08-14T19:14:29.184798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:14:44.224822Z","iopub.execute_input":"2026-08-14T19:14:44.225105Z","iopub.status.idle":"2026-08-14T19:14:44.313936Z","shell.execute_reply.started":"2026-08-14T19:14:44.225071Z","shell.execute_reply":"2026-08-14T19:14:44.313210Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:14:57.279470Z","iopub.execute_input":"2026-08-14T19:14:57.279789Z","iopub.status.idle":"2026-08-14T19:15:03.973320Z","shell.execute_reply.started":"2026-08-14T19:14:57.279758Z","shell.execute_reply":"2026-08-14T19:15:03.972886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:15:09.447086Z","iopub.execute_input":"2026-08-14T19:15:09.447331Z","iopub.status.idle":"2026-08-14T19:15:09.452494Z","shell.execute_reply.started":"2026-08-14T19:15:09.447311Z","shell.execute_reply":"2026-08-14T19:15:09.451549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nprint(accuracy_score(y_pred,y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:15:36.006530Z","iopub.execute_input":"2026-08-14T19:15:36.006857Z","iopub.status.idle":"2026-08-14T19:15:36.013205Z","shell.execute_reply.started":"2026-08-14T19:15:36.006831Z","shell.execute_reply":"2026-08-14T19:15:36.012620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# with PCA\n\n# FIRST STEP IN IN PCA IS U HAVE TO STANDARIZE THE DATA\n\n\nfrom sklearn.preprocessing import StandardScaler\n\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:16:41.525301Z","iopub.execute_input":"2026-08-14T19:16:41.525529Z","iopub.status.idle":"2026-08-14T19:16:41.528875Z","shell.execute_reply.started":"2026-08-14T19:16:41.525509Z","shell.execute_reply":"2026-08-14T19:16:41.528250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:16:45.909332Z","iopub.execute_input":"2026-08-14T19:16:45.909558Z","iopub.status.idle":"2026-08-14T19:16:45.914168Z","shell.execute_reply.started":"2026-08-14T19:16:45.909539Z","shell.execute_reply":"2026-08-14T19:16:45.913599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train=scaler.fit_transform(X_train)\nX_test=scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:21:16.287102Z","iopub.execute_input":"2026-08-14T19:21:16.287336Z","iopub.status.idle":"2026-08-14T19:21:16.527223Z","shell.execute_reply.started":"2026-08-14T19:21:16.287313Z","shell.execute_reply":"2026-08-14T19:21:16.526219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#PCA \n\nfrom sklearn.decomposition import PCA\n\npca=PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:22:08.968710Z","iopub.execute_input":"2026-08-14T19:22:08.969426Z","iopub.status.idle":"2026-08-14T19:22:08.972785Z","shell.execute_reply.started":"2026-08-14T19:22:08.969387Z","shell.execute_reply":"2026-08-14T19:22:08.972049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:22:32.493527Z","iopub.execute_input":"2026-08-14T19:22:32.493934Z","iopub.status.idle":"2026-08-14T19:22:32.499455Z","shell.execute_reply.started":"2026-08-14T19:22:32.493914Z","shell.execute_reply":"2026-08-14T19:22:32.498662Z"}},"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-14T19:22:59.201079Z","iopub.execute_input":"2026-08-14T19:22:59.201334Z","iopub.status.idle":"2026-08-14T19:22:59.819716Z","shell.execute_reply.started":"2026-08-14T19:22:59.201313Z","shell.execute_reply":"2026-08-14T19:22:59.819205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nknn = KNeighborsClassifier()\n\nknn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:23:53.948241Z","iopub.execute_input":"2026-08-14T19:23:53.948501Z","iopub.status.idle":"2026-08-14T19:23:53.959120Z","shell.execute_reply.started":"2026-08-14T19:23:53.948480Z","shell.execute_reply":"2026-08-14T19:23:53.958377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)\ny_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:24:05.904041Z","iopub.execute_input":"2026-08-14T19:24:05.904291Z","iopub.status.idle":"2026-08-14T19:24:07.694928Z","shell.execute_reply.started":"2026-08-14T19:24:05.904272Z","shell.execute_reply":"2026-08-14T19:24:07.694337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Accuracy:\", accuracy_score(y_test, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:24:15.014996Z","iopub.execute_input":"2026-08-14T19:24:15.015241Z","iopub.status.idle":"2026-08-14T19:24:15.020873Z","shell.execute_reply.started":"2026-08-14T19:24:15.015221Z","shell.execute_reply":"2026-08-14T19:24:15.020289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# as some accuracy reducedd so we need to find best pca components which gives us best accuracy for that we will run a for loop 785 times\ncompo={}\nfor i in range(1,785):\n\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    knn.fit(X_train_trf,y_train)\n    \n    y_pred = knn.predict(X_test_trf)\n\n    accuracy=accuracy_score(y_test, y_pred)\n    print(i,\" - Accuracy :\",accuracy )\n    compo[i]=accuracy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:28:50.325398Z","iopub.execute_input":"2026-08-14T19:28:50.325678Z","iopub.status.idle":"2026-08-14T19:55:33.608790Z","shell.execute_reply.started":"2026-08-14T19:28:50.325656Z","shell.execute_reply":"2026-08-14T19:55:33.607466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"compo","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:56:08.040712Z","iopub.execute_input":"2026-08-14T19:56:08.041031Z","iopub.status.idle":"2026-08-14T19:56:08.049320Z","shell.execute_reply.started":"2026-08-14T19:56:08.040980Z","shell.execute_reply":"2026-08-14T19:56:08.048771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max_key = max(compo, key=compo.get)\n\nprint(max_key)\nprint(compo[max_key])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:57:30.141050Z","iopub.execute_input":"2026-08-14T19:57:30.141290Z","iopub.status.idle":"2026-08-14T19:57:30.145730Z","shell.execute_reply.started":"2026-08-14T19:57:30.141269Z","shell.execute_reply":"2026-08-14T19:57:30.145011Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca=PCA(n_components=85)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)\n    \nknn = KNeighborsClassifier()\nknn.fit(X_train_trf,y_train)\n    \ny_pred = knn.predict(X_test_trf)\n\naccuracy=accuracy_score(y_test, y_pred)\nprint(\" - Accuracy :\",accuracy )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-14T19:58:34.806361Z","iopub.execute_input":"2026-08-14T19:58:34.806614Z","iopub.status.idle":"2026-08-14T19:58:36.541134Z","shell.execute_reply.started":"2026-08-14T19:58:34.806593Z","shell.execute_reply":"2026-08-14T19:58:36.540548Z"}},"outputs":[],"execution_count":null}]}