{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":30699,"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","execution":{"iopub.status.busy":"2024-06-13T10:31:23.977143Z","iopub.execute_input":"2024-06-13T10:31:23.978097Z","iopub.status.idle":"2024-06-13T10:31:23.988992Z","shell.execute_reply.started":"2024-06-13T10:31:23.978054Z","shell.execute_reply":"2024-06-13T10:31:23.987739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport plotly.express as px\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:23.991065Z","iopub.execute_input":"2024-06-13T10:31:23.991472Z","iopub.status.idle":"2024-06-13T10:31:23.998242Z","shell.execute_reply.started":"2024-06-13T10:31:23.991441Z","shell.execute_reply":"2024-06-13T10:31:23.997061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:23.999659Z","iopub.execute_input":"2024-06-13T10:31:24.000027Z","iopub.status.idle":"2024-06-13T10:31:26.431462Z","shell.execute_reply.started":"2024-06-13T10:31:23.999997Z","shell.execute_reply":"2024-06-13T10:31:26.430158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[1:2,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:26.432769Z","iopub.execute_input":"2024-06-13T10:31:26.433079Z","iopub.status.idle":"2024-06-13T10:31:26.696593Z","shell.execute_reply.started":"2024-06-13T10:31:26.433054Z","shell.execute_reply":"2024-06-13T10:31:26.695304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:26.699774Z","iopub.execute_input":"2024-06-13T10:31:26.700146Z","iopub.status.idle":"2024-06-13T10:31:26.705714Z","shell.execute_reply.started":"2024-06-13T10:31:26.700116Z","shell.execute_reply":"2024-06-13T10:31:26.704633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PCA for Data Visulisation","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=2)\nx_2D=pca.fit_transform(x)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:26.707064Z","iopub.execute_input":"2024-06-13T10:31:26.707427Z","iopub.status.idle":"2024-06-13T10:31:27.875285Z","shell.execute_reply.started":"2024-06-13T10:31:26.707397Z","shell.execute_reply":"2024-06-13T10:31:27.874073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=px.scatter(x=x_2D[:,0],y=x_2D[:,1],color=y)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:27.876865Z","iopub.execute_input":"2024-06-13T10:31:27.877233Z","iopub.status.idle":"2024-06-13T10:31:27.966332Z","shell.execute_reply.started":"2024-06-13T10:31:27.877203Z","shell.execute_reply":"2024-06-13T10:31:27.964709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=3)\nx_3D=pca.fit_transform(x)\nx_3D","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:27.968081Z","iopub.execute_input":"2024-06-13T10:31:27.968489Z","iopub.status.idle":"2024-06-13T10:31:29.193802Z","shell.execute_reply.started":"2024-06-13T10:31:27.968455Z","shell.execute_reply":"2024-06-13T10:31:29.193026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_3d=pd.DataFrame(x_3D)\ndf_3d['number']=y\ndf_3d.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:29.194927Z","iopub.execute_input":"2024-06-13T10:31:29.196040Z","iopub.status.idle":"2024-06-13T10:31:29.209189Z","shell.execute_reply.started":"2024-06-13T10:31:29.196000Z","shell.execute_reply":"2024-06-13T10:31:29.208089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=px.scatter_3d(df_3d, x=0, y=1, z=2,color='number')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:29.210556Z","iopub.execute_input":"2024-06-13T10:31:29.210890Z","iopub.status.idle":"2024-06-13T10:31:29.320103Z","shell.execute_reply.started":"2024-06-13T10:31:29.210862Z","shell.execute_reply":"2024-06-13T10:31:29.318627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:29.321978Z","iopub.execute_input":"2024-06-13T10:31:29.323105Z","iopub.status.idle":"2024-06-13T10:31:29.590610Z","shell.execute_reply.started":"2024-06-13T10:31:29.323056Z","shell.execute_reply":"2024-06-13T10:31:29.589349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:29.592465Z","iopub.execute_input":"2024-06-13T10:31:29.592888Z","iopub.status.idle":"2024-06-13T10:31:29.606286Z","shell.execute_reply.started":"2024-06-13T10:31:29.592851Z","shell.execute_reply":"2024-06-13T10:31:29.604960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Machine Learing model Without PCA ","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:29.611272Z","iopub.execute_input":"2024-06-13T10:31:29.612017Z","iopub.status.idle":"2024-06-13T10:31:29.616765Z","shell.execute_reply.started":"2024-06-13T10:31:29.611978Z","shell.execute_reply":"2024-06-13T10:31:29.615497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:29.621696Z","iopub.execute_input":"2024-06-13T10:31:29.622556Z","iopub.status.idle":"2024-06-13T10:31:29.763869Z","shell.execute_reply.started":"2024-06-13T10:31:29.622512Z","shell.execute_reply":"2024-06-13T10:31:29.762576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart=time.time()\ny_predict=knn.predict(x_test)\nprint(time.time()-start)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:29.766436Z","iopub.execute_input":"2024-06-13T10:31:29.766801Z","iopub.status.idle":"2024-06-13T10:31:42.479537Z","shell.execute_reply.started":"2024-06-13T10:31:29.766772Z","shell.execute_reply":"2024-06-13T10:31:42.478400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_predict)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:42.480897Z","iopub.execute_input":"2024-06-13T10:31:42.481236Z","iopub.status.idle":"2024-06-13T10:31:42.492603Z","shell.execute_reply.started":"2024-06-13T10:31:42.481206Z","shell.execute_reply":"2024-06-13T10:31:42.491453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Machine Learning Model With PCA","metadata":{}},{"cell_type":"markdown","source":"## Scale the data ","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:42.494740Z","iopub.execute_input":"2024-06-13T10:31:42.495095Z","iopub.status.idle":"2024-06-13T10:31:42.502304Z","shell.execute_reply.started":"2024-06-13T10:31:42.495064Z","shell.execute_reply":"2024-06-13T10:31:42.501212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=scaler.fit_transform(x_train)\nx_test=scaler.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:42.503639Z","iopub.execute_input":"2024-06-13T10:31:42.503943Z","iopub.status.idle":"2024-06-13T10:31:42.999532Z","shell.execute_reply.started":"2024-06-13T10:31:42.503919Z","shell.execute_reply":"2024-06-13T10:31:42.998255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Use PCA On Data","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:43.001245Z","iopub.execute_input":"2024-06-13T10:31:43.001715Z","iopub.status.idle":"2024-06-13T10:31:43.007564Z","shell.execute_reply.started":"2024-06-13T10:31:43.001676Z","shell.execute_reply":"2024-06-13T10:31:43.006416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_p=pca.fit_transform(x_train)\nx_test_P=pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:43.009018Z","iopub.execute_input":"2024-06-13T10:31:43.009468Z","iopub.status.idle":"2024-06-13T10:31:47.764163Z","shell.execute_reply.started":"2024-06-13T10:31:43.009428Z","shell.execute_reply":"2024-06-13T10:31:47.762785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_p.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:47.765827Z","iopub.execute_input":"2024-06-13T10:31:47.766354Z","iopub.status.idle":"2024-06-13T10:31:47.774856Z","shell.execute_reply.started":"2024-06-13T10:31:47.766297Z","shell.execute_reply":"2024-06-13T10:31:47.773662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train_p,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:47.776419Z","iopub.execute_input":"2024-06-13T10:31:47.776955Z","iopub.status.idle":"2024-06-13T10:31:47.831098Z","shell.execute_reply.started":"2024-06-13T10:31:47.776917Z","shell.execute_reply":"2024-06-13T10:31:47.829990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict=knn.predict(x_test_P)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:47.832731Z","iopub.execute_input":"2024-06-13T10:31:47.833401Z","iopub.status.idle":"2024-06-13T10:31:50.241609Z","shell.execute_reply.started":"2024-06-13T10:31:47.833363Z","shell.execute_reply":"2024-06-13T10:31:50.240282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_predict)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:50.243160Z","iopub.execute_input":"2024-06-13T10:31:50.243629Z","iopub.status.idle":"2024-06-13T10:31:50.253699Z","shell.execute_reply.started":"2024-06-13T10:31:50.243590Z","shell.execute_reply":"2024-06-13T10:31:50.252475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_number=[]\npca_accuracy=[]\nfor i in range(1,105):\n    pca=PCA(n_components=i)\n    x_train_p=pca.fit_transform(x_train)\n    x_test_P=pca.transform(x_test)\n    \n    \n    knn=KNeighborsClassifier()\n    knn.fit(x_train_p,y_train)\n    y_predict=knn.predict(x_test_P)\n    pca_number.append(i)\n    t=accuracy_score(y_test,y_predict)\n    pca_accuracy.append(t)\n    print(\"Whan PCA value is\",i,\"than accuracy is =>\",t)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:31:50.254949Z","iopub.execute_input":"2024-06-13T10:31:50.255288Z","iopub.status.idle":"2024-06-13T10:39:47.362618Z","shell.execute_reply.started":"2024-06-13T10:31:50.255260Z","shell.execute_reply":"2024-06-13T10:39:47.360782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(x=pca_number,y=pca_accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T10:39:47.364802Z","iopub.execute_input":"2024-06-13T10:39:47.365425Z","iopub.status.idle":"2024-06-13T10:39:47.454515Z","shell.execute_reply.started":"2024-06-13T10:39:47.365290Z","shell.execute_reply":"2024-06-13T10:39:47.452907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}