{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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,"sourceType":"competition"}],"dockerImageVersionId":31040,"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:31.816886Z","iopub.execute_input":"2025-06-30T05:24:31.817189Z","iopub.status.idle":"2025-06-30T05:24:31.825909Z","shell.execute_reply.started":"2025-06-30T05:24:31.817167Z","shell.execute_reply":"2025-06-30T05:24:31.824823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:35.196351Z","iopub.execute_input":"2025-06-30T05:24:35.196716Z","iopub.status.idle":"2025-06-30T05:24:37.403911Z","shell.execute_reply.started":"2025-06-30T05:24:35.196672Z","shell.execute_reply":"2025-06-30T05:24:37.403001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:38.876806Z","iopub.execute_input":"2025-06-30T05:24:38.877077Z","iopub.status.idle":"2025-06-30T05:24:38.892364Z","shell.execute_reply.started":"2025-06-30T05:24:38.877059Z","shell.execute_reply":"2025-06-30T05:24:38.891264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:39.771811Z","iopub.execute_input":"2025-06-30T05:24:39.772121Z","iopub.status.idle":"2025-06-30T05:24:39.777633Z","shell.execute_reply.started":"2025-06-30T05:24:39.772099Z","shell.execute_reply":"2025-06-30T05:24:39.776728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:42.236993Z","iopub.execute_input":"2025-06-30T05:24:42.237264Z","iopub.status.idle":"2025-06-30T05:24:42.253901Z","shell.execute_reply.started":"2025-06-30T05:24:42.237243Z","shell.execute_reply":"2025-06-30T05:24:42.252898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:42.691775Z","iopub.execute_input":"2025-06-30T05:24:42.692073Z","iopub.status.idle":"2025-06-30T05:24:42.696905Z","shell.execute_reply.started":"2025-06-30T05:24:42.692052Z","shell.execute_reply":"2025-06-30T05:24:42.695898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[35200,1:].values.reshape(28,28),cmap=\"rainbow\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:43.056830Z","iopub.execute_input":"2025-06-30T05:24:43.057102Z","iopub.status.idle":"2025-06-30T05:24:43.233420Z","shell.execute_reply.started":"2025-06-30T05:24:43.057084Z","shell.execute_reply":"2025-06-30T05:24:43.232471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=df.iloc[:,1:]\ny=df.iloc[:,:1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:43.371997Z","iopub.execute_input":"2025-06-30T05:24:43.372285Z","iopub.status.idle":"2025-06-30T05:24:43.378447Z","shell.execute_reply.started":"2025-06-30T05:24:43.372264Z","shell.execute_reply":"2025-06-30T05:24:43.376835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:43.701907Z","iopub.execute_input":"2025-06-30T05:24:43.702215Z","iopub.status.idle":"2025-06-30T05:24:43.721497Z","shell.execute_reply.started":"2025-06-30T05:24:43.702191Z","shell.execute_reply":"2025-06-30T05:24:43.720308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:45.411447Z","iopub.execute_input":"2025-06-30T05:24:45.411752Z","iopub.status.idle":"2025-06-30T05:24:45.420806Z","shell.execute_reply.started":"2025-06-30T05:24:45.411720Z","shell.execute_reply":"2025-06-30T05:24:45.419705Z"}},"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":"2025-06-30T05:24:45.661920Z","iopub.execute_input":"2025-06-30T05:24:45.662224Z","iopub.status.idle":"2025-06-30T05:24:45.903711Z","shell.execute_reply.started":"2025-06-30T05:24:45.662202Z","shell.execute_reply":"2025-06-30T05:24:45.902757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:45.956614Z","iopub.execute_input":"2025-06-30T05:24:45.956907Z","iopub.status.idle":"2025-06-30T05:24:45.962653Z","shell.execute_reply.started":"2025-06-30T05:24:45.956886Z","shell.execute_reply":"2025-06-30T05:24:45.961657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:46.191464Z","iopub.execute_input":"2025-06-30T05:24:46.191749Z","iopub.status.idle":"2025-06-30T05:24:46.196079Z","shell.execute_reply.started":"2025-06-30T05:24:46.191728Z","shell.execute_reply":"2025-06-30T05:24:46.195128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:46.476939Z","iopub.execute_input":"2025-06-30T05:24:46.477226Z","iopub.status.idle":"2025-06-30T05:24:46.481763Z","shell.execute_reply.started":"2025-06-30T05:24:46.477206Z","shell.execute_reply":"2025-06-30T05:24:46.480590Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:46.706739Z","iopub.execute_input":"2025-06-30T05:24:46.707031Z","iopub.status.idle":"2025-06-30T05:24:46.836032Z","shell.execute_reply.started":"2025-06-30T05:24:46.707006Z","shell.execute_reply":"2025-06-30T05:24:46.835138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:46.966692Z","iopub.execute_input":"2025-06-30T05:24:46.967009Z","iopub.status.idle":"2025-06-30T05:24:59.525156Z","shell.execute_reply.started":"2025-06-30T05:24:46.966986Z","shell.execute_reply":"2025-06-30T05:24:59.523977Z"}},"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":"2025-06-30T05:24:59.526623Z","iopub.execute_input":"2025-06-30T05:24:59.526954Z","iopub.status.idle":"2025-06-30T05:24:59.537903Z","shell.execute_reply.started":"2025-06-30T05:24:59.526925Z","shell.execute_reply":"2025-06-30T05:24:59.536786Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:24:59.538874Z","iopub.execute_input":"2025-06-30T05:24:59.539199Z","iopub.status.idle":"2025-06-30T05:24:59.557365Z","shell.execute_reply.started":"2025-06-30T05:24:59.539170Z","shell.execute_reply":"2025-06-30T05:24:59.556480Z"}},"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":"2025-06-30T05:24:59.558978Z","iopub.execute_input":"2025-06-30T05:24:59.559685Z","iopub.status.idle":"2025-06-30T05:25:00.150336Z","shell.execute_reply.started":"2025-06-30T05:24:59.559660Z","shell.execute_reply":"2025-06-30T05:25:00.149142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:04.196629Z","iopub.execute_input":"2025-06-30T05:25:04.196923Z","iopub.status.idle":"2025-06-30T05:25:04.201623Z","shell.execute_reply.started":"2025-06-30T05:25:04.196903Z","shell.execute_reply":"2025-06-30T05:25:04.200452Z"}},"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-06-30T05:25:04.901597Z","iopub.execute_input":"2025-06-30T05:25:04.901997Z","iopub.status.idle":"2025-06-30T05:25:10.028291Z","shell.execute_reply.started":"2025-06-30T05:25:04.901970Z","shell.execute_reply":"2025-06-30T05:25:10.027566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:10.029669Z","iopub.execute_input":"2025-06-30T05:25:10.029906Z","iopub.status.idle":"2025-06-30T05:25:10.036583Z","shell.execute_reply.started":"2025-06-30T05:25:10.029889Z","shell.execute_reply":"2025-06-30T05:25:10.035525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:10.037628Z","iopub.execute_input":"2025-06-30T05:25:10.037911Z","iopub.status.idle":"2025-06-30T05:25:10.054212Z","shell.execute_reply.started":"2025-06-30T05:25:10.037891Z","shell.execute_reply":"2025-06-30T05:25:10.053330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:10.055715Z","iopub.execute_input":"2025-06-30T05:25:10.056046Z","iopub.status.idle":"2025-06-30T05:25:10.109836Z","shell.execute_reply.started":"2025-06-30T05:25:10.055992Z","shell.execute_reply":"2025-06-30T05:25:10.109020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:10.110751Z","iopub.execute_input":"2025-06-30T05:25:10.111086Z","iopub.status.idle":"2025-06-30T05:25:12.449990Z","shell.execute_reply.started":"2025-06-30T05:25:10.111065Z","shell.execute_reply":"2025-06-30T05:25:12.448991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:12.451237Z","iopub.execute_input":"2025-06-30T05:25:12.451716Z","iopub.status.idle":"2025-06-30T05:25:12.462865Z","shell.execute_reply.started":"2025-06-30T05:25:12.451684Z","shell.execute_reply":"2025-06-30T05:25:12.462017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca=PCA(n_components=2)\nX_train_trf=pca.fit_transform(X_train)\nX_train_trf=pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:12.464047Z","iopub.execute_input":"2025-06-30T05:25:12.464372Z","iopub.status.idle":"2025-06-30T05:25:13.271334Z","shell.execute_reply.started":"2025-06-30T05:25:12.464347Z","shell.execute_reply":"2025-06-30T05:25:13.270159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:13.273062Z","iopub.execute_input":"2025-06-30T05:25:13.273792Z","iopub.status.idle":"2025-06-30T05:25:13.280628Z","shell.execute_reply.started":"2025-06-30T05:25:13.273758Z","shell.execute_reply":"2025-06-30T05:25:13.279430Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:15.001946Z","iopub.execute_input":"2025-06-30T05:25:15.002237Z","iopub.status.idle":"2025-06-30T05:25:15.008255Z","shell.execute_reply.started":"2025-06-30T05:25:15.002216Z","shell.execute_reply":"2025-06-30T05:25:15.007083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\n\n# Fix shape mismatch and convert labels to string\ny_train_trf = y_train[:X_train_trf.shape[0]].values.ravel().astype(str)\n\n# Plot\nfig = px.scatter(\n    x=X_train_trf[:, 0],\n    y=X_train_trf[:, 1], \n    color=y_train_trf,\n    color_discrete_sequence=px.colors.qualitative.G10\n)\n\nfig.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T05:25:21.262508Z","iopub.execute_input":"2025-06-30T05:25:21.263222Z","iopub.status.idle":"2025-06-30T05:25:21.334896Z","shell.execute_reply.started":"2025-06-30T05:25:21.263197Z","shell.execute_reply":"2025-06-30T05:25:21.334026Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca=PCA(n_components=3)\nX_train_trf=pca.fit_transform(X_train)\nX_train_trf=pca.transform(X_test)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\n\n# Ensure labels are 1D and match X_train_trf length\ny_train_trf = y_train[:X_train_trf.shape[0]].values.ravel().astype(str)\n\n# Create 3D scatter plot with labels as color\nfig = px.scatter_3d(\n    x=X_train_trf[:, 0],\n    y=X_train_trf[:, 1],\n    z=X_train_trf[:, 2],\n    color=y_train_trf,\n    color_discrete_sequence=px.colors.qualitative.G10,\n    labels={'color': 'Digit'}\n)\n\n# Optional: tighten margins\nfig.update_layout(\n    margin=dict(l=20, r=20, t=20, b=20)\n)\n\nfig.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Eigen Values\npca.explained_variance_","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Eigen Vector\npca.components_","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca=PCA(n_components=None)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}