{"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":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":"2026-03-25T03:28:08.736398Z","iopub.execute_input":"2026-03-25T03:28:08.736708Z","iopub.status.idle":"2026-03-25T03:28:08.744275Z","shell.execute_reply.started":"2026-03-25T03:28:08.736677Z","shell.execute_reply":"2026-03-25T03:28:08.743363Z"}},"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-03-25T03:34:46.383326Z","iopub.execute_input":"2026-03-25T03:34:46.383687Z","iopub.status.idle":"2026-03-25T03:34:48.512289Z","shell.execute_reply.started":"2026-03-25T03:34:46.38366Z","shell.execute_reply":"2026-03-25T03:34:48.511349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:34:49.915219Z","iopub.execute_input":"2026-03-25T03:34:49.915565Z","iopub.status.idle":"2026-03-25T03:34:49.929074Z","shell.execute_reply.started":"2026-03-25T03:34:49.915533Z","shell.execute_reply":"2026-03-25T03:34:49.928258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:10.839129Z","iopub.execute_input":"2026-03-25T03:28:10.839497Z","iopub.status.idle":"2026-03-25T03:28:10.854663Z","shell.execute_reply.started":"2026-03-25T03:28:10.83946Z","shell.execute_reply":"2026-03-25T03:28:10.853954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:10.856979Z","iopub.execute_input":"2026-03-25T03:28:10.857266Z","iopub.status.idle":"2026-03-25T03:28:10.879782Z","shell.execute_reply.started":"2026-03-25T03:28:10.857238Z","shell.execute_reply":"2026-03-25T03:28:10.879024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:10.880755Z","iopub.execute_input":"2026-03-25T03:28:10.881147Z","iopub.status.idle":"2026-03-25T03:28:10.893705Z","shell.execute_reply.started":"2026-03-25T03:28:10.88112Z","shell.execute_reply":"2026-03-25T03:28:10.892734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[9417, 1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:10.894788Z","iopub.execute_input":"2026-03-25T03:28:10.895127Z","iopub.status.idle":"2026-03-25T03:28:11.036473Z","shell.execute_reply.started":"2026-03-25T03:28:10.895098Z","shell.execute_reply":"2026-03-25T03:28:11.035557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:59:43.313598Z","iopub.execute_input":"2026-03-25T03:59:43.314519Z","iopub.status.idle":"2026-03-25T03:59:43.319051Z","shell.execute_reply.started":"2026-03-25T03:59:43.31448Z","shell.execute_reply":"2026-03-25T03:59:43.318337Z"}},"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-03-25T03:59:45.101871Z","iopub.execute_input":"2026-03-25T03:59:45.102892Z","iopub.status.idle":"2026-03-25T03:59:45.325249Z","shell.execute_reply.started":"2026-03-25T03:59:45.102842Z","shell.execute_reply":"2026-03-25T03:59:45.324432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:11.268236Z","iopub.execute_input":"2026-03-25T03:28:11.268574Z","iopub.status.idle":"2026-03-25T03:28:11.2743Z","shell.execute_reply.started":"2026-03-25T03:28:11.268532Z","shell.execute_reply":"2026-03-25T03:28:11.273417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:11.27692Z","iopub.execute_input":"2026-03-25T03:28:11.277481Z","iopub.status.idle":"2026-03-25T03:28:11.290247Z","shell.execute_reply.started":"2026-03-25T03:28:11.277451Z","shell.execute_reply":"2026-03-25T03:28:11.289353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:11.291436Z","iopub.execute_input":"2026-03-25T03:28:11.291785Z","iopub.status.idle":"2026-03-25T03:28:11.308068Z","shell.execute_reply.started":"2026-03-25T03:28:11.291756Z","shell.execute_reply":"2026-03-25T03:28:11.307243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:11.309091Z","iopub.execute_input":"2026-03-25T03:28:11.309474Z","iopub.status.idle":"2026-03-25T03:28:11.440033Z","shell.execute_reply.started":"2026-03-25T03:28:11.309439Z","shell.execute_reply":"2026-03-25T03:28:11.43932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:11.441085Z","iopub.execute_input":"2026-03-25T03:28:11.441392Z","iopub.status.idle":"2026-03-25T03:28:20.814963Z","shell.execute_reply.started":"2026-03-25T03:28:11.441354Z","shell.execute_reply":"2026-03-25T03:28:20.813894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:20.81623Z","iopub.execute_input":"2026-03-25T03:28:20.816593Z","iopub.status.idle":"2026-03-25T03:28:20.821144Z","shell.execute_reply.started":"2026-03-25T03:28:20.816553Z","shell.execute_reply":"2026-03-25T03:28:20.820328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:20.82219Z","iopub.execute_input":"2026-03-25T03:28:20.822488Z","iopub.status.idle":"2026-03-25T03:28:20.845717Z","shell.execute_reply.started":"2026-03-25T03:28:20.82246Z","shell.execute_reply":"2026-03-25T03:28:20.844857Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Apply PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:20.846907Z","iopub.execute_input":"2026-03-25T03:28:20.84733Z","iopub.status.idle":"2026-03-25T03:28:20.864439Z","shell.execute_reply.started":"2026-03-25T03:28:20.84729Z","shell.execute_reply":"2026-03-25T03:28:20.863384Z"}},"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-03-25T03:28:20.865697Z","iopub.execute_input":"2026-03-25T03:28:20.866045Z","iopub.status.idle":"2026-03-25T03:28:21.337256Z","shell.execute_reply.started":"2026-03-25T03:28:20.865982Z","shell.execute_reply":"2026-03-25T03:28:21.336173Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components = 100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:21.33858Z","iopub.execute_input":"2026-03-25T03:28:21.33933Z","iopub.status.idle":"2026-03-25T03:28:21.343304Z","shell.execute_reply.started":"2026-03-25T03:28:21.339296Z","shell.execute_reply":"2026-03-25T03:28:21.342413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train = pca.fit_transform(x_train)\nx_test = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:21.344429Z","iopub.execute_input":"2026-03-25T03:28:21.344745Z","iopub.status.idle":"2026-03-25T03:28:22.023656Z","shell.execute_reply.started":"2026-03-25T03:28:21.344716Z","shell.execute_reply":"2026-03-25T03:28:22.022905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:22.024488Z","iopub.execute_input":"2026-03-25T03:28:22.024778Z","iopub.status.idle":"2026-03-25T03:28:22.030793Z","shell.execute_reply.started":"2026-03-25T03:28:22.024747Z","shell.execute_reply":"2026-03-25T03:28:22.030001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:22.032423Z","iopub.execute_input":"2026-03-25T03:28:22.032734Z","iopub.status.idle":"2026-03-25T03:28:22.049003Z","shell.execute_reply.started":"2026-03-25T03:28:22.032701Z","shell.execute_reply":"2026-03-25T03:28:22.048241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:22.050478Z","iopub.execute_input":"2026-03-25T03:28:22.051034Z","iopub.status.idle":"2026-03-25T03:28:22.07772Z","shell.execute_reply.started":"2026-03-25T03:28:22.050993Z","shell.execute_reply":"2026-03-25T03:28:22.076843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:22.079856Z","iopub.execute_input":"2026-03-25T03:28:22.08238Z","iopub.status.idle":"2026-03-25T03:28:23.520593Z","shell.execute_reply.started":"2026-03-25T03:28:22.082338Z","shell.execute_reply":"2026-03-25T03:28:23.519603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:28:23.522045Z","iopub.execute_input":"2026-03-25T03:28:23.522526Z","iopub.status.idle":"2026-03-25T03:28:23.530331Z","shell.execute_reply.started":"2026-03-25T03:28:23.522483Z","shell.execute_reply":"2026-03-25T03:28:23.529355Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# visualization","metadata":{}},{"cell_type":"code","source":"pca = PCA(n_components = 2)\nx_train = pca.fit_transform(x_train)\nx_test = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:35:09.400306Z","iopub.execute_input":"2026-03-25T03:35:09.40063Z","iopub.status.idle":"2026-03-25T03:35:10.067417Z","shell.execute_reply.started":"2026-03-25T03:35:09.4006Z","shell.execute_reply":"2026-03-25T03:35:10.066653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:35:16.847854Z","iopub.execute_input":"2026-03-25T03:35:16.848218Z","iopub.status.idle":"2026-03-25T03:35:16.85401Z","shell.execute_reply.started":"2026-03-25T03:35:16.848187Z","shell.execute_reply":"2026-03-25T03:35:16.852973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train = y_train.astype(str)\nfig = px.scatter(x=x_train[:,0],\n                y=x_train[:,1],\n                color=y_train,\n                color_discrete_sequence=px.colors.qualitative.G10)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:38:00.616964Z","iopub.execute_input":"2026-03-25T03:38:00.617324Z","iopub.status.idle":"2026-03-25T03:38:06.119881Z","shell.execute_reply.started":"2026-03-25T03:38:00.617296Z","shell.execute_reply":"2026-03-25T03:38:06.118596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming in 3D\npca = PCA(n_components = 3)\nx_train = pca.fit_transform(x_train)\nx_test = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:49:11.217736Z","iopub.execute_input":"2026-03-25T03:49:11.218189Z","iopub.status.idle":"2026-03-25T03:49:11.227376Z","shell.execute_reply.started":"2026-03-25T03:49:11.218153Z","shell.execute_reply":"2026-03-25T03:49:11.226095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:49:13.40638Z","iopub.execute_input":"2026-03-25T03:49:13.406722Z","iopub.status.idle":"2026-03-25T03:49:13.412749Z","shell.execute_reply.started":"2026-03-25T03:49:13.406691Z","shell.execute_reply":"2026-03-25T03:49:13.411915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = y_train.astype(str)\nfig = px.scatter_3d(df, x=x_train[:,0], y=x_train[:,1], z=x_train[:,2],color=y_train)\nfig.update_layout(\n    margin=dict(l=20, r=20, t=20, b=20)\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:49:15.671642Z","iopub.execute_input":"2026-03-25T03:49:15.671986Z","iopub.status.idle":"2026-03-25T03:49:15.768897Z","shell.execute_reply.started":"2026-03-25T03:49:15.671954Z","shell.execute_reply":"2026-03-25T03:49:15.767881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# this is EigenValues\npca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:49:29.773135Z","iopub.execute_input":"2026-03-25T03:49:29.773563Z","iopub.status.idle":"2026-03-25T03:49:29.78065Z","shell.execute_reply.started":"2026-03-25T03:49:29.773519Z","shell.execute_reply":"2026-03-25T03:49:29.77965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# this is EigenVectors\npca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:49:53.852247Z","iopub.execute_input":"2026-03-25T03:49:53.852595Z","iopub.status.idle":"2026-03-25T03:49:53.859049Z","shell.execute_reply.started":"2026-03-25T03:49:53.852565Z","shell.execute_reply":"2026-03-25T03:49:53.857779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = None)\nx_train = pca.fit_transform(x_train)\nx_test = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:59:52.470052Z","iopub.execute_input":"2026-03-25T03:59:52.47037Z","iopub.status.idle":"2026-03-25T03:59:53.896761Z","shell.execute_reply.started":"2026-03-25T03:59:52.470343Z","shell.execute_reply":"2026-03-25T03:59:53.896051Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T03:59:56.232854Z","iopub.execute_input":"2026-03-25T03:59:56.233272Z","iopub.status.idle":"2026-03-25T03:59:56.245702Z","shell.execute_reply.started":"2026-03-25T03:59:56.233233Z","shell.execute_reply":"2026-03-25T03:59:56.244728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T04:00:02.007852Z","iopub.execute_input":"2026-03-25T04:00:02.008207Z","iopub.status.idle":"2026-03-25T04:00:02.017687Z","shell.execute_reply.started":"2026-03-25T04:00:02.008176Z","shell.execute_reply":"2026-03-25T04:00:02.016101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T04:01:59.819085Z","iopub.execute_input":"2026-03-25T04:01:59.820622Z","iopub.status.idle":"2026-03-25T04:01:59.834389Z","shell.execute_reply.started":"2026-03-25T04:01:59.82057Z","shell.execute_reply":"2026-03-25T04:01:59.833087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T04:02:15.262241Z","iopub.execute_input":"2026-03-25T04:02:15.263781Z","iopub.status.idle":"2026-03-25T04:02:15.278366Z","shell.execute_reply.started":"2026-03-25T04:02:15.263725Z","shell.execute_reply":"2026-03-25T04:02:15.277239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T04:03:00.970856Z","iopub.execute_input":"2026-03-25T04:03:00.971204Z","iopub.status.idle":"2026-03-25T04:03:01.114797Z","shell.execute_reply.started":"2026-03-25T04:03:00.971171Z","shell.execute_reply":"2026-03-25T04:03:01.113202Z"}},"outputs":[],"execution_count":null}]}