{"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-09-22T18:21:03.093693Z","iopub.execute_input":"2026-09-22T18:21:03.093903Z","iopub.status.idle":"2026-09-22T18:21:05.13655Z","shell.execute_reply.started":"2026-09-22T18:21:03.093878Z","shell.execute_reply":"2026-09-22T18:21:05.135685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:23:50.564667Z","iopub.execute_input":"2026-09-22T18:23:50.565116Z","iopub.status.idle":"2026-09-22T18:23:50.570062Z","shell.execute_reply.started":"2026-09-22T18:23:50.565049Z","shell.execute_reply":"2026-09-22T18:23:50.569222Z"}},"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-09-22T18:24:18.80882Z","iopub.execute_input":"2026-09-22T18:24:18.809188Z","iopub.status.idle":"2026-09-22T18:24:20.831223Z","shell.execute_reply.started":"2026-09-22T18:24:18.809161Z","shell.execute_reply":"2026-09-22T18:24:20.830492Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:24:20.832721Z","iopub.execute_input":"2026-09-22T18:24:20.833304Z","iopub.status.idle":"2026-09-22T18:24:20.848003Z","shell.execute_reply.started":"2026-09-22T18:24:20.833275Z","shell.execute_reply":"2026-09-22T18:24:20.847278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:24:24.888848Z","iopub.execute_input":"2026-09-22T18:24:24.88922Z","iopub.status.idle":"2026-09-22T18:24:24.904573Z","shell.execute_reply.started":"2026-09-22T18:24:24.889192Z","shell.execute_reply":"2026-09-22T18:24:24.903724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:24:25.308823Z","iopub.execute_input":"2026-09-22T18:24:25.309152Z","iopub.status.idle":"2026-09-22T18:24:25.3149Z","shell.execute_reply.started":"2026-09-22T18:24:25.309118Z","shell.execute_reply":"2026-09-22T18:24:25.314135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[11031, 1:].values.reshape(28, 28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:24:25.829062Z","iopub.execute_input":"2026-09-22T18:24:25.829422Z","iopub.status.idle":"2026-09-22T18:24:25.96643Z","shell.execute_reply.started":"2026-09-22T18:24:25.829397Z","shell.execute_reply":"2026-09-22T18:24:25.96549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[41107, 1:].values.reshape(28, 28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:24:26.524448Z","iopub.execute_input":"2026-09-22T18:24:26.524785Z","iopub.status.idle":"2026-09-22T18:24:26.66038Z","shell.execute_reply.started":"2026-09-22T18:24:26.524759Z","shell.execute_reply":"2026-09-22T18:24:26.659482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:, 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:10.975711Z","iopub.execute_input":"2026-09-22T18:36:10.976035Z","iopub.status.idle":"2026-09-22T18:36:10.981166Z","shell.execute_reply.started":"2026-09-22T18:36:10.976008Z","shell.execute_reply":"2026-09-22T18:36:10.980199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:11.641163Z","iopub.execute_input":"2026-09-22T18:36:11.641494Z","iopub.status.idle":"2026-09-22T18:36:11.645934Z","shell.execute_reply.started":"2026-09-22T18:36:11.641467Z","shell.execute_reply":"2026-09-22T18:36:11.645161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_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-09-22T18:36:12.819974Z","iopub.execute_input":"2026-09-22T18:36:12.820371Z","iopub.status.idle":"2026-09-22T18:36:12.977518Z","shell.execute_reply.started":"2026-09-22T18:36:12.820341Z","shell.execute_reply":"2026-09-22T18:36:12.976437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:13.593851Z","iopub.execute_input":"2026-09-22T18:36:13.594199Z","iopub.status.idle":"2026-09-22T18:36:13.600687Z","shell.execute_reply.started":"2026-09-22T18:36:13.594171Z","shell.execute_reply":"2026-09-22T18:36:13.599928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:14.33128Z","iopub.execute_input":"2026-09-22T18:36:14.332019Z","iopub.status.idle":"2026-09-22T18:36:14.33629Z","shell.execute_reply.started":"2026-09-22T18:36:14.331983Z","shell.execute_reply":"2026-09-22T18:36:14.335364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:14.966908Z","iopub.execute_input":"2026-09-22T18:36:14.967366Z","iopub.status.idle":"2026-09-22T18:36:14.97222Z","shell.execute_reply.started":"2026-09-22T18:36:14.967327Z","shell.execute_reply":"2026-09-22T18:36:14.971197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:15.60695Z","iopub.execute_input":"2026-09-22T18:36:15.607359Z","iopub.status.idle":"2026-09-22T18:36:15.726283Z","shell.execute_reply.started":"2026-09-22T18:36:15.607332Z","shell.execute_reply":"2026-09-22T18:36:15.725407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:18.669906Z","iopub.execute_input":"2026-09-22T18:36:18.670263Z","iopub.status.idle":"2026-09-22T18:36:29.807206Z","shell.execute_reply.started":"2026-09-22T18:36:18.670236Z","shell.execute_reply":"2026-09-22T18:36:29.806368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:29.808795Z","iopub.execute_input":"2026-09-22T18:36:29.809256Z","iopub.status.idle":"2026-09-22T18:36:29.81687Z","shell.execute_reply.started":"2026-09-22T18:36:29.809229Z","shell.execute_reply":"2026-09-22T18:36:29.816142Z"}},"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-09-22T18:36:29.817864Z","iopub.execute_input":"2026-09-22T18:36:29.818218Z","iopub.status.idle":"2026-09-22T18:36:29.833566Z","shell.execute_reply.started":"2026-09-22T18:36:29.818184Z","shell.execute_reply":"2026-09-22T18:36:29.832731Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**PCA**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:36:29.835414Z","iopub.execute_input":"2026-09-22T18:36:29.835669Z","iopub.status.idle":"2026-09-22T18:36:29.84578Z","shell.execute_reply.started":"2026-09-22T18:36:29.835647Z","shell.execute_reply":"2026-09-22T18:36:29.844855Z"}},"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-09-22T18:39:51.144931Z","iopub.execute_input":"2026-09-22T18:39:51.145627Z","iopub.status.idle":"2026-09-22T18:39:51.501031Z","shell.execute_reply.started":"2026-09-22T18:39:51.145595Z","shell.execute_reply":"2026-09-22T18:39:51.49995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:39:51.699845Z","iopub.execute_input":"2026-09-22T18:39:51.700664Z","iopub.status.idle":"2026-09-22T18:39:51.704293Z","shell.execute_reply.started":"2026-09-22T18:39:51.700632Z","shell.execute_reply":"2026-09-22T18:39:51.703551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:41:31.249933Z","iopub.execute_input":"2026-09-22T18:41:31.250302Z","iopub.status.idle":"2026-09-22T18:41:31.255363Z","shell.execute_reply.started":"2026-09-22T18:41:31.250275Z","shell.execute_reply":"2026-09-22T18:41:31.254502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_pca = pca.fit_transform(x_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:41:31.765894Z","iopub.execute_input":"2026-09-22T18:41:31.766261Z","iopub.status.idle":"2026-09-22T18:41:32.533729Z","shell.execute_reply.started":"2026-09-22T18:41:31.766235Z","shell.execute_reply":"2026-09-22T18:41:32.533015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test_pca = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:41:32.535422Z","iopub.execute_input":"2026-09-22T18:41:32.535728Z","iopub.status.idle":"2026-09-22T18:41:32.584867Z","shell.execute_reply.started":"2026-09-22T18:41:32.535699Z","shell.execute_reply":"2026-09-22T18:41:32.584112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_pca.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:41:33.211237Z","iopub.execute_input":"2026-09-22T18:41:33.211577Z","iopub.status.idle":"2026-09-22T18:41:33.218629Z","shell.execute_reply.started":"2026-09-22T18:41:33.211549Z","shell.execute_reply":"2026-09-22T18:41:33.21781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\nimport matplotlib.pyplot as plt\n\n# Scaling\nscaler = StandardScaler()\n\nx_train = scaler.fit_transform(x_train)\nx_test = scaler.transform(x_test)\n\n# PCA\npca = PCA(n_components=2)\n\nx_train_pca = pca.fit_transform(x_train)\nx_test_pca = pca.transform(x_test)\n\n# Explained variance\nprint(\"Explained variance ratio:\",\n      pca.explained_variance_ratio_)\n\nprint(\"Total variance retained:\",\n      pca.explained_variance_ratio_.sum())\n\n# Shape\nprint(\"Original training shape:\", x_train.shape)\nprint(\"PCA training shape:\", x_train_pca.shape)\n\n# Visualization\nplt.scatter(\n    x_train_pca[:, 0],\n    x_train_pca[:, 1],\n    c=y_train\n)\n\nplt.xlabel(\"Principal Component 1\")\nplt.ylabel(\"Principal Component 2\")\nplt.title(\"PCA Visualization\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-22T18:43:32.120854Z","iopub.execute_input":"2026-09-22T18:43:32.121723Z","iopub.status.idle":"2026-09-22T18:43:34.09255Z","shell.execute_reply.started":"2026-09-22T18:43:32.12167Z","shell.execute_reply":"2026-09-22T18:43:34.091609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}