{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31328,"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-05-23T09:40:09.636396Z","iopub.execute_input":"2026-05-23T09:40:09.637074Z","iopub.status.idle":"2026-05-23T09:40:09.646055Z","shell.execute_reply.started":"2026-05-23T09:40:09.637035Z","shell.execute_reply":"2026-05-23T09:40:09.644795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')\ndf.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:09.647866Z","iopub.execute_input":"2026-05-23T09:40:09.649019Z","iopub.status.idle":"2026-05-23T09:40:11.661413Z","shell.execute_reply.started":"2026-05-23T09:40:09.648966Z","shell.execute_reply":"2026-05-23T09:40:11.660410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:11.662649Z","iopub.execute_input":"2026-05-23T09:40:11.663010Z","iopub.status.idle":"2026-05-23T09:40:11.668669Z","shell.execute_reply.started":"2026-05-23T09:40:11.662967Z","shell.execute_reply":"2026-05-23T09:40:11.667849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:11.670845Z","iopub.execute_input":"2026-05-23T09:40:11.671201Z","iopub.status.idle":"2026-05-23T09:40:11.693416Z","shell.execute_reply.started":"2026-05-23T09:40:11.671174Z","shell.execute_reply":"2026-05-23T09:40:11.692363Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **We have 42000 images which have 784 pixels thats why (42000,785). That extra 1 is label column**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df.iloc[19098,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:11.694770Z","iopub.execute_input":"2026-05-23T09:40:11.695524Z","iopub.status.idle":"2026-05-23T09:40:11.835491Z","shell.execute_reply.started":"2026-05-23T09:40:11.695475Z","shell.execute_reply":"2026-05-23T09:40:11.834554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:11.836842Z","iopub.execute_input":"2026-05-23T09:40:11.837224Z","iopub.status.idle":"2026-05-23T09:40:11.842321Z","shell.execute_reply.started":"2026-05-23T09:40:11.837182Z","shell.execute_reply":"2026-05-23T09:40:11.841557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nxt,xte,yt,yte=train_test_split(x,y,test_size=0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:11.843570Z","iopub.execute_input":"2026-05-23T09:40:11.843987Z","iopub.status.idle":"2026-05-23T09:40:12.073154Z","shell.execute_reply.started":"2026-05-23T09:40:11.843941Z","shell.execute_reply":"2026-05-23T09:40:12.072253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xt.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:12.074223Z","iopub.execute_input":"2026-05-23T09:40:12.074782Z","iopub.status.idle":"2026-05-23T09:40:12.080614Z","shell.execute_reply.started":"2026-05-23T09:40:12.074753Z","shell.execute_reply":"2026-05-23T09:40:12.079660Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:12.081955Z","iopub.execute_input":"2026-05-23T09:40:12.082481Z","iopub.status.idle":"2026-05-23T09:40:12.095326Z","shell.execute_reply.started":"2026-05-23T09:40:12.082433Z","shell.execute_reply":"2026-05-23T09:40:12.094474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:12.098537Z","iopub.execute_input":"2026-05-23T09:40:12.098965Z","iopub.status.idle":"2026-05-23T09:40:12.108116Z","shell.execute_reply.started":"2026-05-23T09:40:12.098906Z","shell.execute_reply":"2026-05-23T09:40:12.107226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(xt,yt)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:12.109376Z","iopub.execute_input":"2026-05-23T09:40:12.109729Z","iopub.status.idle":"2026-05-23T09:40:12.235618Z","shell.execute_reply.started":"2026-05-23T09:40:12.109702Z","shell.execute_reply":"2026-05-23T09:40:12.234810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(xte)\nprint(time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:12.236634Z","iopub.execute_input":"2026-05-23T09:40:12.236863Z","iopub.status.idle":"2026-05-23T09:40:28.998250Z","shell.execute_reply.started":"2026-05-23T09:40:12.236838Z","shell.execute_reply":"2026-05-23T09:40:28.997246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(yte,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:28.999588Z","iopub.execute_input":"2026-05-23T09:40:28.999903Z","iopub.status.idle":"2026-05-23T09:40:29.008681Z","shell.execute_reply.started":"2026-05-23T09:40:28.999874Z","shell.execute_reply":"2026-05-23T09:40:29.007749Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **PCA**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscalar=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:29.009864Z","iopub.execute_input":"2026-05-23T09:40:29.010354Z","iopub.status.idle":"2026-05-23T09:40:29.021605Z","shell.execute_reply.started":"2026-05-23T09:40:29.010302Z","shell.execute_reply":"2026-05-23T09:40:29.020438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xt_trf=scalar.fit_transform(xt)\nxte_trf=scalar.transform(xte)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:29.023066Z","iopub.execute_input":"2026-05-23T09:40:29.023537Z","iopub.status.idle":"2026-05-23T09:40:29.500237Z","shell.execute_reply.started":"2026-05-23T09:40:29.023491Z","shell.execute_reply":"2026-05-23T09:40:29.499339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=100) #This n_components is the no of columns we want at last","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:29.501370Z","iopub.execute_input":"2026-05-23T09:40:29.501659Z","iopub.status.idle":"2026-05-23T09:40:29.506541Z","shell.execute_reply.started":"2026-05-23T09:40:29.501632Z","shell.execute_reply":"2026-05-23T09:40:29.505327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xt_trf=pca.fit_transform(xt_trf)\nxte_trf=pca.transform(xte_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:29.507810Z","iopub.execute_input":"2026-05-23T09:40:29.508499Z","iopub.status.idle":"2026-05-23T09:40:30.743674Z","shell.execute_reply.started":"2026-05-23T09:40:29.508400Z","shell.execute_reply":"2026-05-23T09:40:30.742552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xt_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:30.744925Z","iopub.execute_input":"2026-05-23T09:40:30.745243Z","iopub.status.idle":"2026-05-23T09:40:30.751048Z","shell.execute_reply.started":"2026-05-23T09:40:30.745207Z","shell.execute_reply":"2026-05-23T09:40:30.750359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(xt_trf,yt)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:30.752251Z","iopub.execute_input":"2026-05-23T09:40:30.753003Z","iopub.status.idle":"2026-05-23T09:40:30.773445Z","shell.execute_reply.started":"2026-05-23T09:40:30.752953Z","shell.execute_reply":"2026-05-23T09:40:30.772473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(xte_trf)\naccuracy_score(yte,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:30.774620Z","iopub.execute_input":"2026-05-23T09:40:30.775455Z","iopub.status.idle":"2026-05-23T09:40:32.438026Z","shell.execute_reply.started":"2026-05-23T09:40:30.775424Z","shell.execute_reply":"2026-05-23T09:40:32.437154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for i in range(1,785):\n#     pca=PCA(n_components=i)\n#     xt_trf=pca.fit_transform(xt)\n#     xte_trf=pca.transform(xte)\n#     knn.fit(xt_trf,yt)\n#     yp=knn.predict(xte_trf)\n#     print(accuracy_score(yte,yp)) #With this we can see how much accuracy its there with i","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:32.439007Z","iopub.execute_input":"2026-05-23T09:40:32.439416Z","iopub.status.idle":"2026-05-23T09:40:32.443465Z","shell.execute_reply.started":"2026-05-23T09:40:32.439357Z","shell.execute_reply":"2026-05-23T09:40:32.442550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Eigen Values\npca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:40:32.444847Z","iopub.execute_input":"2026-05-23T09:40:32.445220Z","iopub.status.idle":"2026-05-23T09:40:32.463136Z","shell.execute_reply.started":"2026-05-23T09:40:32.445177Z","shell.execute_reply":"2026-05-23T09:40:32.462113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T09:42:08.936707Z","iopub.execute_input":"2026-05-23T09:42:08.937811Z","iopub.status.idle":"2026-05-23T09:42:08.945118Z","shell.execute_reply.started":"2026-05-23T09:42:08.937769Z","shell.execute_reply":"2026-05-23T09:42:08.944037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}