{"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":30746,"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","execution":{"iopub.status.busy":"2024-07-29T11:07:22.676132Z","iopub.execute_input":"2024-07-29T11:07:22.676494Z","iopub.status.idle":"2024-07-29T11:07:23.765734Z","shell.execute_reply.started":"2024-07-29T11:07:22.676463Z","shell.execute_reply":"2024-07-29T11:07:23.764598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.sample(5)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:23.767507Z","iopub.execute_input":"2024-07-29T11:07:23.768496Z","iopub.status.idle":"2024-07-29T11:07:27.462449Z","shell.execute_reply.started":"2024-07-29T11:07:23.768462Z","shell.execute_reply":"2024-07-29T11:07:27.461194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:27.463962Z","iopub.execute_input":"2024-07-29T11:07:27.464391Z","iopub.status.idle":"2024-07-29T11:07:27.474640Z","shell.execute_reply.started":"2024-07-29T11:07:27.464353Z","shell.execute_reply":"2024-07-29T11:07:27.473010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:27.477396Z","iopub.execute_input":"2024-07-29T11:07:27.477840Z","iopub.status.idle":"2024-07-29T11:07:27.497254Z","shell.execute_reply.started":"2024-07-29T11:07:27.477802Z","shell.execute_reply":"2024-07-29T11:07:27.496147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df.iloc[19260,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:27.498534Z","iopub.execute_input":"2024-07-29T11:07:27.498926Z","iopub.status.idle":"2024-07-29T11:07:27.766987Z","shell.execute_reply.started":"2024-07-29T11:07:27.498892Z","shell.execute_reply":"2024-07-29T11:07:27.765722Z"},"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, random_state = 42)\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:27.768370Z","iopub.execute_input":"2024-07-29T11:07:27.768708Z","iopub.status.idle":"2024-07-29T11:07:28.578278Z","shell.execute_reply.started":"2024-07-29T11:07:27.768681Z","shell.execute_reply":"2024-07-29T11:07:28.577014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nknn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:28.579616Z","iopub.execute_input":"2024-07-29T11:07:28.579922Z","iopub.status.idle":"2024-07-29T11:07:28.814363Z","shell.execute_reply.started":"2024-07-29T11:07:28.579896Z","shell.execute_reply":"2024-07-29T11:07:28.813379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:28.815704Z","iopub.execute_input":"2024-07-29T11:07:28.816546Z","iopub.status.idle":"2024-07-29T11:07:28.968000Z","shell.execute_reply.started":"2024-07-29T11:07:28.816480Z","shell.execute_reply":"2024-07-29T11:07:28.967009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" y_predict = knn.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:28.969562Z","iopub.execute_input":"2024-07-29T11:07:28.970329Z","iopub.status.idle":"2024-07-29T11:07:41.613233Z","shell.execute_reply.started":"2024-07-29T11:07:28.970289Z","shell.execute_reply":"2024-07-29T11:07:41.611872Z"},"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-07-29T11:07:41.616773Z","iopub.execute_input":"2024-07-29T11:07:41.617142Z","iopub.status.idle":"2024-07-29T11:07:41.626851Z","shell.execute_reply.started":"2024-07-29T11:07:41.617114Z","shell.execute_reply":"2024-07-29T11:07:41.625429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:41.628205Z","iopub.execute_input":"2024-07-29T11:07:41.628592Z","iopub.status.idle":"2024-07-29T11:07:41.638619Z","shell.execute_reply.started":"2024-07-29T11:07:41.628559Z","shell.execute_reply":"2024-07-29T11:07:41.637193Z"},"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-07-29T11:07:41.640098Z","iopub.execute_input":"2024-07-29T11:07:41.640769Z","iopub.status.idle":"2024-07-29T11:07:42.124546Z","shell.execute_reply.started":"2024-07-29T11:07:41.640735Z","shell.execute_reply":"2024-07-29T11:07:42.123453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components = 200)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:42.125824Z","iopub.execute_input":"2024-07-29T11:07:42.126175Z","iopub.status.idle":"2024-07-29T11:07:42.131166Z","shell.execute_reply.started":"2024-07-29T11:07:42.126138Z","shell.execute_reply":"2024-07-29T11:07:42.130013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:42.132675Z","iopub.execute_input":"2024-07-29T11:07:42.133089Z","iopub.status.idle":"2024-07-29T11:07:46.823865Z","shell.execute_reply.started":"2024-07-29T11:07:42.133052Z","shell.execute_reply":"2024-07-29T11:07:46.822624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:46.825411Z","iopub.execute_input":"2024-07-29T11:07:46.826699Z","iopub.status.idle":"2024-07-29T11:07:46.834643Z","shell.execute_reply.started":"2024-07-29T11:07:46.826645Z","shell.execute_reply":"2024-07-29T11:07:46.833046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:46.836353Z","iopub.execute_input":"2024-07-29T11:07:46.837075Z","iopub.status.idle":"2024-07-29T11:07:46.844152Z","shell.execute_reply.started":"2024-07-29T11:07:46.837028Z","shell.execute_reply":"2024-07-29T11:07:46.842862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:46.845677Z","iopub.execute_input":"2024-07-29T11:07:46.851500Z","iopub.status.idle":"2024-07-29T11:07:46.903140Z","shell.execute_reply.started":"2024-07-29T11:07:46.851425Z","shell.execute_reply":"2024-07-29T11:07:46.902020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict2 = knn.predict(x_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:46.904921Z","iopub.execute_input":"2024-07-29T11:07:46.905712Z","iopub.status.idle":"2024-07-29T11:07:49.361094Z","shell.execute_reply.started":"2024-07-29T11:07:46.905673Z","shell.execute_reply":"2024-07-29T11:07:49.359981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_predict2)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:49.362358Z","iopub.execute_input":"2024-07-29T11:07:49.362752Z","iopub.status.idle":"2024-07-29T11:07:49.371447Z","shell.execute_reply.started":"2024-07-29T11:07:49.362722Z","shell.execute_reply":"2024-07-29T11:07:49.370430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 = KNeighborsClassifier()\n    knn.fit(x_train_trf,y_train)\n    y_predict = knn.predict(x_test_trf)\n    print(accuracy_score(y_test,y_predict))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:07:49.372617Z","iopub.execute_input":"2024-07-29T11:07:49.373276Z","iopub.status.idle":"2024-07-29T11:08:11.801881Z","shell.execute_reply.started":"2024-07-29T11:07:49.373244Z","shell.execute_reply":"2024-07-29T11:08:11.800296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components = 2)\nx_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:10:01.435789Z","iopub.execute_input":"2024-07-29T11:10:01.436294Z","iopub.status.idle":"2024-07-29T11:10:02.402673Z","shell.execute_reply.started":"2024-07-29T11:10:01.436248Z","shell.execute_reply":"2024-07-29T11:10:02.401391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:10:05.602388Z","iopub.execute_input":"2024-07-29T11:10:05.603400Z","iopub.status.idle":"2024-07-29T11:10:05.611099Z","shell.execute_reply.started":"2024-07-29T11:10:05.603351Z","shell.execute_reply":"2024-07-29T11:10:05.609892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px \ny_train_trf = y_train.astype(str)\nfig = px.scatter(x= x_train_trf[:,0],y= x_train_trf[:,1],color = y_train_trf,color_discrete_sequence= px.colors.qualitative.G10)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:10:10.283376Z","iopub.execute_input":"2024-07-29T11:10:10.283798Z","iopub.status.idle":"2024-07-29T11:10:12.939495Z","shell.execute_reply.started":"2024-07-29T11:10:10.283766Z","shell.execute_reply":"2024-07-29T11:10:12.938360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:10:51.915885Z","iopub.execute_input":"2024-07-29T11:10:51.916332Z","iopub.status.idle":"2024-07-29T11:10:51.923639Z","shell.execute_reply.started":"2024-07-29T11:10:51.916301Z","shell.execute_reply":"2024-07-29T11:10:51.922488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:11:37.667822Z","iopub.execute_input":"2024-07-29T11:11:37.668196Z","iopub.status.idle":"2024-07-29T11:11:37.677594Z","shell.execute_reply.started":"2024-07-29T11:11:37.668169Z","shell.execute_reply":"2024-07-29T11:11:37.676415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-29T11:12:18.341833Z","iopub.execute_input":"2024-07-29T11:12:18.342225Z","iopub.status.idle":"2024-07-29T11:12:22.236587Z","shell.execute_reply.started":"2024-07-29T11:12:18.342196Z","shell.execute_reply":"2024-07-29T11:12:22.235404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:15:39.691626Z","iopub.execute_input":"2024-07-29T11:15:39.692012Z","iopub.status.idle":"2024-07-29T11:15:39.699790Z","shell.execute_reply.started":"2024-07-29T11:15:39.691985Z","shell.execute_reply":"2024-07-29T11:15:39.698784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:16:17.314053Z","iopub.execute_input":"2024-07-29T11:16:17.314420Z","iopub.status.idle":"2024-07-29T11:16:17.320949Z","shell.execute_reply.started":"2024-07-29T11:16:17.314393Z","shell.execute_reply":"2024-07-29T11:16:17.319975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)* 100","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:17:09.792611Z","iopub.execute_input":"2024-07-29T11:17:09.793010Z","iopub.status.idle":"2024-07-29T11:17:09.807209Z","shell.execute_reply.started":"2024-07-29T11:17:09.792981Z","shell.execute_reply":"2024-07-29T11:17:09.805805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npx.line(np.cumsum(pca.explained_variance_ratio_)* 100)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T11:19:52.799075Z","iopub.execute_input":"2024-07-29T11:19:52.799471Z","iopub.status.idle":"2024-07-29T11:19:52.890870Z","shell.execute_reply.started":"2024-07-29T11:19:52.799442Z","shell.execute_reply":"2024-07-29T11:19:52.889809Z"},"trusted":true},"execution_count":null,"outputs":[]}]}