{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":10756780,"sourceType":"datasetVersion","datasetId":6671937}],"dockerImageVersionId":30886,"isInternetEnabled":true,"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-02-15T12:38:02.128671Z","iopub.execute_input":"2025-02-15T12:38:02.129175Z","iopub.status.idle":"2025-02-15T12:38:02.140880Z","shell.execute_reply.started":"2025-02-15T12:38:02.129137Z","shell.execute_reply":"2025-02-15T12:38:02.139260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:02.165472Z","iopub.execute_input":"2025-02-15T12:38:02.165982Z","iopub.status.idle":"2025-02-15T12:38:04.198682Z","shell.execute_reply.started":"2025-02-15T12:38:02.165926Z","shell.execute_reply":"2025-02-15T12:38:04.197370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.200092Z","iopub.execute_input":"2025-02-15T12:38:04.200414Z","iopub.status.idle":"2025-02-15T12:38:04.218821Z","shell.execute_reply.started":"2025-02-15T12:38:04.200387Z","shell.execute_reply":"2025-02-15T12:38:04.217329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.221056Z","iopub.execute_input":"2025-02-15T12:38:04.221397Z","iopub.status.idle":"2025-02-15T12:38:04.240431Z","shell.execute_reply.started":"2025-02-15T12:38:04.221367Z","shell.execute_reply":"2025-02-15T12:38:04.239302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = df['label']\nX = df.drop(columns = 'label' , axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.242053Z","iopub.execute_input":"2025-02-15T12:38:04.242520Z","iopub.status.idle":"2025-02-15T12:38:04.359996Z","shell.execute_reply.started":"2025-02-15T12:38:04.242477Z","shell.execute_reply":"2025-02-15T12:38:04.358664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.imshow(df.iloc[30247 , 1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.360988Z","iopub.execute_input":"2025-02-15T12:38:04.361282Z","iopub.status.idle":"2025-02-15T12:38:04.579154Z","shell.execute_reply.started":"2025-02-15T12:38:04.361257Z","shell.execute_reply":"2025-02-15T12:38:04.577398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.580668Z","iopub.execute_input":"2025-02-15T12:38:04.581217Z","iopub.status.idle":"2025-02-15T12:38:04.589358Z","shell.execute_reply.started":"2025-02-15T12:38:04.581164Z","shell.execute_reply":"2025-02-15T12:38:04.587936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.590585Z","iopub.execute_input":"2025-02-15T12:38:04.590956Z","iopub.status.idle":"2025-02-15T12:38:04.620921Z","shell.execute_reply.started":"2025-02-15T12:38:04.590926Z","shell.execute_reply":"2025-02-15T12:38:04.619631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n\nX_train , X_test , y_train , y_test = train_test_split(X,y,random_state = 42 , test_size = 0.2) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.624545Z","iopub.execute_input":"2025-02-15T12:38:04.624974Z","iopub.status.idle":"2025-02-15T12:38:04.863562Z","shell.execute_reply.started":"2025-02-15T12:38:04.624938Z","shell.execute_reply":"2025-02-15T12:38:04.862347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.865565Z","iopub.execute_input":"2025-02-15T12:38:04.866008Z","iopub.status.idle":"2025-02-15T12:38:04.873319Z","shell.execute_reply.started":"2025-02-15T12:38:04.865976Z","shell.execute_reply":"2025-02-15T12:38:04.872020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.874907Z","iopub.execute_input":"2025-02-15T12:38:04.875409Z","iopub.status.idle":"2025-02-15T12:38:04.903132Z","shell.execute_reply.started":"2025-02-15T12:38:04.875361Z","shell.execute_reply":"2025-02-15T12:38:04.901594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.904507Z","iopub.execute_input":"2025-02-15T12:38:04.904954Z","iopub.status.idle":"2025-02-15T12:38:04.929691Z","shell.execute_reply.started":"2025-02-15T12:38:04.904910Z","shell.execute_reply":"2025-02-15T12:38:04.928265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn  = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.931170Z","iopub.execute_input":"2025-02-15T12:38:04.931576Z","iopub.status.idle":"2025-02-15T12:38:04.963879Z","shell.execute_reply.started":"2025-02-15T12:38:04.931544Z","shell.execute_reply":"2025-02-15T12:38:04.962413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:04.965234Z","iopub.execute_input":"2025-02-15T12:38:04.965655Z","iopub.status.idle":"2025-02-15T12:38:05.187928Z","shell.execute_reply.started":"2025-02-15T12:38:04.965599Z","shell.execute_reply":"2025-02-15T12:38:05.186290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:05.189232Z","iopub.execute_input":"2025-02-15T12:38:05.189676Z","iopub.status.idle":"2025-02-15T12:38:22.652637Z","shell.execute_reply.started":"2025-02-15T12:38:05.189641Z","shell.execute_reply":"2025-02-15T12:38:22.651233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import  classification_report\n\n\nprint(classification_report(y_test , y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:22.654371Z","iopub.execute_input":"2025-02-15T12:38:22.654895Z","iopub.status.idle":"2025-02-15T12:38:22.694524Z","shell.execute_reply.started":"2025-02-15T12:38:22.654847Z","shell.execute_reply":"2025-02-15T12:38:22.692266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:22.696245Z","iopub.execute_input":"2025-02-15T12:38:22.696667Z","iopub.status.idle":"2025-02-15T12:38:22.703198Z","shell.execute_reply.started":"2025-02-15T12:38:22.696632Z","shell.execute_reply":"2025-02-15T12:38:22.700894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lr = LogisticRegression()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:22.704692Z","iopub.execute_input":"2025-02-15T12:38:22.705242Z","iopub.status.idle":"2025-02-15T12:38:22.737701Z","shell.execute_reply.started":"2025-02-15T12:38:22.705200Z","shell.execute_reply":"2025-02-15T12:38:22.735801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lr.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:22.739156Z","iopub.execute_input":"2025-02-15T12:38:22.739652Z","iopub.status.idle":"2025-02-15T12:38:31.846837Z","shell.execute_reply.started":"2025-02-15T12:38:22.739602Z","shell.execute_reply":"2025-02-15T12:38:31.845298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = lr.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:31.847955Z","iopub.execute_input":"2025-02-15T12:38:31.848429Z","iopub.status.idle":"2025-02-15T12:38:31.907280Z","shell.execute_reply.started":"2025-02-15T12:38:31.848391Z","shell.execute_reply":"2025-02-15T12:38:31.905720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:31.908591Z","iopub.execute_input":"2025-02-15T12:38:31.909015Z","iopub.status.idle":"2025-02-15T12:38:31.917625Z","shell.execute_reply.started":"2025-02-15T12:38:31.908973Z","shell.execute_reply":"2025-02-15T12:38:31.915593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(classification_report(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:31.919371Z","iopub.execute_input":"2025-02-15T12:38:31.919885Z","iopub.status.idle":"2025-02-15T12:38:31.974489Z","shell.execute_reply.started":"2025-02-15T12:38:31.919840Z","shell.execute_reply":"2025-02-15T12:38:31.972957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:31.976022Z","iopub.execute_input":"2025-02-15T12:38:31.976543Z","iopub.status.idle":"2025-02-15T12:38:31.982951Z","shell.execute_reply.started":"2025-02-15T12:38:31.976493Z","shell.execute_reply":"2025-02-15T12:38:31.981156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rf = RandomForestClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:31.988527Z","iopub.execute_input":"2025-02-15T12:38:31.989007Z","iopub.status.idle":"2025-02-15T12:38:32.016989Z","shell.execute_reply.started":"2025-02-15T12:38:31.988969Z","shell.execute_reply":"2025-02-15T12:38:32.014945Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rf.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:38:32.020030Z","iopub.execute_input":"2025-02-15T12:38:32.020457Z","iopub.status.idle":"2025-02-15T12:39:01.526472Z","shell.execute_reply.started":"2025-02-15T12:38:32.020426Z","shell.execute_reply":"2025-02-15T12:39:01.524685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred  = rf.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:01.527869Z","iopub.execute_input":"2025-02-15T12:39:01.528427Z","iopub.status.idle":"2025-02-15T12:39:01.991033Z","shell.execute_reply.started":"2025-02-15T12:39:01.528386Z","shell.execute_reply":"2025-02-15T12:39:01.989331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(classification_report(y_test , y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:01.992511Z","iopub.execute_input":"2025-02-15T12:39:01.993113Z","iopub.status.idle":"2025-02-15T12:39:02.036500Z","shell.execute_reply.started":"2025-02-15T12:39:01.993064Z","shell.execute_reply":"2025-02-15T12:39:02.034658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import AdaBoostClassifier , GradientBoostingClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:02.038626Z","iopub.execute_input":"2025-02-15T12:39:02.039184Z","iopub.status.idle":"2025-02-15T12:39:02.045879Z","shell.execute_reply.started":"2025-02-15T12:39:02.039135Z","shell.execute_reply":"2025-02-15T12:39:02.044464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ada = AdaBoostClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:02.047604Z","iopub.execute_input":"2025-02-15T12:39:02.048151Z","iopub.status.idle":"2025-02-15T12:39:02.076017Z","shell.execute_reply.started":"2025-02-15T12:39:02.048104Z","shell.execute_reply":"2025-02-15T12:39:02.074322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ada.fit(X_train , y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:02.077107Z","iopub.execute_input":"2025-02-15T12:39:02.077572Z","iopub.status.idle":"2025-02-15T12:39:38.166160Z","shell.execute_reply.started":"2025-02-15T12:39:02.077530Z","shell.execute_reply":"2025-02-15T12:39:38.164806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = ada.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:38.167465Z","iopub.execute_input":"2025-02-15T12:39:38.167839Z","iopub.status.idle":"2025-02-15T12:39:39.009837Z","shell.execute_reply.started":"2025-02-15T12:39:38.167811Z","shell.execute_reply":"2025-02-15T12:39:39.008886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(classification_report(y_test , y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:39.010703Z","iopub.execute_input":"2025-02-15T12:39:39.011104Z","iopub.status.idle":"2025-02-15T12:39:39.047376Z","shell.execute_reply.started":"2025-02-15T12:39:39.011068Z","shell.execute_reply":"2025-02-15T12:39:39.046117Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Applying PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:39.048615Z","iopub.execute_input":"2025-02-15T12:39:39.049019Z","iopub.status.idle":"2025-02-15T12:39:39.054670Z","shell.execute_reply.started":"2025-02-15T12:39:39.048988Z","shell.execute_reply":"2025-02-15T12:39:39.053310Z"}},"outputs":[],"execution_count":null},{"cell_type":"raw","source":"X_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:39.055937Z","iopub.execute_input":"2025-02-15T12:39:39.056328Z","iopub.status.idle":"2025-02-15T12:39:39.077605Z","shell.execute_reply.started":"2025-02-15T12:39:39.056297Z","shell.execute_reply":"2025-02-15T12:39:39.075950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = 500)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:39.078824Z","iopub.execute_input":"2025-02-15T12:39:39.079179Z","iopub.status.idle":"2025-02-15T12:39:39.101229Z","shell.execute_reply.started":"2025-02-15T12:39:39.079142Z","shell.execute_reply":"2025-02-15T12:39:39.099790Z"}},"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":"2025-02-15T12:39:39.102407Z","iopub.execute_input":"2025-02-15T12:39:39.102847Z","iopub.status.idle":"2025-02-15T12:39:49.905275Z","shell.execute_reply.started":"2025-02-15T12:39:39.102805Z","shell.execute_reply":"2025-02-15T12:39:49.903976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:49.906632Z","iopub.execute_input":"2025-02-15T12:39:49.907030Z","iopub.status.idle":"2025-02-15T12:39:49.914811Z","shell.execute_reply.started":"2025-02-15T12:39:49.906998Z","shell.execute_reply":"2025-02-15T12:39:49.913398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:49.916066Z","iopub.execute_input":"2025-02-15T12:39:49.916458Z","iopub.status.idle":"2025-02-15T12:39:50.018205Z","shell.execute_reply.started":"2025-02-15T12:39:49.916422Z","shell.execute_reply":"2025-02-15T12:39:50.016997Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:50.019166Z","iopub.execute_input":"2025-02-15T12:39:50.019533Z","iopub.status.idle":"2025-02-15T12:39:57.506656Z","shell.execute_reply.started":"2025-02-15T12:39:50.019494Z","shell.execute_reply":"2025-02-15T12:39:57.505212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:57.508038Z","iopub.execute_input":"2025-02-15T12:39:57.508427Z","iopub.status.idle":"2025-02-15T12:39:57.516896Z","shell.execute_reply.started":"2025-02-15T12:39:57.508396Z","shell.execute_reply":"2025-02-15T12:39:57.514926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(classification_report(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:57.517811Z","iopub.execute_input":"2025-02-15T12:39:57.518209Z","iopub.status.idle":"2025-02-15T12:39:57.581748Z","shell.execute_reply.started":"2025-02-15T12:39:57.518177Z","shell.execute_reply":"2025-02-15T12:39:57.580232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rf.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:39:57.583138Z","iopub.execute_input":"2025-02-15T12:39:57.583554Z","iopub.status.idle":"2025-02-15T12:42:32.896242Z","shell.execute_reply.started":"2025-02-15T12:39:57.583523Z","shell.execute_reply":"2025-02-15T12:42:32.894664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = rf.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:42:32.897589Z","iopub.execute_input":"2025-02-15T12:42:32.897985Z","iopub.status.idle":"2025-02-15T12:42:33.369742Z","shell.execute_reply.started":"2025-02-15T12:42:32.897954Z","shell.execute_reply":"2025-02-15T12:42:33.368182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(classification_report(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:42:33.371040Z","iopub.execute_input":"2025-02-15T12:42:33.371433Z","iopub.status.idle":"2025-02-15T12:42:33.410483Z","shell.execute_reply.started":"2025-02-15T12:42:33.371400Z","shell.execute_reply":"2025-02-15T12:42:33.408346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test_trf[2].reshape(1,-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:42:33.411631Z","iopub.execute_input":"2025-02-15T12:42:33.412099Z","iopub.status.idle":"2025-02-15T12:42:33.427204Z","shell.execute_reply.started":"2025-02-15T12:42:33.412060Z","shell.execute_reply":"2025-02-15T12:42:33.425675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction = knn.predict(X_test_trf[2].reshape(1, -1))[0]\nprint(f\"Predicted Label: {prediction}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:42:33.429313Z","iopub.execute_input":"2025-02-15T12:42:33.429760Z","iopub.status.idle":"2025-02-15T12:42:33.480429Z","shell.execute_reply.started":"2025-02-15T12:42:33.429713Z","shell.execute_reply":"2025-02-15T12:42:33.478490Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reconstruct the image from the PCA components\nreconstructed = pca.inverse_transform(X_test_trf[2]).reshape(28, 28)\n\nplt.imshow(reconstructed, cmap='gray')\nplt.title(f\"Predicted Label: {prediction}\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:42:33.483407Z","iopub.execute_input":"2025-02-15T12:42:33.483848Z","iopub.status.idle":"2025-02-15T12:42:33.856243Z","shell.execute_reply.started":"2025-02-15T12:42:33.483809Z","shell.execute_reply":"2025-02-15T12:42:33.853917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom sklearn.decomposition import PCA\nfrom sklearn.neighbors import KNeighborsClassifier\nimport matplotlib.pyplot as plt\n\n# Step 1: Load the Input Image\ninput_path = \"/kaggle/input/imagee/download.png\"  # Update with your image path\nimg = cv2.imread(input_path, cv2.IMREAD_GRAYSCALE)\n\n# Step 2: Preprocess the Image\nimg_resized = cv2.resize(img, (28, 28))  \nimg_flattened = img_resized.flatten().reshape(1, -1)  # Flatten to (1, 784)\nimg_scaled = img_flattened / 255.0  # Normalize pixel values to [0, 1]\n\n# Step 3: Apply PCA (Assuming PCA is already fitted)\nimg_pca = pca.transform(img_scaled)  # Reduce to 10 components\n\n# Step 4: Predict with KNN\nprediction = knn.predict(img_pca)[0]\n\n# Step 5: Display the Image with the Prediction\nplt.imshow(img_resized, cmap='gray')\nplt.title(f\"Predicted Label: {prediction}\")\nplt.axis('off')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T12:44:35.808586Z","iopub.execute_input":"2025-02-15T12:44:35.809078Z","iopub.status.idle":"2025-02-15T12:44:35.956527Z","shell.execute_reply.started":"2025-02-15T12:44:35.809038Z","shell.execute_reply":"2025-02-15T12:44:35.954985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}