{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30761,"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-09-07T04:43:56.959503Z","iopub.execute_input":"2024-09-07T04:43:56.960047Z","iopub.status.idle":"2024-09-07T04:43:57.000632Z","shell.execute_reply.started":"2024-09-07T04:43:56.959995Z","shell.execute_reply":"2024-09-07T04:43:56.999309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:57.003052Z","iopub.execute_input":"2024-09-07T04:43:57.003531Z","iopub.status.idle":"2024-09-07T04:43:59.356456Z","shell.execute_reply.started":"2024-09-07T04:43:57.003469Z","shell.execute_reply":"2024-09-07T04:43:59.355399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.357917Z","iopub.execute_input":"2024-09-07T04:43:59.359048Z","iopub.status.idle":"2024-09-07T04:43:59.376579Z","shell.execute_reply.started":"2024-09-07T04:43:59.359006Z","shell.execute_reply":"2024-09-07T04:43:59.375212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.379340Z","iopub.execute_input":"2024-09-07T04:43:59.379769Z","iopub.status.idle":"2024-09-07T04:43:59.388896Z","shell.execute_reply.started":"2024-09-07T04:43:59.379721Z","shell.execute_reply":"2024-09-07T04:43:59.387740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.390161Z","iopub.execute_input":"2024-09-07T04:43:59.390563Z","iopub.status.idle":"2024-09-07T04:43:59.402977Z","shell.execute_reply.started":"2024-09-07T04:43:59.390517Z","shell.execute_reply":"2024-09-07T04:43:59.401739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.404342Z","iopub.execute_input":"2024-09-07T04:43:59.404707Z","iopub.status.idle":"2024-09-07T04:43:59.426972Z","shell.execute_reply.started":"2024-09-07T04:43:59.404654Z","shell.execute_reply":"2024-09-07T04:43:59.425621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.428391Z","iopub.execute_input":"2024-09-07T04:43:59.430371Z","iopub.status.idle":"2024-09-07T04:43:59.434668Z","shell.execute_reply.started":"2024-09-07T04:43:59.430326Z","shell.execute_reply":"2024-09-07T04:43:59.433669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[35794,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.435924Z","iopub.execute_input":"2024-09-07T04:43:59.436255Z","iopub.status.idle":"2024-09-07T04:43:59.677373Z","shell.execute_reply.started":"2024-09-07T04:43:59.436220Z","shell.execute_reply":"2024-09-07T04:43:59.676268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X= df.iloc[:,1:]\ny= df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.678800Z","iopub.execute_input":"2024-09-07T04:43:59.679143Z","iopub.status.idle":"2024-09-07T04:43:59.684626Z","shell.execute_reply.started":"2024-09-07T04:43:59.679108Z","shell.execute_reply":"2024-09-07T04:43:59.683469Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.688237Z","iopub.execute_input":"2024-09-07T04:43:59.688617Z","iopub.status.idle":"2024-09-07T04:43:59.919203Z","shell.execute_reply.started":"2024-09-07T04:43:59.688579Z","shell.execute_reply":"2024-09-07T04:43:59.918090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.920447Z","iopub.execute_input":"2024-09-07T04:43:59.920809Z","iopub.status.idle":"2024-09-07T04:43:59.927703Z","shell.execute_reply.started":"2024-09-07T04:43:59.920771Z","shell.execute_reply":"2024-09-07T04:43:59.926710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.928968Z","iopub.execute_input":"2024-09-07T04:43:59.929323Z","iopub.status.idle":"2024-09-07T04:43:59.940420Z","shell.execute_reply.started":"2024-09-07T04:43:59.929286Z","shell.execute_reply":"2024-09-07T04:43:59.939405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.941755Z","iopub.execute_input":"2024-09-07T04:43:59.942124Z","iopub.status.idle":"2024-09-07T04:43:59.949505Z","shell.execute_reply.started":"2024-09-07T04:43:59.942086Z","shell.execute_reply":"2024-09-07T04:43:59.948557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn= KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.950790Z","iopub.execute_input":"2024-09-07T04:43:59.951484Z","iopub.status.idle":"2024-09-07T04:43:59.959040Z","shell.execute_reply.started":"2024-09-07T04:43:59.951436Z","shell.execute_reply":"2024-09-07T04:43:59.957987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:43:59.960311Z","iopub.execute_input":"2024-09-07T04:43:59.960727Z","iopub.status.idle":"2024-09-07T04:44:00.093657Z","shell.execute_reply.started":"2024-09-07T04:43:59.960628Z","shell.execute_reply":"2024-09-07T04:44:00.092695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(X_test)\nprint(time.time()- start)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:44:00.095027Z","iopub.execute_input":"2024-09-07T04:44:00.095387Z","iopub.status.idle":"2024-09-07T04:44:12.081340Z","shell.execute_reply.started":"2024-09-07T04:44:00.095351Z","shell.execute_reply":"2024-09-07T04:44:12.080185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:44:12.082949Z","iopub.execute_input":"2024-09-07T04:44:12.083476Z","iopub.status.idle":"2024-09-07T04:44:12.092592Z","shell.execute_reply.started":"2024-09-07T04:44:12.083432Z","shell.execute_reply":"2024-09-07T04:44:12.091507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler= StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:44:12.093883Z","iopub.execute_input":"2024-09-07T04:44:12.094212Z","iopub.status.idle":"2024-09-07T04:44:12.104006Z","shell.execute_reply.started":"2024-09-07T04:44:12.094178Z","shell.execute_reply":"2024-09-07T04:44:12.102877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=scaler.fit_transform(X_train)\nX_test=scaler.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:44:12.105220Z","iopub.execute_input":"2024-09-07T04:44:12.105534Z","iopub.status.idle":"2024-09-07T04:44:12.662181Z","shell.execute_reply.started":"2024-09-07T04:44:12.105501Z","shell.execute_reply":"2024-09-07T04:44:12.661067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##PCA\nfrom sklearn.decomposition import PCA\npca= PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:46:14.616275Z","iopub.execute_input":"2024-09-07T04:46:14.616698Z","iopub.status.idle":"2024-09-07T04:46:14.622106Z","shell.execute_reply.started":"2024-09-07T04:46:14.616644Z","shell.execute_reply":"2024-09-07T04:46:14.620632Z"},"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-09-07T04:46:15.246407Z","iopub.execute_input":"2024-09-07T04:46:15.247214Z","iopub.status.idle":"2024-09-07T04:46:19.875639Z","shell.execute_reply.started":"2024-09-07T04:46:15.247168Z","shell.execute_reply":"2024-09-07T04:46:19.874388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:44:17.960207Z","iopub.execute_input":"2024-09-07T04:44:17.960643Z","iopub.status.idle":"2024-09-07T04:44:17.967737Z","shell.execute_reply.started":"2024-09-07T04:44:17.960596Z","shell.execute_reply":"2024-09-07T04:44:17.966599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn= KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:44:43.126646Z","iopub.execute_input":"2024-09-07T04:44:43.127085Z","iopub.status.idle":"2024-09-07T04:44:43.131887Z","shell.execute_reply.started":"2024-09-07T04:44:43.127046Z","shell.execute_reply":"2024-09-07T04:44:43.130763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:44:45.446728Z","iopub.execute_input":"2024-09-07T04:44:45.447871Z","iopub.status.idle":"2024-09-07T04:44:45.468203Z","shell.execute_reply.started":"2024-09-07T04:44:45.447823Z","shell.execute_reply":"2024-09-07T04:44:45.466939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=knn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:45:08.912362Z","iopub.execute_input":"2024-09-07T04:45:08.912811Z","iopub.status.idle":"2024-09-07T04:45:10.574353Z","shell.execute_reply.started":"2024-09-07T04:45:08.912768Z","shell.execute_reply":"2024-09-07T04:45:10.573303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T04:45:37.263534Z","iopub.execute_input":"2024-09-07T04:45:37.263964Z","iopub.status.idle":"2024-09-07T04:45:37.273087Z","shell.execute_reply.started":"2024-09-07T04:45:37.263923Z","shell.execute_reply":"2024-09-07T04:45:37.271888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for i in range(500,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_pred=knn.predict(X_test_trf)\n    #accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-09-07T05:08:18.769625Z","iopub.execute_input":"2024-09-07T05:08:18.770043Z","iopub.status.idle":"2024-09-07T05:08:18.774909Z","shell.execute_reply.started":"2024-09-07T05:08:18.770005Z","shell.execute_reply":"2024-09-07T05:08:18.773763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}