{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31089,"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-08-09T13:53:21.237937Z","iopub.execute_input":"2025-08-09T13:53:21.238317Z","iopub.status.idle":"2025-08-09T13:53:21.245919Z","shell.execute_reply.started":"2025-08-09T13:53:21.238281Z","shell.execute_reply":"2025-08-09T13:53:21.245144Z"}},"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-08-09T13:53:21.24724Z","iopub.execute_input":"2025-08-09T13:53:21.247497Z","iopub.status.idle":"2025-08-09T13:53:23.419461Z","shell.execute_reply.started":"2025-08-09T13:53:21.24747Z","shell.execute_reply":"2025-08-09T13:53:23.418441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.42101Z","iopub.execute_input":"2025-08-09T13:53:23.421351Z","iopub.status.idle":"2025-08-09T13:53:23.435617Z","shell.execute_reply.started":"2025-08-09T13:53:23.421329Z","shell.execute_reply":"2025-08-09T13:53:23.434708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.43665Z","iopub.execute_input":"2025-08-09T13:53:23.436995Z","iopub.status.idle":"2025-08-09T13:53:23.454198Z","shell.execute_reply.started":"2025-08-09T13:53:23.436966Z","shell.execute_reply":"2025-08-09T13:53:23.453227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# here label does work as output data ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.456238Z","iopub.execute_input":"2025-08-09T13:53:23.456542Z","iopub.status.idle":"2025-08-09T13:53:23.470156Z","shell.execute_reply.started":"2025-08-09T13:53:23.456517Z","shell.execute_reply":"2025-08-09T13:53:23.46921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.471739Z","iopub.execute_input":"2025-08-09T13:53:23.472045Z","iopub.status.idle":"2025-08-09T13:53:23.487511Z","shell.execute_reply.started":"2025-08-09T13:53:23.472018Z","shell.execute_reply":"2025-08-09T13:53:23.486591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.488514Z","iopub.execute_input":"2025-08-09T13:53:23.48895Z","iopub.status.idle":"2025-08-09T13:53:23.671114Z","shell.execute_reply.started":"2025-08-09T13:53:23.488915Z","shell.execute_reply":"2025-08-09T13:53:23.670289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=df.iloc[:,1:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.672177Z","iopub.execute_input":"2025-08-09T13:53:23.672593Z","iopub.status.idle":"2025-08-09T13:53:23.677055Z","shell.execute_reply.started":"2025-08-09T13:53:23.672566Z","shell.execute_reply":"2025-08-09T13:53:23.676218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y=df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.678073Z","iopub.execute_input":"2025-08-09T13:53:23.678417Z","iopub.status.idle":"2025-08-09T13:53:23.697446Z","shell.execute_reply.started":"2025-08-09T13:53:23.678395Z","shell.execute_reply":"2025-08-09T13:53:23.696417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.69857Z","iopub.execute_input":"2025-08-09T13:53:23.698938Z","iopub.status.idle":"2025-08-09T13:53:23.717308Z","shell.execute_reply.started":"2025-08-09T13:53:23.698911Z","shell.execute_reply":"2025-08-09T13:53:23.716258Z"}},"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":"2025-08-09T13:53:23.721556Z","iopub.execute_input":"2025-08-09T13:53:23.72263Z","iopub.status.idle":"2025-08-09T13:53:23.96946Z","shell.execute_reply.started":"2025-08-09T13:53:23.722606Z","shell.execute_reply":"2025-08-09T13:53:23.968456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.97057Z","iopub.execute_input":"2025-08-09T13:53:23.970899Z","iopub.status.idle":"2025-08-09T13:53:23.975913Z","shell.execute_reply.started":"2025-08-09T13:53:23.970868Z","shell.execute_reply":"2025-08-09T13:53:23.975238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.976807Z","iopub.execute_input":"2025-08-09T13:53:23.977734Z","iopub.status.idle":"2025-08-09T13:53:23.992944Z","shell.execute_reply.started":"2025-08-09T13:53:23.977697Z","shell.execute_reply":"2025-08-09T13:53:23.991917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:23.994043Z","iopub.execute_input":"2025-08-09T13:53:23.995024Z","iopub.status.idle":"2025-08-09T13:53:24.015159Z","shell.execute_reply.started":"2025-08-09T13:53:23.994994Z","shell.execute_reply":"2025-08-09T13:53:24.01411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:24.016087Z","iopub.execute_input":"2025-08-09T13:53:24.016473Z","iopub.status.idle":"2025-08-09T13:53:24.155898Z","shell.execute_reply.started":"2025-08-09T13:53:24.016384Z","shell.execute_reply":"2025-08-09T13:53:24.155085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import  time \nstart=time.time()\ny_pred=knn.predict(X_test)\nprint(time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:24.156773Z","iopub.execute_input":"2025-08-09T13:53:24.157025Z","iopub.status.idle":"2025-08-09T13:53:38.06183Z","shell.execute_reply.started":"2025-08-09T13:53:24.15699Z","shell.execute_reply":"2025-08-09T13:53:38.061023Z"}},"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-08-09T13:53:38.062676Z","iopub.execute_input":"2025-08-09T13:53:38.062899Z","iopub.status.idle":"2025-08-09T13:53:38.069835Z","shell.execute_reply.started":"2025-08-09T13:53:38.062882Z","shell.execute_reply":"2025-08-09T13:53:38.069125Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:38.070726Z","iopub.execute_input":"2025-08-09T13:53:38.071028Z","iopub.status.idle":"2025-08-09T13:53:38.086772Z","shell.execute_reply.started":"2025-08-09T13:53:38.071002Z","shell.execute_reply":"2025-08-09T13:53:38.08586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train=scaler.fit_transform(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:38.087903Z","iopub.execute_input":"2025-08-09T13:53:38.088221Z","iopub.status.idle":"2025-08-09T13:53:38.609167Z","shell.execute_reply.started":"2025-08-09T13:53:38.088192Z","shell.execute_reply":"2025-08-09T13:53:38.608449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test=scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:38.610023Z","iopub.execute_input":"2025-08-09T13:53:38.610315Z","iopub.status.idle":"2025-08-09T13:53:38.670036Z","shell.execute_reply.started":"2025-08-09T13:53:38.610285Z","shell.execute_reply":"2025-08-09T13:53:38.669128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:38.670965Z","iopub.execute_input":"2025-08-09T13:53:38.671253Z","iopub.status.idle":"2025-08-09T13:53:38.675477Z","shell.execute_reply.started":"2025-08-09T13:53:38.671232Z","shell.execute_reply":"2025-08-09T13:53:38.674529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf=pca.fit_transform(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:38.676393Z","iopub.execute_input":"2025-08-09T13:53:38.677016Z","iopub.status.idle":"2025-08-09T13:53:39.474485Z","shell.execute_reply.started":"2025-08-09T13:53:38.676985Z","shell.execute_reply":"2025-08-09T13:53:39.47369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test_trf=pca.transform(X_test) \npca=PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:39.475107Z","iopub.execute_input":"2025-08-09T13:53:39.475367Z","iopub.status.idle":"2025-08-09T13:53:39.535932Z","shell.execute_reply.started":"2025-08-09T13:53:39.475347Z","shell.execute_reply":"2025-08-09T13:53:39.535208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf. shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:39.536904Z","iopub.execute_input":"2025-08-09T13:53:39.5373Z","iopub.status.idle":"2025-08-09T13:53:39.541833Z","shell.execute_reply.started":"2025-08-09T13:53:39.537279Z","shell.execute_reply":"2025-08-09T13:53:39.540946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:39.542763Z","iopub.execute_input":"2025-08-09T13:53:39.542961Z","iopub.status.idle":"2025-08-09T13:53:39.591156Z","shell.execute_reply.started":"2025-08-09T13:53:39.542946Z","shell.execute_reply":"2025-08-09T13:53:39.590321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:39.59207Z","iopub.execute_input":"2025-08-09T13:53:39.592305Z","iopub.status.idle":"2025-08-09T13:53:39.979484Z","shell.execute_reply.started":"2025-08-09T13:53:39.592288Z","shell.execute_reply":"2025-08-09T13:53:39.978775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:39.980144Z","iopub.execute_input":"2025-08-09T13:53:39.980383Z","iopub.status.idle":"2025-08-09T13:53:39.987611Z","shell.execute_reply.started":"2025-08-09T13:53:39.980365Z","shell.execute_reply":"2025-08-09T13:53:39.986976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for i in range(1,785):\n#     pca=PCA(n_components=i)\n#     X_train_trf=pca.fit_transform(X_train)\n#     X_test_trf=pca.transform(X_test)\n#     knn.fit(X_train_trf,y_train)\n#     y_pred=knn.predict(X_test_trf)\n#     print(accuracy_score(y_test,y_pred))\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:39.988677Z","iopub.execute_input":"2025-08-09T13:53:39.98896Z","iopub.status.idle":"2025-08-09T13:53:40.003952Z","shell.execute_reply.started":"2025-08-09T13:53:39.988942Z","shell.execute_reply":"2025-08-09T13:53:40.003017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=2)\nimport plotly.express as px\ny_train_trf = y_train.astype(str)\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)\nfig.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:40.008388Z","iopub.execute_input":"2025-08-09T13:53:40.008708Z","iopub.status.idle":"2025-08-09T13:53:40.115823Z","shell.execute_reply.started":"2025-08-09T13:53:40.008678Z","shell.execute_reply":"2025-08-09T13:53:40.114727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=3)\nX_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:40.117403Z","iopub.execute_input":"2025-08-09T13:53:40.11776Z","iopub.status.idle":"2025-08-09T13:53:40.953846Z","shell.execute_reply.started":"2025-08-09T13:53:40.117734Z","shell.execute_reply":"2025-08-09T13:53:40.953089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:53:40.961992Z","iopub.execute_input":"2025-08-09T13:53:40.962301Z","iopub.status.idle":"2025-08-09T13:53:40.982765Z","shell.execute_reply.started":"2025-08-09T13:53:40.962274Z","shell.execute_reply":"2025-08-09T13:53:40.981878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_trf=y_train.astype(str)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:57:51.532076Z","iopub.execute_input":"2025-08-09T13:57:51.53248Z","iopub.status.idle":"2025-08-09T13:57:51.545952Z","shell.execute_reply.started":"2025-08-09T13:57:51.53245Z","shell.execute_reply":"2025-08-09T13:57:51.545044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig=px.scatter_3d(df,x=X_train_trf[:,0],y=X_train_trf[:,1],z=X_train_trf[:,2],\n                 color=y_train_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T13:59:30.072377Z","iopub.execute_input":"2025-08-09T13:59:30.072728Z","iopub.status.idle":"2025-08-09T13:59:30.199389Z","shell.execute_reply.started":"2025-08-09T13:59:30.072704Z","shell.execute_reply":"2025-08-09T13:59:30.198465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig.update_layout(margin=dict(l=20,r=20,t=20,b=20))\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T14:13:09.194127Z","iopub.execute_input":"2025-08-09T14:13:09.195351Z","iopub.status.idle":"2025-08-09T14:13:09.244843Z","shell.execute_reply.started":"2025-08-09T14:13:09.195288Z","shell.execute_reply":"2025-08-09T14:13:09.243454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T14:40:25.345406Z","iopub.execute_input":"2025-08-09T14:40:25.345799Z","iopub.status.idle":"2025-08-09T14:40:25.353881Z","shell.execute_reply.started":"2025-08-09T14:40:25.345777Z","shell.execute_reply":"2025-08-09T14:40:25.352965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T14:42:25.369967Z","iopub.execute_input":"2025-08-09T14:42:25.370323Z","iopub.status.idle":"2025-08-09T14:42:25.376575Z","shell.execute_reply.started":"2025-08-09T14:42:25.370297Z","shell.execute_reply":"2025-08-09T14:42:25.375585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}