{"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":30673,"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-03-28T10:56:22.490049Z","iopub.execute_input":"2024-03-28T10:56:22.490782Z","iopub.status.idle":"2024-03-28T10:56:22.501318Z","shell.execute_reply.started":"2024-03-28T10:56:22.490743Z","shell.execute_reply":"2024-03-28T10:56:22.500164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:22.503474Z","iopub.execute_input":"2024-03-28T10:56:22.503896Z","iopub.status.idle":"2024-03-28T10:56:25.571937Z","shell.execute_reply.started":"2024-03-28T10:56:22.503866Z","shell.execute_reply":"2024-03-28T10:56:25.570705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head(15)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:25.573353Z","iopub.execute_input":"2024-03-28T10:56:25.573669Z","iopub.status.idle":"2024-03-28T10:56:25.600780Z","shell.execute_reply.started":"2024-03-28T10:56:25.573643Z","shell.execute_reply":"2024-03-28T10:56:25.599589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:25.602571Z","iopub.execute_input":"2024-03-28T10:56:25.603431Z","iopub.status.idle":"2024-03-28T10:56:25.608923Z","shell.execute_reply.started":"2024-03-28T10:56:25.603391Z","shell.execute_reply":"2024-03-28T10:56:25.607705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.sample()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:25.612444Z","iopub.execute_input":"2024-03-28T10:56:25.613532Z","iopub.status.idle":"2024-03-28T10:56:25.638410Z","shell.execute_reply.started":"2024-03-28T10:56:25.613490Z","shell.execute_reply":"2024-03-28T10:56:25.637167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data.iloc[38729 , 1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:25.640092Z","iopub.execute_input":"2024-03-28T10:56:25.640622Z","iopub.status.idle":"2024-03-28T10:56:25.924632Z","shell.execute_reply.started":"2024-03-28T10:56:25.640584Z","shell.execute_reply":"2024-03-28T10:56:25.923606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = data.iloc[ : ,1:].values\ny = data.iloc[ : , 0].values","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:25.925986Z","iopub.execute_input":"2024-03-28T10:56:25.926380Z","iopub.status.idle":"2024-03-28T10:56:25.932434Z","shell.execute_reply.started":"2024-03-28T10:56:25.926350Z","shell.execute_reply":"2024-03-28T10:56:25.931319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train , x_test , y_train , y_test = train_test_split(x,y , test_size = 0.2 , random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:25.933989Z","iopub.execute_input":"2024-03-28T10:56:25.934352Z","iopub.status.idle":"2024-03-28T10:56:26.364579Z","shell.execute_reply.started":"2024-03-28T10:56:25.934312Z","shell.execute_reply":"2024-03-28T10:56:26.363017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:26.366695Z","iopub.execute_input":"2024-03-28T10:56:26.367027Z","iopub.status.idle":"2024-03-28T10:56:26.372390Z","shell.execute_reply.started":"2024-03-28T10:56:26.366999Z","shell.execute_reply":"2024-03-28T10:56:26.371029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:26.373698Z","iopub.execute_input":"2024-03-28T10:56:26.374104Z","iopub.status.idle":"2024-03-28T10:56:26.383796Z","shell.execute_reply.started":"2024-03-28T10:56:26.374075Z","shell.execute_reply":"2024-03-28T10:56:26.382587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:26.385372Z","iopub.execute_input":"2024-03-28T10:56:26.385810Z","iopub.status.idle":"2024-03-28T10:56:26.402798Z","shell.execute_reply.started":"2024-03-28T10:56:26.385773Z","shell.execute_reply":"2024-03-28T10:56:26.401634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\npredicted = knn.predict(x_test)\nprint(time.time() - start)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:26.404557Z","iopub.execute_input":"2024-03-28T10:56:26.405271Z","iopub.status.idle":"2024-03-28T10:56:39.257695Z","shell.execute_reply.started":"2024-03-28T10:56:26.405230Z","shell.execute_reply":"2024-03-28T10:56:39.256507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint('Accuracy of the model : ' , accuracy_score(y_test , predicted))","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:39.259176Z","iopub.execute_input":"2024-03-28T10:56:39.259557Z","iopub.status.idle":"2024-03-28T10:56:39.268873Z","shell.execute_reply.started":"2024-03-28T10:56:39.259528Z","shell.execute_reply":"2024-03-28T10:56:39.267482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#applying PCA on the data \nfrom sklearn.preprocessing import StandardScaler\nsc = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:39.273392Z","iopub.execute_input":"2024-03-28T10:56:39.273763Z","iopub.status.idle":"2024-03-28T10:56:39.280007Z","shell.execute_reply.started":"2024-03-28T10:56:39.273732Z","shell.execute_reply":"2024-03-28T10:56:39.278903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = sc.fit_transform(x_train)\nx_test = sc.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:39.281401Z","iopub.execute_input":"2024-03-28T10:56:39.281733Z","iopub.status.idle":"2024-03-28T10:56:39.775432Z","shell.execute_reply.started":"2024-03-28T10:56:39.281705Z","shell.execute_reply":"2024-03-28T10:56:39.774296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n# tranforming the data into lower dimensional data by determing the \n# number of principle components equal to the half of the original features\n\npca = PCA(n_components = 392)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:39.776823Z","iopub.execute_input":"2024-03-28T10:56:39.777141Z","iopub.status.idle":"2024-03-28T10:56:39.783531Z","shell.execute_reply.started":"2024-03-28T10:56:39.777115Z","shell.execute_reply":"2024-03-28T10:56:39.782461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train1 = pca.fit_transform(x_train)\nx_test1 = pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:39.785228Z","iopub.execute_input":"2024-03-28T10:56:39.785575Z","iopub.status.idle":"2024-03-28T10:56:48.582988Z","shell.execute_reply.started":"2024-03-28T10:56:39.785510Z","shell.execute_reply":"2024-03-28T10:56:48.581216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:48.584918Z","iopub.execute_input":"2024-03-28T10:56:48.586356Z","iopub.status.idle":"2024-03-28T10:56:48.592911Z","shell.execute_reply.started":"2024-03-28T10:56:48.586309Z","shell.execute_reply":"2024-03-28T10:56:48.591462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train1 , y_train)\ny_pred = knn.predict(x_test1)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:48.596103Z","iopub.execute_input":"2024-03-28T10:56:48.598813Z","iopub.status.idle":"2024-03-28T10:56:53.270991Z","shell.execute_reply.started":"2024-03-28T10:56:48.598763Z","shell.execute_reply":"2024-03-28T10:56:53.269702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Accuracy of the model after applying PCA : ' , accuracy_score(y_test , y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:53.272527Z","iopub.execute_input":"2024-03-28T10:56:53.272937Z","iopub.status.idle":"2024-03-28T10:56:53.282379Z","shell.execute_reply.started":"2024-03-28T10:56:53.272901Z","shell.execute_reply":"2024-03-28T10:56:53.280952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1,785):\n    pca = PCA(n_components = i)\n    \n    x_train1 = pca.fit_transform(x_train)\n    x_test1 = pca.transform(x_test)\n    \n    knn = KNeighborsClassifier()\n    \n    knn.fit(x_train1 , y_train)\n    y_pred = knn.predict(x_test1)\n    \n    print(f'Accuracy Score of model having {i} principle components' , accuracy_score(y_pred , y_test)) \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-03-28T10:56:53.283789Z","iopub.execute_input":"2024-03-28T10:56:53.284228Z","iopub.status.idle":"2024-03-28T10:57:30.168168Z","shell.execute_reply.started":"2024-03-28T10:56:53.284171Z","shell.execute_reply":"2024-03-28T10:57:30.166403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components = 2)\n    \nx_train2 = pca.fit_transform(x_train)\nx_test2 = pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:01:24.347791Z","iopub.execute_input":"2024-03-28T11:01:24.348270Z","iopub.status.idle":"2024-03-28T11:01:25.343803Z","shell.execute_reply.started":"2024-03-28T11:01:24.348228Z","shell.execute_reply":"2024-03-28T11:01:25.342387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\ny_train2 = y_train.astype(str)\n\nfig = px.scatter(x = x_train2[ : ,0] , y = x_train2[ : ,1], \n                color = y_train2 , \n                color_discrete_sequence = px.colors.qualitative.G10)\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T11:01:26.395156Z","iopub.execute_input":"2024-03-28T11:01:26.395812Z","iopub.status.idle":"2024-03-28T11:01:26.549531Z","shell.execute_reply.started":"2024-03-28T11:01:26.395772Z","shell.execute_reply":"2024-03-28T11:01:26.548356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}